Users, Groups and Permissions

Control who can read, write and execute what on a Linux system, and grant elevated access without handing out root.

Beginner · about 5 hours · difficulty 2/5

Learn first: Linux Command Line and Packages

Permissions are the first layer of least privilege. Misconfigured permissions cause both security holes and baffling "permission denied" failures in deploys.

The model

  • Every file has an owner, a group, and read/write/execute bits for owner, group and others. Read them with ls -l, change them with chmod (symbolic u+x or octal 640) and chown user:group.
  • On directories, x means "can enter" and r means "can list".
  • Special bits: setuid, setgid (new files inherit the directory group) and the sticky bit (as on /tmp).
  • umask sets default permissions for new files.

Users and privilege

  • Accounts live in /etc/passwd and /etc/shadow; groups in /etc/group. Use useradd, usermod -aG, id.
  • Service accounts: run each daemon as its own non-login user.
  • sudo with rules in /etc/sudoers.d/ (edit via visudo) grants specific commands instead of full root.
  • Beyond basics: ACLs (setfacl), and mandatory access control with SELinux or AppArmor.

Pitfalls

chmod -R 777 to "fix" an error, running apps as root, and disabling SELinux instead of reading its audit log.

Good enough to move on: you can create a service user, give a deploy group write access to one directory only, and grant a single sudo command.

Key concepts

  • rwx bits, chmod and chown
  • Users, groups and service accounts
  • sudo and sudoers
  • setuid, setgid, sticky bit
  • umask
  • ACLs
  • SELinux / AppArmor

Mini project

Create an app user and a deployers group. Make /srv/app writable by deployers only, run a small service as the app user, and allow deployers to restart just that service with sudo.

Check yourself

Why can a user sometimes delete a file they have no write permission on?

Free resources