Lesson 4 of 12 · 10 min · Users and access
Permissions and ownership
What you will learn
- How to read the ten characters at the start of
ls -l - What read, write and execute mean for a file and for a directory
chmodin symbolic (u+x) and numeric (640) form, andchown- What
umaskdoes and why new files are644and new directories755 - The special bits: setuid, setgid and the sticky bit
In simple words
Every file in Linux has a lock with three sets of keys: one for its owner, one for a group of people, and one for everybody else. Each set says whether that person may read the file, change it, or run it. This lesson teaches you to read those locks and change them safely.
Words to know
- Owner: the user a file belongs to.
- Group: a named set of users who share the same access to a file.
- Permission: what someone may do with a file: read it (r), write to it (w) or run it (x).
- chmod: the command that changes a file's permissions.
Numbers and letters
Each bit has a value: r=4, w=2, x=1. Add them per class: rw- is 6, r-- is 4. So rw-r----- is 640.
chmod 640 f
stat -c '%A %a %U:%G %n' f # %A symbolic, %a octal, owner:group, name
-rw-r----- 640 root:root f
Symbolic mode changes only what you say. u user (owner), g group, o others, a all; + adds, - removes, = sets:
chmod u+x,g-r f # owner gets execute, group loses read
stat -c '%A %a' f
chmod o+r f # others get read
stat -c '%A %a' f
-rwx------ 700
-rwx---r-- 704
Common numbers you should recognise: 644 (file everyone can read, owner can write), 755 (program or directory everyone can use, owner can change), 600 (private file such as an SSH private key), 700 (private directory), 664/775 (group-shared). 777 means anyone can change it, and it is almost never the right answer; if a deployment "needs 777", something else (the owner or group) is wrong.
chmod -R applies to a whole tree. Be careful: giving every file x is usually wrong. Directories need x, regular files mostly don't. The capital X helps: chmod -R u=rwX,g=rX,o= dir sets x only on directories (and files that already have it).
Changing the owner
sudo chown asha f # change owner
sudo chown asha:devs f # owner and group
sudo chown -R asha:devs app/ # a whole directory tree
sudo chgrp devs f # group only
Only root can give a file away; ordinary users can change the group of their own files to a group they belong to. After chown asha:devs a.txt:
-rw-r--r-- 1 asha devs 8 Nov 12 12:28 a.txt
Why services care
A web server runs as a low-privilege user (for example www-data or nginx). If it cannot read its config or write its log directory, the problem is almost always an owner/group/mode mismatch, not the application. namei -l /path/to/file prints the permissions of every directory on the way, which finds the blocked one quickly.
umask: the default for new files
New files start as 666 and new directories as 777, then the umask removes bits:
umask
touch u; mkdir ud
stat -c '%a' u ud
0022
644
755
022 removes write from group and others: files become 644, directories 755. A stricter umask 077 makes new files private (600). Services and scripts often set it on purpose when they create secrets.
The special bits
mkdir /srv/team
chgrp devs /srv/team
chmod 2770 /srv/team
ls -ld /srv/team
drwxrws--- 2 root devs 4096 Nov 12 12:28 /srv/team
| Bit | Number | Effect |
|---|---|---|
| setuid | 4000 | An executable runs with its owner's privileges. /usr/bin/passwd is root-owned and setuid so any user can change their own password. Shown as s in the owner's x slot. |
| setgid | 2000 | On an executable: runs with the file's group. On a directory: new files inside inherit the directory's group. That is how shared team folders work, as above (s in the group slot). |
| sticky | 1000 | On a directory: you may only delete or rename entries you own, even if you can write to the directory. /tmp has it: drwxrwxrwt (the t). |
Setuid programs are a security risk if they have bugs, so audit them: find / -perm -4000 -type f 2>/dev/null.
Common mistakes
chmod 777to make an error disappear. It hides a wrong owner. Fix the owner or group.- Forgetting directory
x. "Permission denied" on a file you can read often means a parent directory lacksxfor you. chmod -R 644on a tree. Directories losexand become unusable.- SSH key permissions.
sshrefuses a private key that others can read. Usechmod 600 ~/.ssh/id_ed25519andchmod 700 ~/.ssh.
Try it yourself
- Create a file, set it to 640, and write down who can read it. Then change it with symbolic mode so the group can also write.
- As a normal user (root ignores these checks), make a directory with a file in it, remove
xfrom your own permissions on the directory, and trycdinto it, thenlsit. Explain the results. - Create
/tmp/sharedwith the setgid bit and a group, create a file in it as another user (or just as yourself) and check that the file's group is inherited. - Run
ls -l /etc/passwd /etc/shadowand explain why the second file is stricter.
Check yourself
A file is -rw-r----- with owner asha and group devs. What can user ravi (in group devs) and user kiran (not in devs) do?
Ravi matches the group class, so he can read but not write. Kiran falls into "others", which has no permissions, so he can do nothing with it.
What does chmod 755 dir mean for a directory?
Owner can list, enter and change entries (7); group and others can list and enter but not change (5). It is the normal mode for shared directories and programs.
A user has write permission on a file but still cannot delete it. Why?
Deleting removes a name from the directory, so what matters is write (and execute) permission on the directory, plus the sticky bit if set.
Why is /tmp mode 1777?
Everyone must be able to create files there (777), and the sticky bit (1) prevents users from deleting each other's files.
Interview questions
Explain the permission string -rwxr-x--- 1 deploy www app.sh.
A regular file named app.sh, owned by user deploy and group www. The owner can read, write and execute; the group can read and execute; others have no access. In numbers, 750.
What is setuid, and give an example.
A bit on an executable that makes it run with the file owner's privileges instead of the caller's. passwd is the standard example: it must write /etc/shadow, which only root can.
A web server returns 403 on files you just uploaded. What do you check?
The files' mode and owner (does the web server's user have read?), execute on every parent directory (namei -l), and then any security layer such as SELinux or AppArmor.
Summary
ls -lshows type, owner, group and three permission sets; the first matching class applies.- On directories,
wmeans create/delete entries andxmeans enter; file deletion depends on the directory. chmodtakes numbers (4/2/1) or symbolic changes;chown user:groupsets ownership; prefer fixing ownership to opening permissions.umasksubtracts from the defaults; setuid, setgid and sticky solve privilege, shared-folder and/tmpproblems.
Going deeper
- chmod(1) manual page: the complete mode syntax.
- credentials(7): how Linux decides which user and group a process acts as.
- GNU Coreutils: file permissions: permission bits explained by the GNU project.
See how much of this you can explain
A free mock interview asks you new questions on Linux for DevOps for DevOps Engineer and scores every answer, with what to fix.