Linux file permissions
What rwx, 755, chmod, chown, and umask mean, and how the special bits work.
Every file on Linux has permissions. Permissions decide who can read the file, who can change it, and who can run it.
If you have ever copied chmod 755 from a tutorial without knowing what it does, this is for you.
In the examples, $ is the prompt. Do not type it.
Read ls -l
ls -l shows the permissions of a file.
$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 59944 Feb 7 2025 /usr/bin/passwd
Look at the first ten characters: -rwsr-xr-x.
- The first character is the file type.
-means a normal file.dmeans a folder.lmeans a link. - The next three are for the owner:
rws. - The next three are for the group:
r-x. - The last three are for everyone else:
r-x.
r means read. w means write. x means run it as a program. A - means no permission.
The s is a special permission. We will get to it.
The rest of the line is the owner, group, size, and date. You can ignore it for now.
The number code
Linux also writes permissions as numbers. Each permission has a value:
- read = 4
- write = 2
- run = 1
Add the values for each group.
rwx= 4 + 2 + 1 = 7r-x= 4 + 0 + 1 = 5r--= 4
So 755 means rwxr-xr-x.
- Owner: 7 = read, write, run
- Group: 5 = read, run
- Others: 5 = read, run
644 means rw-r--r--. 600 means rw-------. A private key should be 600.
Change permissions with chmod
chmod changes permissions. You can use letters or numbers.
Letters:
u= ownerg= groupo= othersa= everyone+= add-= remove== set exactly
chmod u+x script.sh # owner can run it
chmod o-w notes.txt # others cannot write
chmod ug=rw,o= shared.log
Numbers replace all permissions at once. chmod 600 file does not add to the old permissions. It replaces them.
chmod 600 ~/.ssh/id_ed25519
chmod 755 /srv/public
~ means your home folder. If you do not have an SSH key, try it on a test file:
touch notes.txt
ls -l notes.txt
chmod 600 notes.txt
ls -l notes.txt
touch makes an empty file.
Folders are different
On a folder, the permissions mean:
r: you can list the file names.x: you can enter the folder and open files by name.w: you can create, delete, and rename files, but only if you also havex.
If you remove x from a folder, you can see names with r, but you cannot open anything inside. That surprises a lot of people.
Change the owner with chown
chown changes who owns a file. chgrp changes the group. Most people use chown for both.
sudo chown youruser app.log
sudo chown youruser:devs app.log
sudo chown -R youruser:devs /srv/app
Replace youruser with your username. sudo runs the command as the root user.
-R means "do this to every file inside the folder too." It is powerful. Test it on a throwaway folder first.
Default permissions with umask
When a program creates a file, it asks for 0666. For a folder, it asks for 0777. Then the system removes some permissions based on your umask.
$ umask
022
022 removes write from the group and write from others.
- New file:
644=rw-r--r-- - New folder:
755=rwxr-xr-x
If you want group members to be able to write new files, use umask 002. This is common on shared servers.
The special bits
Three extra bits sit in front of the normal nine. They replace the x with s or t.
The first number is the special bit: 4755 for setuid, 2775 for setgid, 1777 for sticky. In letters: chmod u+s, chmod g+s, chmod +t.
setuid
If a program has setuid, it runs as the file's owner, not as you. The passwd program uses this. It needs to write to /etc/shadow, which normal users cannot read.
That s in rws is setuid.
setuid is dangerous. A bug in a setuid-root program can give someone root. Check for them:
find /usr -perm -4000 -type f 2>/dev/null
-perm -4000 means the setuid bit is set. That 4 is the same 4 in 4755.
If you make a setuid-root program, keep it owned by root and make sure other users cannot write to it. Also, setuid does nothing on shell scripts. It only works on programs written in languages like C.
setgid
On a program, setgid does the same thing for the group. On a folder, it means new files get the folder's group instead of the creator's group. This is useful for shared folders.
sudo mkdir /srv/team
sudo chgrp devs /srv/team
sudo chmod 2775 /srv/team
Sticky bit
On a folder, the sticky bit means only the file's owner can delete or rename it. /tmp uses this.
$ ls -ld /tmp
drwxrwxrwt 25 root root 4096 Mar 22 09:12 /tmp
That t is the sticky bit. Without it, anyone could delete anyone else's files in /tmp.
Bottom line
r,w,xmean read, write, run.- The three groups are owner, group, others.
- The number code is 4, 2, 1.
chmodchanges permissions.chownchanges the owner.umasksets defaults.- setuid, setgid, and sticky are extra rules for special cases.
The man pages are the full reference: man 2 chmod and man 1 chmod. If you work with capabilities or ACLs, the capabilities and ACL man pages cover the edge cases.