Linux Administration

Linux Users, Groups and Permissions: rwx Bits, umask, ACLs and sudo

Beginner10 min readUpdated

Every access decision on a Linux box comes down to a numeric user id, a set of group ids and twelve bits attached to a file. This guide shows where accounts are stored, how the kernel evaluates a permission check, and how to widen or narrow access safely with chmod, umask, ACLs and sudo. It ends with key-based SSH, the login mechanism you should be using on any server that faces the network.

What you will learn

  • Describe how accounts, primary groups and supplementary groups are stored and created
  • Read and set file modes in both symbolic and octal form, and predict what umask will produce
  • Explain what setuid, setgid and the sticky bit do and where each is legitimately used
  • Grant an exception to one user or group with a POSIX ACL instead of loosening the mode
  • Delegate administrative commands with sudo rules and log in with an SSH key pair

Permissions are the first thing a Linux system checks and the last thing most people learn properly. The model is small enough to hold in your head — a numeric user id, a list of group ids, and twelve bits per file — but the details bite: why chmod 777 on a web directory is both a security problem and usually not even the fix, why adding yourself to the docker group has no effect until you log out, why a shared folder keeps producing files nobody else can edit.

This guide starts with the accounts themselves, works through the classic rwx bits and the special bits above them, then covers the two mechanisms you reach for when the classic model is too coarse: access control lists and sudo. Examples assume a current Debian, Ubuntu or RHEL-family system. If the ls -l output format is still unfamiliar, read the filesystem guide first.

Who you are to the kernel

The kernel does not know user names. It knows a user id (UID) and a set of group ids (GIDs) attached to every process. Names exist only so humans can read output; the translation happens in user space, through /etc/passwd, /etc/group and whatever directory service the machine is configured to consult.

$ id
uid=1000(alice) gid=1000(alice) groups=1000(alice),27(sudo),110(www-data)

/etc/passwd holds one colon-separated line per account: name, an x placeholder where the hashed password used to sit, UID, primary GID, a free-text comment field, home directory and login shell. It is world-readable because many programs need to map ids to names.

alice:x:1000:1000:Alice Martin:/home/alice:/bin/bash
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin

The actual password hashes live in /etc/shadow, readable only by root, together with ageing information: when the password was last changed, minimum and maximum lifetimes, warning period and expiry date. A ! or * in the hash field means no password can ever match, which is how service accounts are locked. Those accounts also get /usr/sbin/nologin as their shell so that even a successful authentication cannot produce an interactive session.

UIDs below 1000 are reserved for the system on most distributions; regular humans start at 1000. UID 0 is special in the kernel itself — it is the superuser, and most permission checks are simply skipped for it.

Creating and modifying accounts

sudo useradd -m -s /bin/bash -c "Bob Chen" bob   # -m creates and populates /home/bob
sudo passwd bob
sudo usermod -aG www-data,sudo bob               # -a is essential: append, do not replace
sudo usermod -L bob                              # lock the password
sudo userdel -r bob                              # -r also removes the home directory

Debian and Ubuntu also ship adduser, an interactive wrapper that picks sensible defaults and prompts for the password. Whichever you use, the single most important flag is -a on usermod -G: without it the supplementary group list is replaced, and administrators have locked themselves out of sudo this way more than once.

New home directories are populated from /etc/skel, so anything you place there — a default .bashrc, an editor configuration, a .ssh directory with the right mode — is copied for every account created afterwards.

Groups, primary and supplementary

Each account has exactly one primary group, recorded in /etc/passwd and used as the group owner of files it creates. It may additionally belong to any number of supplementary groups, listed in /etc/group.

www-data:x:33:alice,bob
developers:x:1500:alice,bob,carol

Most distributions create a private group per user (alice:alice), so that a default mode of 0644 grants nothing to anyone else even if the file is later moved into a shared area. Shared access is then expressed by creating a purpose-named group and adding members to it.

Because credentials are established at session start, id is the source of truth for what your current processes can do, while /etc/group shows what your next login will get. newgrp developers starts a subshell with the new membership if you cannot log out.

The twelve bits

Every file carries an owner UID, a group GID and a mode: three sets of three bits, plus three special bits on top.

BitOn a regular fileOn a directory
rread the contentslist the names inside
wmodify the contentscreate, rename and delete entries
xexecute it as a programtraverse it, i.e. resolve names through it

The kernel evaluates in a strict order and stops at the first match: if your UID equals the owner, the owner triplet applies and nothing else is consulted; otherwise, if any of your GIDs matches the file's group, the group triplet applies; otherwise the other triplet applies. This is why ----rwxrwx denies the owner everything while allowing everyone else — an ordering that surprises people who assume the sets accumulate.

Two consequences follow from the directory row of the table. First, deleting a file is a write to its directory, not to the file, so you can remove a read-only file from a directory you can write. Second, x without r on a directory is a useful pattern: you can open /srv/data/report.pdf if you know the name, but you cannot enumerate what is there.

Octal modes

Each triplet is a three-bit number: r=4, w=2, x=1. Add them up.

