Each permission has a value
Admins usually write permissions as numbers. Each letter has a value and you add them up:
| Letter | Value |
|---|---|
r | 4 |
w | 2 |
x | 1 |
- | 0 |
So rwx = 4+2+1 = 7, rw- = 4+2 = 6, r-x = 4+1 = 5 and r-- = 4. Three digits give the owner, the group and the others, in that order:
| Number | Letters | Typical use |
|---|---|---|
644 | rw-r--r-- | Normal files: you edit, everyone reads |
755 | rwxr-xr-x | Programs, scripts and directories |
600 | rw------- | Private files: passwords, SSH keys |
700 | rwx------ | Private directories and scripts |
640 | rw-r----- | Your group may read, others nothing |
777 | rwxrwxrwx | Everyone may do everything: almost always a mistake |
chmod with numbers
chmod 600 diary.txt
chmod 755 hello.sh
ls -l diary.txt hello.sh
stat -c '%a %A %n' diary.txt hello.shIn the terminal
student@vulntech:~$ chmod 600 diary.txt student@vulntech:~$ chmod 755 hello.sh student@vulntech:~$ ls -l diary.txt hello.sh -rw------- 1 student student 35 Oct 17 06:30 diary.txt -rwxr-xr-x 1 student student 40 Oct 17 06:30 hello.sh student@vulntech:~$ stat -c '%a %A %n' diary.txt hello.sh 600 -rw------- diary.txt 755 -rwxr-xr-x hello.sh
Numbers always set all the permissions at once. Letters are handier to change just one (chmod g+w).
Common mistake: Permission denied? Just chmod 777 it! That fixes the error by letting every user on the machine read, change and run the file. On a web server, a writable 777 script is an open door for attackers. Find out who needs access and give exactly that.
Tip: Security audits search for world-writable files:
find /srv -perm -o+wlists everything others may write.
Remember: r = 4, w = 2, x = 1. Digits for owner, group, others. 644 files, 755 programs and folders, 600 secrets.