Linux File Permissions: rwx, chmod, chown, umask and Special Bits
How do Linux file permissions work?
Linux file permissions control who can read, write and execute each file and directory. Every file has an owner and a group, and three permission sets apply to the owner, the group and everyone else. You view them with ls -l, change them with chmod, change ownership with chown or chgrp, and set defaults for new files with umask.
Reading the output of ls -l
$ ls -l
-rw-r----- 1 alice finance 2048 Nov 10 09:15 budget.xlsx
drwxr-x--- 2 alice finance 4096 Nov 10 09:10 reports
-rwxr-xr-x 1 root root 8192 Nov 9 18:02 backup.shThe first character is the file type: - for a regular file, d for a directory and l for a symbolic link. The next nine characters are three groups of three: the user (owner), the group and others. Then come the link count, owner name, group name, size, modification time and name.
So -rw-r----- on budget.xlsx means alice can read and write, members of the finance group can read, and nobody else has any access. A + after the nine characters means the file also has an access control list, which you can view with getfacl.
What r, w and x mean on files and directories
| Permission | On a file | On a directory |
|---|---|---|
| r (read) | Read the contents | List the names inside it |
| w (write) | Change the contents | Create, delete and rename entries inside it (needs x too) |
| x (execute) | Run it as a program or script | Enter it with cd and reach files inside it |
The directory rules surprise many learners. Deleting a file depends on write permission on the directory, not on the file. A directory with r but no x lets you see names but not open anything. The root user bypasses these checks, which is one reason to avoid working as root, as explained in Linux for network engineers.
Numeric (octal) chmod
Each permission has a value: read is 4, write is 2 and execute is 1. Add them for each of user, group and others to get three digits.
| Mode | String | Typical use |
|---|---|---|
| 644 | rw-r--r-- | Ordinary files, web pages |
| 640 | rw-r----- | Files shared with a group only |
| 600 | rw------- | Private keys, files with credentials |
| 755 | rwxr-xr-x | Directories, scripts and programs |
| 750 | rwxr-x--- | Directories for one team |
| 700 | rwx------ | A private directory such as ~/.ssh |
chmod 640 budget.xlsx
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519 Symbolic chmod, chown and chgrp
Symbolic mode changes individual bits without retyping the rest. Use u, g, o or a (all), then + to add, - to remove or = to set exactly.
chmod u+x deploy.sh # owner may execute
chmod g-w shared.txt # group may no longer write
chmod o= private.txt # remove all access for others
chmod u=rw,g=r,o= notes.txt # same as 640
chmod -R g+rX projects/ # group read; X adds execute only to directories
# (and files already executable)
sudo chown bob report.txt # change owner
sudo chown bob:finance report.txt # change owner and group
sudo chgrp finance report.txt # change group only
sudo chown -R www-data:www-data /var/www/siteOnly root can give a file to another user. An owner can change the group to one they belong to. Use -R carefully: a recursive change on the wrong path can break a system or expose data.
umask: default permissions for new files
New files start from 666 and new directories from 777. The umask lists the bits to remove. With the common umask 022, new files become 644 and new directories 755. A stricter umask of 027 gives 640 and 750, so others get nothing.
umask # show the current value, for example 0022
umask 027 # set it for this shell sessionTo make a change permanent for your account, add the umask line to your shell start-up file such as ~/.bashrc. System-wide defaults are set in files such as /etc/login.defs, depending on the distribution. Services started by systemd use the UMask= setting in their unit files.
setuid, setgid and the sticky bit
| Bit | Octal | Shows as | Effect |
|---|---|---|---|
| setuid | 4000 | s in the user execute position | A program runs with the privileges of the file owner, as /usr/bin/passwd does to update password data |
| setgid | 2000 | s in the group execute position | On a directory, new files inherit the directory group; on a program, it runs with the file group |
| sticky | 1000 | t in the others execute position | On a directory, users can delete only their own files, as in /tmp |
$ ls -ld /tmp /usr/bin/passwd
drwxrwxrwt 18 root root 4096 Nov 10 09:00 /tmp
-rwsr-xr-x 1 root root 68248 Nov 1 12:00 /usr/bin/passwd
sudo chmod 2775 /srv/shared # setgid shared team directory
sudo find / -perm -4000 -type f 2>/dev/null # list setuid programsA capital S or T means the special bit is set but the matching execute bit is not, which is usually a mistake. Setuid programs owned by root are a classic route to privilege escalation, so security audits list them and compare against a known baseline, as covered in security auditing. Linux ignores setuid on shell scripts.
Practical exercise: set up a shared team directory
On a Linux virtual machine you own, with sudo access:
- Create a group
labteamand two users, and add both users to the group. - Create
/srv/labteam, set its group tolabteamand its mode to2770. - As each user, create a file inside it and confirm with
ls -lthat the group islabteam. - Set the umask to
007in a shell and create another file; confirm group members can edit it. - Add the sticky bit with
chmod +tand confirm one user can no longer delete a file owned by the other. - Confirm a third user outside the group cannot list the directory.
You are done when every check behaves as expected and you can explain each permission string you see. More system administration practice is in the Networking Foundation course, and everyday shell tools are in essential Linux commands.
Common mistakes
- Running
chmod 777to fix an access error, which lets every user change or replace the file. - Running
chmod -R 755on a tree of files, which makes every data file executable. - Removing execute from a directory and then wondering why nobody can open files inside it.
- Leaving private keys or configuration files with passwords readable by others.
- Setting the setuid bit on a program without understanding that it runs with the privileges of its owner.
Frequently asked questions
Why can I not delete a file even though I own it?
Deleting needs write and execute permission on the directory that holds the file. If the directory also has the sticky bit, only the file owner, the directory owner or root can delete it.
What is the difference between chmod and chown?
chmod changes what the owner, group and others may do. chown changes who the owner and group are.
When should I use ACLs instead of normal permissions?
Use access control lists when one file needs different access for several specific users or groups, which owner, group and others cannot express. Set them with setfacl -m u:bob:r file and view them with getfacl.