OctalSymbolicTypical use
644rw-r--r--ordinary data and configuration files
600rw-------private keys, credentials, ~/.ssh/id_ed25519
755rwxr-xr-xprograms, scripts and most directories
750rwxr-x---a directory shared with one group only
775rwxrwxr-xa directory a group may write to
700rwx------a private directory such as ~/.ssh

chmod accepts both forms. The symbolic form is relative and often safer, because it changes only the bits you name:

chmod 640 /etc/myapp/secrets.env
chmod u+x deploy.sh          # add execute for the owner only
chmod go-w,o-rx /srv/private # remove group write, and all "other" access
chmod -R u=rwX,g=rX,o= /srv/app   # capital X: execute only where it is already set, or on directories

That capital X is worth remembering: chmod -R 755 marks every data file executable, whereas chmod -R u=rwX,go=rX fixes directories without lying about which files are programs.

Ownership is changed with chown and chgrp, and only root may give a file away:

sudo chown -R www-data:www-data /var/www/example.com
sudo chgrp developers /srv/shared
sudo chown --reference=/etc/nginx/nginx.conf /etc/nginx/conf.d/site.conf

umask: the permissions files are born with

Programs ask for a mode when they create a file — conventionally 0666 for data and 0777 for directories. The umask of the process removes bits from that request before the file is created. It is a mask of bits to clear, not to grant.

With the common default of 022, a new file gets 0666 & ~022 = 0644 and a new directory 0777 & ~022 = 0755. Set umask 027 and the same file becomes 0640 and the directory 0750, invisible to users outside the group. Set umask 077 for a machine where nothing should be shared by default.

$ umask
0022
$ umask 027; touch a; mkdir b; ls -ld a b
-rw-r-----  1 alice alice    0 Sep  7 11:20 a
drwxr-x---  2 alice alice 4096 Sep  7 11:20 b

Because umask is a property of the process, it is inherited by children and must be set where the process starts: in /etc/profile or ~/.profile for login shells, in a UMask= directive for a systemd service, or in the daemon's own configuration. Note that files never receive the execute bit from a 0666 request, which is why a script copied out of an archive so often needs an explicit chmod +x.

setuid, setgid and the sticky bit

Above the nine permission bits sit three more, shown in the x positions of ls -l.

setuid (4000, an s in the owner triplet) makes an executable run with the privileges of its owner rather than the caller. /usr/bin/passwd is the canonical example: an unprivileged user must be able to rewrite one line of /etc/shadow, and setuid-root is how that is arranged. Setuid is powerful and historically the source of many local privilege escalations, so it belongs on distribution binaries and essentially nothing else. Auditing for surprises is a one-liner:

sudo find / -xdev -type f -perm -4000 -printf '%M %u %p\n' 2>/dev/null

setgid (2000) does the same for the group on an executable, but its more useful form is on a directory: files created inside inherit the directory's group instead of the creator's primary group, and new subdirectories inherit the setgid bit too. That is the standard recipe for a shared project area.

sudo install -d -o root -g developers -m 2775 /srv/projects

The sticky bit (1000, a t in the other triplet) restricts deletion in a world-writable directory to the owner of each file. /tmp is drwxrwxrwt for exactly this reason: anyone may create files there, nobody may delete someone else's.

chmod 2775 /srv/projects   # setgid directory
chmod 1777 /srv/scratch    # sticky, world-writable
chmod g+s,o-rwx /srv/team  # symbolic equivalents

Setgid directories fix the group but not the mode, so a member creating a file with umask 022 still produces rw-r--r--, which colleagues cannot edit. Pair the setgid bit with umask 002 for the users involved, or use an ACL, which is the better answer.

ACLs, when three sets are not enough

A POSIX access control list attaches named user and group entries to a file, so you can grant one extra person access without inventing a group or loosening other. The filesystem must be mounted with ACL support, which is the default for ext4 and XFS on current kernels.

$ setfacl -m u:jenkins:rx /srv/app/bin
$ setfacl -m g:auditors:r /var/log/app/audit.log
$ getfacl /srv/app/bin
# file: srv/app/bin
# owner: deploy
# group: deploy
user::rwx
user:jenkins:r-x
group::r-x
mask::r-x
other::---

ls -l marks a file carrying an ACL with a trailing +. The mask entry is a ceiling on every named entry and on the group class; chmod g= adjusts the mask rather than the group entry on an ACL-bearing file, which explains occasional "I granted it and it still does not work" confusion.

Default ACLs on a directory are the inheritance mechanism the classic bits lack. They apply to entries created later, so a deployment user can keep write access to everything a build drops into a tree:

sudo setfacl -R -m u:deploy:rwx -m d:u:deploy:rwx /var/www/example.com
sudo setfacl -x u:jenkins /srv/app/bin     # remove one entry
sudo setfacl -b /srv/app/bin               # remove the whole ACL

Use ACLs deliberately and document them. They are invisible in a plain ls -l beyond that +, and a permission puzzle nobody can explain is worse than a slightly clumsy group.

Delegating privilege with sudo

