Users, groups and permissions
Linux was built for many people sharing one computer, and servers still work that way: there's you, the administrator, and separate accounts for each service (www-data for the web server, postgres for the database). Each account can only touch what it's allowed to. That's a big part of what keeps a server safe when one program is broken into.
Who am I?
whoami
id
groupsyou uid=1001(you) gid=1001(you) groups=1001(you),4(adm),999(systemd-journal) you adm systemd-journal
Every user has a numeric user ID (uid) and belongs to one main group plus any number of extra groups. The system works with the numbers; names are for people.
Where accounts are stored
grep -E "^(root|you|www-data):" /etc/passwd
echo "---"
grep -E "^(sudo|you|www-data):" /etc/group
echo "---"
sudo grep "^you:" /etc/shadow | cut -c1-24root:x:0:0:root:/root:/bin/bash www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin you:x:1001:1001::/home/you:/bin/bash --- sudo:x:27:ubuntu www-data:x:33: you:x:1001: --- you:!:20779:0:99999:7:::
/etc/passwd has one line per account, separated by :
you:x:1001:1001::/home/you:/bin/bash
│ │ │ │ │ │ └── login shell
│ │ │ │ │ └──────────── home folder
│ │ │ │ └────────────── description (often empty)
│ │ │ └─────────────────── main group ID
│ │ └──────────────────────── user ID
│ └────────────────────────── password is stored in /etc/shadow
└────────────────────────────── user name- uid 0 is
root, the all-powerful administrator. - System accounts (uid below 1000) run services. Their shell is usually
/usr/sbin/nologin, so nobody can log in as them. - Passwords are stored hashed in
/etc/shadow, which only root can read. The second field holds the hash;!(as foryouabove) means no password is set, so that account can't log in with a password. It can still log in with an SSH key, as you'll see in the SSH lesson.
root and sudo
Logging in as root is risky: one typo can destroy the system. Instead, normal users who are allowed to administer the machine (members of the sudo group on Ubuntu, wheel on Fedora and RHEL) run single commands as root with sudo:
cat /etc/shadow > /dev/null
echo "exit code: $?"
sudo head -1 /etc/shadow | cut -d: -f1
sudo -u www-data whoami
sudo -l | tail -2cat: /etc/shadow: Permission denied
exit code: 1
root
www-data
User you may run the following commands on devbox:
(ALL) NOPASSWD: ALLsudo commandruns a command as root, asking for your password (and remembering it for a few minutes).sudo -u user commandruns it as another user.sudo -iopens a root shell. Use it sparingly andexitas soon as you're done.- Every
sudocommand is logged, so admins can see who did what.
Who may use sudo is configured in /etc/sudoers and /etc/sudoers.d/. Always edit them with sudo visudo, which checks the syntax before saving. A broken sudoers file can lock everyone out of sudo.
Managing users and groups
sudo useradd --create-home --shell /bin/bash asha
sudo useradd --system --no-create-home --shell /usr/sbin/nologin shopapp
sudo groupadd developers
sudo usermod -aG developers asha
sudo usermod -aG developers you
id asha
id shopapp
getent group developersuid=1002(asha) gid=1002(asha) groups=1002(asha),1003(developers) uid=999(shopapp) gid=996(shopapp) groups=996(shopapp) developers:x:1003:asha,you
| Command | Does |
|---|---|
useradd -m -s /bin/bash name |
Create a user with a home folder (Ubuntu also has the friendlier adduser name) |
useradd --system name |
Create a service account |
passwd name |
Set a user's password |
usermod -aG group name |
Add a user to an extra group |
groupadd name |
Create a group |
userdel -r name |
Delete a user and their home folder |
getent passwd name |
Look a user up (works with LDAP and other directories too) |
⚠️ Always use
-aG(append).usermod -G developers ashawithout-areplaces all of Asha's groups.
Group changes apply to new logins. After adding yourself to a group, log out and in again (or run newgrp developers) before it takes effect.
Permissions recap
Each file has an owner, a group, and three sets of permissions: for the owner (u), the group (g) and everyone else (o). Each set is any of read (4), write (2) and execute (1).
touch report.txt
chmod 640 report.txt
ls -l report.txt
stat -c "%A %a %U:%G %n" report.txt-rw-r----- 1 you you 0 Nov 22 10:48 report.txt -rw-r----- 640 you:you report.txt
640 means owner rw- (6), group r-- (4), others --- (0). If you need a refresher on chmod, see the Permissions and processes lesson in the Terminal track.
Changing ownership: chown
sudo mkdir -p /srv/shop
sudo chown you:developers /srv/shop
sudo chmod 2775 /srv/shop
ls -ld /srv/shop
touch /srv/shop/index.html
ls -l /srv/shop/index.htmldrwxrwsr-x 2 you developers 4096 Nov 22 10:48 /srv/shop -rw-r--r-- 1 you developers 0 Nov 22 10:48 /srv/shop/index.html
chown user:group pathchanges both;chown -Rdoes it for a whole folder tree.- Only root can give files away to another user.
That 2 in 2775 is the setgid bit (shown as s in the group's x position). On a folder, it makes new files inside inherit the folder's group (developers above) instead of the creator's main group. It's the standard way to set up a folder that a team shares.
Special bits
| Bit | On a folder | On a program |
|---|---|---|
setuid (4000, s in owner) |
— | Runs as the file's owner (e.g. passwd runs as root to edit /etc/shadow) |
setgid (2000, s in group) |
New files inherit the folder's group | Runs with the file's group |
sticky (1000, t in others) |
Users can only delete their own files (that's how /tmp works) |
— |
ls -l /usr/bin/passwd
ls -ld /tmp-rwsr-xr-x 1 root root 72056 May 30 2024 /usr/bin/passwd drwxrwxrwt 1 root root 4096 Nov 22 10:48 /tmp
Default permissions: umask
New files get their permissions from the umask, which removes bits from the maximum (666 for files, 777 for folders):
umask
touch a.txt && mkdir b
stat -c "%a %n" a.txt b
(umask 077; touch private.txt; stat -c "%a %n" private.txt)0022 644 a.txt 755 b 600 private.txt
The usual umask 022 gives files 644 and folders 755. 077 makes everything private to you, which is a good choice for scripts that write secrets.
Fine-grained access: ACLs
When owner/group/others isn't enough, for example "the deploy user also needs to write here", access control lists add permissions for specific users:
sudo useradd -m deploy
setfacl -m u:deploy:rwx /srv/shop
getfacl -p /srv/shop | grep -v "^#"
ls -ld /srv/shop | cut -c1-11user::rwx user:deploy:rwx group::rwx mask::rwx other::r-x drwxrwsr-x+
The + at the end of the permissions means "this has an ACL, run getfacl to see it".
Practice
- Create a user
raviwith a home folder and Bash as the shell, then add them to a group calledops. - Make a shared folder
/srv/reportsowned by theopsgroup where new files automatically belong toops. - Create a file that only you can read and write, and check its numeric permissions with
stat -c %a. - Find every setuid program on the system:
find / -perm -4000 -type f 2>/dev/null.
1.Which file lists user accounts?
2.What does sudo usermod -aG docker asha do?
3.What are the permissions of a file with mode 640?
4.What is the sticky bit on /tmp for?