Opscraft
Linux · Lesson 2 of 10 · 6 min read

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. The x means 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 docker group effectively gives them root: they can run docker run -v /:/host --privileged and 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.

Enlarge diagram-rwxr-x---chmod 750 filethe same permissions in octaltype- d lowner (u)rwx = 4+2+1 = 7group (g)r-x = 4+0+1 = 5others (o)--- = 0r = 4 · readw = 2 · writex = 1 · execute (or enter a folder)
Figure 1. Reading the permission string from ls -l. Each group of three adds up to one octal digit: r = 4, w = 2, x = 1.
Swipe sideways to see all of it, or tap to open it full screen.
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.

Enlarge diagramA process (user deploy, groups: deploy, docker) tries to read /srv/app.conf1. You own the file?yes → use the owner bits2. Your group owns it?yes → use the group bits3. Neitheruse the others bitsSELinux / AppArmor policy check (if enforcing)even root is limited hereAllowedPermission deniedFirst match winsIf you own a file with mode 077, you are denied even though "others" may read it. root (UID 0) skips the rwxcheck, but not SELinux.
Figure 2. How Linux decides whether a process may open a file. Only the first matching class is checked.
Swipe sideways to see all of it, or tap to open it full screen.

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.

Enlarge diagramstarts at 666rw-rw-rw-umask 022removes w from group andothersresult 644rw-r--r--starts at 777rwxrwxrwxumask 022removes w from group andothersresult 755rwxr-xr-xNew file−=New folder−=
Figure 3. The umask switches bits off in new files and folders. With the usual umask 022, nobody but the owner can write.
Swipe sideways to see all of it, or tap to open it full screen.

Beyond rwx: ACLs, capabilities, SELinux

  • ACLs give extra users/groups specific rights: setfacl -m u:jenkins:rx /srv/releases, view with getfacl. A + at the end of ls -l permissions 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_ADMIN adds 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 getenforce and ausearch -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. ~/.ssh should be 700 and authorized_keys 600, owned by the user, or sshd ignores them.