DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · SECURITY · 14 OF 187

Linux Users, Groups, Permissions, sudo and ACLs

Who can do what on a Linux server: users and groups, the nine permission bits and their numbers (644, 640, 600), umask 0022 turning new files into 644, ownership with chown, special bits (setuid on passwd and sudo, the sticky bit on /tmp), ACLs to grant one extra user access, and sudo with least privilege, each tested with a real second user on Ubuntu 24.04.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • uid and groups
  • rwx for owner, group, other
  • 644 / 640 / 600 / 755
  • umask
  • ACLs
  • setuid, sticky
  • sudo

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.

Who am I
$ 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-- (our new file)
-  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.

Our machine
$ 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.

As user dev1 (our runs)
$ 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.

Directory bits
  • 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 (our run)
$ 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.

Seen on our machine
-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.

sudo, safely
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What do 644, 640, 600 and 755 mean?

Owner/group/other permissions as read=4, write=2, execute=1: 644 rw-r--r--, 640 rw-r-----, 600 rw-------, 755 rwxr-xr-x (programs and folders).

Q2What is umask?

A mask of permission bits removed from new files and folders; 0022 turns requested 666/777 into 644/755.

Q3What does execute permission mean on a directory?

Permission to enter it and reach the files inside; you need x on every folder in a path to open a file.

Q4What is setuid?

A bit that makes a program run with its owner's identity (for example root for passwd and sudo); powerful and a common security risk.

Q5What is the sticky bit?

On a shared folder like /tmp, it lets only a file's owner (or root) delete or rename it.

Q6When would you use an ACL?

To give an additional user or group access to a file or folder without changing its owner or group (setfacl/getfacl).

Q7How should sudo be configured?

With visudo and files in /etc/sudoers.d, granting the minimum specific commands to specific users or groups; every use is logged.

Q8Why should services run as their own users?

Least privilege: a compromised or buggy service can only touch its own files, not the whole system.