Permission denied
Why can ana not read a file that is sitting right there?
Access is a comparison: the file’s owner, group and permissions against the user and groups the command runs as.
Sooner or later every command you type ends in two words: permission denied. Why? Nothing is wrong with cat. The kernel refused to open the file for you. l s dash l shows the file’s side: it belongs to root and to the group shadow, and the letters say what the owner, the group and everyone else may do. id shows your side: your user and your groups. You are not the owner and not in the group, so you are everyone else, and everyone else gets nothing.
Users and groups
The kernel knows you by numbers: a UID and your groups
Accounts live in /etc/passwd, hashes in /etc/shadow and memberships in /etc/group; files store only the numbers.
Every account is a line of text. In the password file: the name, a placeholder for the password, the user ID, the primary group, a comment, the home directory and the login shell. The real password hash lives in the shadow file, which only root can read, with the rules for when it expires. The group file lists groups and their extra members; wheel, or sudo on Ubuntu, is the group allowed to act as root. Underneath, it is all numbers. Zero is root, below a thousand are services, and people start at a thousand.
/etc/shadow · Going deeper
Eight fields hold the hash and every ageing rule
Shadow fields store the hash, last change, minimum, maximum, warning, inactivity and expiry; chage edits them.
File permissions
Nine bits: read, write and execute, for three kinds of user
Permissions are r, w and x for the owner, the group and others; each set is an octal digit, so rwxr-xr-- is 754.
Every file carries nine permission bits. Here is a deployment script. The first three bits are for the owner, the next three for the file’s group, the last three for everyone else. Each three are read, write and execute. Give them values four, two and one and each group becomes a single digit: seven, five, four. So chmod seven five four sets all nine in one go: the owner can do anything, the team can run it, and everyone else can only read it.
chmod, chgrp, chown
How do you change who may do what?
chmod sets the permission bits with letters or numbers, chgrp changes the group and chown, as root, the owner.
Reading permissions is half of it. Here is how you change them. This script has no x, so not even its owner can run it. c h mod u plus x adds execute for the owner, and now it runs. Numbers set all nine at once: six four zero is read and write for the owner, read for the group, nothing for others. c h g r p hands the file to another group, one you belong to. And giving a file to another owner is reserved for root.
Permission checks
Exactly one of the three sets applies, checked in order
The kernel uses the owner bits if you own the file, else the group bits if you are in its group, else the other bits.
Which three bits apply to you? When ana opens a file, the kernel asks first: is she the owner? If so, it uses the owner bits, and only those. If not, is she in the file’s group? Then the group bits. Otherwise, the bits for others. The sets are never combined, so an owner with no permissions is refused even when everyone else is allowed. And remember the folders: you need execute on every directory along the path.
Directory permissions
On a folder, write means you may delete what is inside
On a directory r lists, w creates and deletes entries, x enters; the sticky bit limits deletion to each file’s owner.
On a directory the three bits mean something different. Read lets you list the names. Write lets you create, rename and delete entries. Execute lets you enter it and reach anything below. That leads to a surprise: in a folder anyone can write to, anyone can delete anyone’s file, even a read-only one, because deleting changes the folder, not the file. That is why slash tmp has the sticky bit, shown as t. With it, only a file’s owner can delete it.
Directory permissions · Going deeper · 1 of 3
New files start at 666, and the umask takes bits away
The umask clears bits from the default 666 for files and 777 for directories: umask 022 gives 644 and 755.
What permissions does a new file get? Programs ask for six six six, read and write for all, and the umask clears bits from that. With the common umask of zero two two, the file ends up six four four. Directories start from seven seven seven and become seven five five. Ubuntu gives each user their own group and a umask of zero zero two, so group members can write. And zero seven seven makes everything private. Notice that no new file is ever executable.
Directory permissions · Going deeper · 2 of 3
setuid, setgid and sticky: a fourth digit with real power
setuid runs a program as its owner, setgid on a directory passes its group to new files, and sticky protects deletes.
Three more bits form a fourth digit in front. Setuid, worth four, makes a program run as its owner. That is how passwd, owned by root, can write the shadow file for any user. Setgid, worth two, on a directory makes new files inherit the directory’s group. So chmod two seven seven five gives a team a shared folder where everything stays in the team’s group. Sticky, worth one, is the slash tmp protection you just saw.
Directory permissions · Going deeper · 3 of 3
Give one more person access without a new group
An ACL adds named users and groups to a file’s permissions; setfacl sets them, getfacl shows them, + marks them.
Sometimes the owner, one group and everyone else is not enough. An access control list adds entries for named users and groups. Here raj gets read access to a report without joining the group. ls shows a plus sign, and getfacl lists every entry, including the mask, which caps what named entries can get. On a directory, a default ACL is inherited by every new file created inside.
sudo
One command as root, if a rule allows it, and on the record
sudo checks /etc/sudoers, runs a single command as root with your own password, and logs it; edit rules with visudo.
Nobody should work as root all day. Instead, ana puts sudo in front of the one command that needs it. sudo checks its rules, and if one allows it, runs that command as root. She types her own password, not root’s. Every use is logged. A rule has four parts: who, on which host, as which user, and which commands. The usual rule lets the wheel group run anything; a careful one names a single program. Always edit the rules with visudo.
Account management
Create, group, age and remove an account in five commands
useradd, passwd, usermod -aG, chage and userdel manage the account lifecycle; chage sets password ageing.
Managing accounts takes a handful of commands. Password ageing first: with a maximum of ninety days and seven days of warning, warnings start on day eighty-three, the password expires on day ninety, and after fourteen more inactive days the account locks. useradd creates an account, passwd sets its password, usermod dash a capital G adds a group, and userdel dash r removes it with its home. Forget the dash a, and every other extra group is removed.
PAM and limits · Going deeper
Every login passes through PAM, which also sets limits
PAM modules authenticate, check the account and set session limits from limits.conf; SSSD adds LDAP or AD users.
Lab
Build a team folder and prove who can reach it
Create users and a group, a setgid folder, an ACL for one outsider, a sudo rule for one command, and a password policy.
In this lab you create two users and a team group, build a shared folder that keeps files in the team, prove that an outsider is refused, let one person in with an ACL, and set a password policy. Press the down arrow for the steps.
Lab · Going deeper
Users, a team folder and a password policy
Run these as root (sudo -i) on a lab machine. Every step can be checked with ls -l, id, getfacl or chage -l.
Run these as root (sudo -i) on a lab machine. Every step can be checked with ls -l, id, getfacl or chage -l. Steps. Create the people. groupadd devops; useradd -m -G devops raj; useradd -m ana; passwd raj. Read the new lines in /etc/passwd, /etc/shadow and /etc/group. Decode permissions. ls -l /etc/passwd /etc/shadow /usr/bin/passwd. Write each as octal and say why passwd needs the s. Make a team folder. mkdir /srv/team; chgrp devops /srv/team; chmod 2770 /srv/team. As raj, touch a file in it and check its group. Prove the refusal. su - ana -c "ls /srv/team". Walk the check: is ana the owner, in the group, or other? Grant by ACL. setfacl -m u:ana:rx /srv/team; getfacl /srv/team; repeat the listing as ana. Then add a default ACL so new files are readable too. Test the sticky bit. chmod 1777 /srv/drop; have raj create a file and ana try to delete it. Remove the t and try again. Change the umask. As ana run umask 077; touch private.txt; ls -l. Make it permanent in ~/.bashrc and explain the result in octal. Delegate one command. visudo -f /etc/sudoers.d/raj with raj ALL=(root) /usr/bin/systemctl; as raj run sudo -l and one allowed and one refused command. Age a password. chage -M 90 -W 7 -I 14 raj; chage -l raj. Then chage -d 0 raj and log in as raj to see the forced change. Check: ana is refused before the ACL and allowed after it, raj’s files in /srv/team belong to devops, and sudo -l shows raj only the one command.
Steps
- Create the people. groupadd devops; useradd -m -G devops raj; useradd -m ana; passwd raj. Read the new lines in /etc/passwd, /etc/shadow and /etc/group.
- Decode permissions. ls -l /etc/passwd /etc/shadow /usr/bin/passwd. Write each as octal and say why passwd needs the s.
- Make a team folder. mkdir /srv/team; chgrp devops /srv/team; chmod 2770 /srv/team. As raj, touch a file in it and check its group.
- Prove the refusal. su - ana -c "ls /srv/team". Walk the check: is ana the owner, in the group, or other?
- Grant by ACL. setfacl -m u:ana:rx /srv/team; getfacl /srv/team; repeat the listing as ana. Then add a default ACL so new files are readable too.
- Test the sticky bit. chmod 1777 /srv/drop; have raj create a file and ana try to delete it. Remove the t and try again.
- Change the umask. As ana run umask 077; touch private.txt; ls -l. Make it permanent in ~/.bashrc and explain the result in octal.
- Delegate one command. visudo -f /etc/sudoers.d/raj with raj ALL=(root) /usr/bin/systemctl; as raj run sudo -l and one allowed and one refused command.
- Age a password. chage -M 90 -W 7 -I 14 raj; chage -l raj. Then chage -d 0 raj and log in as raj to see the forced change.
Lesson complete
You finished “Users, groups and permissions”
Nicely done.