Notes
Users and groups
Every process runs as a user, identified by a number called the uid, and belongs to one or more groups. The id command shows yours. Root has uid zero. Users are listed in slash etc slash passwd, groups in slash etc slash group, and password hashes in shadow, readable only by root. Services run as their own users, like www data for web servers or postgres for the database, usually with no login at all. That way a hacked service cannot touch everything.
$ id uid=0(root) gid=0(root) groups=0(root) $ useradd -m dev1 /etc/passwd users /etc/group groups /etc/shadow password hashes service users: www-data, postgres (no login)
Permission bits
Each file has an owner, a group, and nine permission bits: read, write and execute, for the owner, the group, and everyone else. Each set adds up to a number: read is four, write two, execute one. So r w is six and r alone is four. Our new file showed six four four: owner reads and writes, everyone else reads. Six four zero hides it from others, six zero zero keeps it to the owner, and seven five five is the usual mode for programs and folders.
- rw- r-- r-- owner group other r = 4 w = 2 x = 1 rw- = 6, r-- = 4, --- = 0 644 -rw-r--r-- 640 -rw-r----- 600 -rw------- 755 -rwxr-xr-x programs, dirs
umask
Why six four four? Programs ask for six six six on new files, and the umask removes bits from that. Our umask was zero zero two two, which removes write for the group and for others, giving six four four for files and seven five five for folders. A umask of zero seven seven makes everything private by default. Services can set their own umask in their systemd unit, which matters for apps that write files other services read.
$ umask 0022 new file: 666 minus 022 = 644 new dir: 777 minus 022 = 755 umask 077 → files 600, dirs 700 (private by default) services: UMask= in the unit
Test it
Let's test with a real second user. We made the file six four zero and set its group to dev one. As dev one, cat printed the secret, because the group has read. Then we changed it to six zero zero. Now dev one got permission denied: only the owner may read. The kernel checks in order: if you are the owner, it uses the owner bits; else if you are in the group, the group bits; else the others bits.
$ chmod 640 s.txt $ chown root:dev1 s.txt $ su dev1 -c "cat s.txt" secret ← group read $ chmod 600 s.txt $ su dev1 -c "cat s.txt" cat: s.txt: Permission denied
Folders and execute
On folders the bits mean something slightly different. Read lets you list the names, write lets you create, delete and rename files inside, and execute lets you enter the folder and reach files in it. To read a config file deep in a path, you need execute on every folder along the way, plus read on the file. namei minus l prints the permissions of every part of a path, the fastest way to find the folder that blocks you.
- r list the names
- w create, delete, rename files inside
- x enter it and reach files
- to read /srv/app/config.yml you need x on /, /srv and /srv/app, plus r on the file
- namei -l <path> shows each
ACLs
Sometimes you need one extra user to read a file, without changing its group. Access control lists, ACLs, add extra rules. setfacl gave dev one read access to the private file, getfacl showed the new rule, and dev one could read it again. ls marks such files with a plus sign at the end of the permissions, which is worth knowing when permissions look right but behave strangely.
$ setfacl -m u:dev1:r s.txt $ getfacl -c s.txt user::rw- user:dev1:r-- group::--- mask::r-- $ ls -l s.txt -rw-r-----+ ... ← + means ACL
Special bits
Three special bits. Setuid, shown as an s in the owner's execute spot, makes a program run as its owner: passwd and sudo are setuid root, which is how a normal user can change their own password. Setuid files are prime targets for attackers, so audit them. The sticky bit, the t on slash tmp, stops people deleting each other's files. And setgid on a folder makes new files inherit the folder's group, handy for shared team folders.
-rwsr-xr-x /usr/bin/passwd -rwsr-xr-x /usr/bin/sudo s = setuid: runs as the file's owner (root) drwxrwxrwt /tmp t = sticky: only owners may delete their files setgid dir: new files keep the folder's group
sudo
To do admin work, use sudo: run one command as root, after checking a rule, and log it. Rules live in files under sudoers dot d, always edited with visudo, which checks the syntax so you cannot lock yourself out. Grant specific commands, not everything: a deploy user that may only restart the ShopRung service. Never work as root all day; sudo for each risky command keeps you careful and leaves an audit trail.
- run one command as root
- rules in /etc/sudoers.d/ , edited with visudo
- grant specific commands: deploy ALL=(root) /usr/bin/systemctl restart shoprung
- every use is logged
- never: log in as root daily
Common mistakes
Mistakes. chmod minus R seven seven seven to make an error go away, which hands write access to everyone. Running applications as root. Secrets files readable by every user. Forgetting execute on a parent folder. Editing sudoers without visudo and locking everyone out. And sharing one admin account, so nobody knows who did what.
- chmod -R 777 to "fix" it
- apps running as root
- secrets readable by others (644 on .env)
- missing x on a parent folder
- editing /etc/sudoers without visudo
- sharing one admin account
Say it like this
In the interview: first I check which user the service runs as, in its unit file or with ps. Then namei minus l on the path, because it needs execute on every folder and read on the file. I fix it with the right owner or group, for example root as owner, the app's group, mode six forty, or an ACL for one extra user. Never seven seven seven. Each service runs as its own unprivileged user, and admins use sudo with narrow rules.
- "I check which user the service
- runs as, then namei -l on the path: x on every folder, r on the file. I fix ownership or group, for example root:app 640, or add an ACL. Never 777; the service runs as its own user
- with least privilege."