Part 1 · Basics · Lesson 10 · 55 minutes
Control who can read, write and run
- Read the owner, group and permission bits of a file and a directory
- Change permissions with chmod and set a safe default with umask
- Find files with the SUID and SGID bits and world-writable directories
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
A developer left a copy of a web shop on a shared server, with its folder structure intact. Your team lead says: "Some files in there are too open. A deploy key, a script, an upload folder. Find what is wrong and close it, without breaking the site." You get the folder. Everything you need is in the permission bits.
Recall
In a long listing, the line of a folder starts with "d". What does the first character of a line tell you?
It is the type of the entry: - for a file, d for a directory, and l for a symbolic link. The nine characters after it are the permissions.
Why do you list an archive with "tar -tf" before you extract it?
An archive from outside can hold names that leave the target folder, links to other places, and files with the SUID bit. The list shows them before they can do harm.
The idea
Every file has an owner (one user) and a group. Three sets of bits say what the owner, the group and everyone else may do.
| Bit | On a file | On a directory |
|---|---|---|
r read (4) | Read the content. | List the names inside. |
w write (2) | Change the content. | Create, rename and delete entries. |
x execute (1) | Run it as a program. | Enter it, and reach the files inside by name. |
For a compiled program (machine code), x alone is enough to run it. A script also needs r, because the shell must read it to run it: with 751, other users get "Permission denied" on a script. A directory needs x to enter it and r to list it.
- Owner → report: owner: read, write and run
- Group member → report: group: read and run
- Other user → report: others: run only
rwxr-x--x is three groups of three. Add the numbers inside each group and you get the octal form:
Three extra bits exist. SUID on a program makes it run as the owner of the file, not as the user who starts it. SGID does the same for the group. On a directory, the sticky bit lets only the owner of a file delete it (as in /tmp). SUID on a program that belongs to root is a risk: a flaw in the program gives the attacker root.
Worked example
1. Say who you are
id prints your user ID and your groups. Linux works with the numbers, and it shows the names next to them.
iduid=1000(you) gid=1000(you) groups=1000(you),27(sudo),100(users)Your groups differ. The group sudo (wheel on some systems) means that you may use sudo. getent passwd "$USER" (get entries) prints your line of the account database. Its third and fourth fields are the same two numbers.
2. Read a permission string
Compare a file that everyone may read with one that only root and a group may read.
ls -l /etc/passwd /etc/shadow-rw-r--r-- 1 root root 1990 Nov 2 14:33 /etc/passwd
-rw-r----- 1 root shadow 1031 Nov 2 14:33 /etc/shadow/etc/passwd lists accounts, so every program must read it. /etc/shadow holds the password hashes, so others get no access at all.
3. Change permissions with chmod
chmod (change mode) sets the permission bits. Make a file with a fake key. A new file starts as 664 here. Close it for others in two steps: first 640 (you read and write, your group reads), then u=rw,go= so that only you can use it.
mkdir -p ~/krlab/linux-basics/perm && cd ~/krlab/linux-basics/perm
echo "api_key=1234" > notes.txt
ls -l notes.txt
chmod 640 notes.txt
ls -l notes.txt
chmod u=rw,go= notes.txt
ls -l notes.txt-rw-rw-r-- 1 you you 13 Nov 21 08:37 notes.txt
-rw-r----- 1 you you 13 Nov 21 08:37 notes.txt
-rw------- 1 you you 13 Nov 21 08:37 notes.txtThe octal form (640) sets every bit at once. The symbolic form changes some: u, g, o pick the owner, group, others; +, -, = add, remove, set; so u=rw,go= means "owner gets read and write, group and others get nothing". For a secret, 600 is the usual answer.
4. See how a directory differs
Without x you cannot reach a file inside, even if you know its name. Without r you cannot list the names.
mkdir box && echo hi > box/a.txt
chmod 600 box
command ls box
cat box/a.txt
chmod 100 box
cat box/a.txt
ls box
chmod 700 boxa.txt
cat: box/a.txt: Permission denied
hi
ls: cannot open directory 'box': Permission deniedcommand ls runs ls without the alias (lesson 3). On Ubuntu, plain ls is an alias with colour, and colour needs the details that x protects, so ls box would also print cannot access 'box/a.txt'. With 600 you can list a.txt but not open it. With 100 you can open it by name but not list.
5. Set the default with umask
umask shows, and sets, the bits that a new file does not get. A file starts from 666 and a directory from 777, minus the umask. touch makes an empty file (lesson 4).
umask
(umask 027; touch a; mkdir d; ls -ld a d)0002
-rw-r----- 1 you you 0 Nov 21 08:37 a
drwxr-x--- 2 you you 40 Nov 21 08:37 dThe brackets run the change in a sub-shell, so your own umask stays as it was. Your value may be 0022, which gives 644 files.
6. Change owner and group (needs root)
chown changes the owner and chgrp the group. sudo runs one command as root. Only root may give a file to another user, so this step is not run here.
sudo chown root:adm notes.txt
sudo chgrp developers notes.txt7. Hunt for risky bits
Make a lab: a SUID program, a world-writable directory, and a safe one with the sticky bit. cp /bin/true copies a harmless program that only exits with success, and && (lesson 6) runs the next command only if the first one worked.
mkdir -p lab/drop lab/inbox
cp /bin/true lab/mytool && chmod u+s lab/mytool
chmod 777 lab/drop
chmod 1777 lab/inbox
ls -l labtotal 36
drwxrwxrwx 2 you you 40 Nov 21 10:59 drop
drwxrwxrwt 2 you you 40 Nov 21 10:59 inbox
-rwsr-xr-x 1 you you 35288 Nov 21 10:59 mytoolThe s in the place of the owner's x is the SUID bit, and the t at the end of inbox is the sticky bit.
find (lesson 8) can test permissions. -perm -4000 means "has at least the SUID bit". -0002 means "others may write", and ! negates the next test, so ! -perm -1000 removes the directories that have the sticky bit.
find lab -type f -perm -4000
find lab -type d -perm -0002 ! -perm -1000lab/mytool
lab/droplab/inbox is correctly not in the list. Now look at the real system. 2>/dev/null (lesson 6) throws away the error messages.
find /usr/bin /usr/sbin -perm -4000 -type f 2>/dev/null/usr/bin/su
/usr/bin/mount
/usr/bin/umount
/usr/bin/newgrp
/usr/bin/gpasswd
/usr/bin/chfn
/usr/bin/passwd
/usr/bin/chsh
/usr/bin/sudoYour list is longer or different, and its order can differ. On Ubuntu 26.04 the entry for sudo can have another name (for example sudo.ws), because that release uses a newer sudo. sudo is on the list on purpose: it must run as root to work. Each name is a program that runs as root for any user, so each is a target. The list is normal on a clean system; a new name in it is not. find -perm -2000 finds the SGID files.
8. Use sudo with least privilege
sudo COMMAND runs one command as root and writes a log line. sudo -l lists what you may run. It asks for your password, so it is not run here. Least privilege means: give each user and each program only the access that the job needs, and use root for one command, not for a whole session.
Your turn
The folder of the developer is below. Run this block once. It builds the data with fixed modes, so it is the same for everyone. It is not the task.
mkdir -p ~/krlab/linux-basics/perm-task && cd ~/krlab/linux-basics/perm-task
mkdir -p keys web/upload web/cache bin
echo "token=k9f2" > keys/deploy.key
echo "token=r41x" > keys/readonly.key
echo "<h1>shop</h1>" > web/index.html
printf '#!/bin/sh\necho saving\n' > bin/save.sh
cp bin/save.sh bin/old.sh
chmod 666 keys/deploy.key
chmod 640 keys/readonly.key
chmod 644 web/index.html
chmod 777 web/upload
chmod 1777 web/cache
chmod 4755 bin/save.sh
chmod 777 bin/old.sh
chmod 755 . keys web binThere are no steps here. Look for the faults with the tools of the worked example, in this order: files that anyone may write, files with the SUID bit, and world-writable directories without the sticky bit. Then answer from your own terminal.
Now fix keys/deploy.key so that only you can read and write it. Run ls -l on it and read the permission string.
Check yourself
A file is "-rwxr-x--x". What is its octal mode, and who can read it?
751: 7 for the owner, 5 for the group, 1 for others. The owner and the group can read it. Others have no r, so they cannot read it. If it is a compiled program, they can still run it. If it is a script, they cannot run it, because the shell must read a script.
You may write to a file that you do not own, but you cannot delete it. Why?
Deleting changes the directory, not the file. You need w and x on the directory. In a directory with the sticky bit you also must own the file.
Why is a world-writable directory without the sticky bit a risk?
Any user can delete or rename the files of other users in it. An attacker can swap a file that a privileged program expects to find there.
Field notes
- Never fix a problem with
chmod 777. Find which user or group needs access, then give that one the bit it needs. - Linux ignores SUID on shell scripts. A SUID program is machine code, and its bugs are what attackers look for. In an audit, compare your SUID list to a clean install of the same version and look at each difference.
- Mount options add protection:
nosuid,noexecandnodevon/tmp,/var/tmpand user data stop a planted SUID or program from working. CIS Benchmarks for Linux list these settings. - Access control lists give finer rules than three classes:
getfaclandsetfacl. A+after the permission string inls -lmeans an ACL exists, so the string alone may not be the whole story. - Run single commands with
sudo, not a root shell. Edit the sudo rules only withvisudo, which checks the syntax.