Modern practice is to disable direct root login and grant specific privileges through sudo. The policy lives in /etc/sudoers and, preferably, in files under /etc/sudoers.d/. Always edit with visudo, which validates the syntax before installing the file.

# /etc/sudoers.d/deploy   (mode 0440, edited with: sudo visudo -f /etc/sudoers.d/deploy)
Cmnd_Alias APPCTL = /usr/bin/systemctl restart app, /usr/bin/systemctl status app

%developers ALL=(ALL) APPCTL
deploy      ALL=(www-data) NOPASSWD: /usr/bin/rsync

Read a rule as who host=(as whom) what. The first line lets members of the developers group restart one service, after entering their own password. The second lets the deploy account run rsync as www-data with no password, which is what an automated pipeline needs.

Two habits keep sudo honest. Grant commands, not shells — %developers ALL=(ALL) NOPASSWD: ALL is root access with extra steps, and any allowed command that can spawn a shell or edit an arbitrary file (vi, less, find, tar) is equivalent to full root. And keep the log: every invocation is recorded by the journal or syslog, which makes journalctl -t sudo a genuinely useful audit trail, as covered in logs and troubleshooting.

Logging in with keys instead of passwords

Password authentication over SSH invites brute-force attempts from the moment a host gets a public address. Key pairs remove the guessable secret entirely.

ssh-keygen -t ed25519 -C "alice@laptop"        # private key stays on your machine
ssh-copy-id [email protected]           # appends the public key remotely
ssh -o PreferredAuthentications=publickey [email protected]

The private key must be mode 600 and the ~/.ssh directory 700; sshd refuses keys that are group- or world-readable, and it refuses authorized_keys if the home directory itself is group-writable. Once every account that needs access has a key, harden the daemon in /etc/ssh/sshd_config.d/:

PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
AllowGroups sshusers

Validate with sudo sshd -t and reload rather than restart, and — this is the part people skip — keep your existing session open while you test a new one from another terminal. A typo in sshd_config on a remote host is a support ticket at best.

Common mistakes

chmod 777 as a debugging step. It grants every user on the box write access and rarely fixes the real problem, which is usually a wrong owner or a missing x on a parent directory. Find the failing path with namei -l /var/www/example.com/index.php.

usermod -G without -a. It silently drops every other supplementary group, including sudo and wheel.

Recursive chmod with a numeric mode. chmod -R 755 makes every configuration file and image executable. Use u=rwX,go=rX, or find … -type d -exec chmod 755 and -type f -exec chmod 644.

Assuming permissions protect a file's history. Tightening a mode does nothing for copies already taken, and a secret committed to Git or written into a container image stays there. Rotate it instead.

Forgetting that root ignores the bits. A read-only mode is not a safeguard against a root process, and neither is it a safeguard inside a container that runs as root with the path bind-mounted.

Group changes without a new session. id reflects the session; check there before assuming the group did not take effect.

Key takeaways

  • The kernel authorises by UID and GID; names come from /etc/passwd and /etc/group, hashes and ageing from /etc/shadow.
  • Permission checks stop at the first matching class — owner, then group, then other — so the triplets never accumulate.
  • On directories x means traverse and w means create or delete, which is why deleting a read-only file is allowed and why a missing x breaks a whole path.
  • umask subtracts bits at creation time; set it in the login profile or in a service unit, not after the fact.
  • setgid on a directory fixes group inheritance, the sticky bit protects world-writable directories, and setuid belongs almost exclusively to distribution binaries.
  • Reach for a POSIX ACL, with a default entry for inheritance, when one user or group needs an exception; reach for sudo rules scoped to specific commands when a human needs privilege.
  • Replace SSH passwords with key pairs, then turn PasswordAuthentication off and verify from a second session before closing the first.

Sources and further reading

Frequently asked questions

Why can I not enter a directory even though its files are readable?
Directories use the execute bit differently: x means the right to traverse the directory, that is, to resolve a name inside it. Without x on every directory along the path, the kernel stops before it ever looks at the file itself. A directory with r but no x lets you list names yet not stat or open anything, which is why 0644 on a directory is almost always a mistake.
I added myself to a group but still get permission denied. What is wrong?
Group membership is copied into the process credentials at login. Your current shell was started before the change, so it still carries the old set. Log out and back in, start a fresh login session, or run newgrp groupname in the shell you are in. Check the effective list with the id command rather than by reading /etc/group, because the kernel uses what your session was given.
Should I edit /etc/sudoers directly?
No. Use visudo, which locks the file and refuses to install a version that fails a syntax check. A malformed sudoers file can lock every administrator out of privilege escalation on that host. Better still, drop a small file into /etc/sudoers.d/ and edit it with visudo -f, so package upgrades never conflict with your rules and each delegation lives in its own file.
What is the difference between su and sudo?
su starts a shell as another user and asks for that user's password, so anyone using it needs the target account's credentials. sudo runs a single command under a policy stored in /etc/sudoers, authenticates with the caller's own password, and logs every invocation. sudo lets you delegate precisely and audit afterwards, which is why most distributions now disable direct root login altogether.