Users, groups and permissions
Identities
Every process runs as a UID and a primary GID, plus supplementary groups. The kernel only cares about the numbers; names come from:
/etc/passwd—name:x:UID:GID:comment:home:shell. Thexmeans the password hash lives in/etc/shadow(readable only by root)./etc/group— groups and their members.- UID 0 is root. System/service accounts usually have UIDs below 1000 and a shell of
/usr/sbin/nologin.
id deploy # uid, gid, groups
useradd -m -s /bin/bash deploy
usermod -aG docker deploy # -a APPEND; without -a, -G REPLACES all supplementary groups
passwd -l deploy # lock the password
Group changes apply to new login sessions — after usermod -aG docker, the user must log out and back in (or run newgrp docker).
Watch out: adding a user to the
dockergroup effectively gives them root: they can rundocker run -v /:/host --privilegedand own the host.
sudo
sudo runs a command as another user (root by default) according to /etc/sudoers and /etc/sudoers.d/*. Always edit with visudo — it syntax-checks, and a broken sudoers file can lock everyone out of root.
# /etc/sudoers.d/deploy
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp
Least privilege means granting specific commands, not ALL.
Permission bits
ls -l shows -rwxr-x---: file type, then three triplets for owner, group, others.
| Bit | On a file | On a directory |
|---|---|---|
r (4) |
Read contents | List entry names |
w (2) |
Modify contents | Create, delete, rename entries inside (even files you don't own!) |
x (1) |
Execute | Enter (cd) and access entries by name |
Octal adds per triplet: 754 = rwx (7) r-x (5) r-- (4).
chmod 640 app.conf # rw-r-----
chmod u+x,go-w deploy.sh # symbolic form
chown app:app /srv/data -R # owner and group, recursively
Deleting a file depends on write permission on the directory, not on the file. That's why /tmp needs the sticky bit.
Special bits
| Bit | Octal | Effect |
|---|---|---|
| setuid | 4000 | An executable runs as the file's owner (e.g. /usr/bin/passwd runs as root to edit /etc/shadow) |
| setgid | 2000 | On an executable: runs with the file's group. On a directory: new files inherit the directory's group — useful for shared team folders |
| sticky | 1000 | On a directory: only a file's owner (or root) can delete it — /tmp is 1777 (drwxrwxrwt) |
Setuid binaries are a classic privilege-escalation path; audit them with find / -perm -4000 -type f 2>/dev/null.
umask
The umask removes permissions from newly created files. Files start from 666, directories from 777; a umask of 022 gives 644 files and 755 directories. 027 gives 640/750 — nothing for "others". Set per user in shell profiles or per service with UMask= in a systemd unit.
Beyond rwx: ACLs, capabilities, SELinux
- ACLs give extra users/groups specific rights:
setfacl -m u:jenkins:rx /srv/releases, view withgetfacl. A+at the end ofls -lpermissions means an ACL exists. - Capabilities split root's power into pieces. A web server can bind port 80 without being root:
setcap cap_net_bind_service=+ep /usr/bin/myserver. Containers drop most capabilities by default;--cap-add NET_ADMINadds one back. - SELinux (RHEL, Amazon Linux) and AppArmor (Ubuntu) are mandatory access control: even root processes are confined by policy. "Permission denied although permissions look right" on RHEL is often SELinux — check
getenforceandausearch -m avc//var/log/audit/audit.log. The fix is to relabel (restorecon -Rv /path) or set the right boolean, not to disable SELinux.
Watch out: SSH refuses private keys readable by others ("UNPROTECTED PRIVATE KEY FILE"). Fix with
chmod 600 key.pem.~/.sshshould be 700 andauthorized_keys600, owned by the user, or sshd ignores them.