Skip to content
$ lynuxpro
← Linux

LESSON 6 OF 26 · BEGINNER · 19 MIN

Users, groups and permissions

In one minute

  • Linux decides who can read, change or run every file. It does this with users, groups and permissions.
  • "Permission denied" is one of the most common errors in DevOps. A deploy script cannot write to a folder, a container cannot read a secret, a web server cannot open its own config. All of these come down to permissions.
  • Every file has an owner (a user) and a group. It also has three sets of rights, for the owner, the group and everyone else. Each set says whether that person can read, write or execute (run) the file.
  • 👉 Any time you see "Permission denied", deploy an app, set up SSH keys or store a secret.

Think of it like: an office building. Each room (file) has an owner, a team (group) that shares it, and visitors (others). The door sign says who can look in, who can change things, and who can use the room.

Users and groups

  • Every running program runs as a user. nginx often runs as www-data, a database as postgres, and your shell as you.
  • Users belong to groups. Give a group access to a folder, and every member gets that access.
  • root (user ID 0) is the admin and can do anything. Normal users type sudo in front of a command to run just that command as root.

Running each app as its own user keeps damage small. If the web server is hacked, the attacker only gets what www-data can touch, not the whole server.

whoamiid
SHELLbashutf-8 · 2L

On a real server, id prints your user ID, group ID and groups, for example uid=1000(lynuxpro) gid=1000(lynuxpro) groups=1000(lynuxpro),27(sudo),999(docker).

  • Being in the docker group lets you run Docker without sudo.
  • Being in the sudo group lets you become root.

User accounts are listed in /etc/passwd, and groups in /etc/group.

Reading permissions

The first ten characters of ls -l show a file's type and permissions.

👉 The first thing to check whenever you see "Permission denied".

ls -l
SHELLbashutf-8 · 1L
-rw-r--r-- 1 lynuxpro lynuxpro 153 Nov 28 09:42 README.txt
OUTPUTterminalutf-8 · 1L

Split the first ten characters like this:

-    rw-    r--    r--type owner  group  others
OUTPUTterminalutf-8 · 2L
LetterOn a fileOn a folder
r readRead what is insideSee the names inside (list it)
w writeChange what is insideAdd, delete and rename files inside
x executeRun it as a programGo into it with cd and reach the files inside

So -rw-r--r-- means: the owner can read and write, everyone else can only read. A script needs x before you can run it with ./script.sh.

Remember it: Read the three groups as U-G-O: User (owner), Group, Others.

Permissions as numbers

Each right has a number: r = 4, w = 2, x = 1. Add them up for each of the three sets.

It is shorter. chmod 644 file sets all nine rights in one go.

NumberLettersHow it adds upTypical use
7rwx4+2+1Owner of a script or folder
6rw-4+2Owner of a normal file
5r-x4+1Others who may run or enter
4r--4Read only
0---0No access

Full modes you should know on sight:

ModeLettersUsed for
644rw-r--r--Normal files and configs
755rwxr-xr-xScripts, programs and folders
600rw-------Private files: SSH keys, secrets
700rwx------Private folders like ~/.ssh
750rwxr-x---Scripts that only the owner's group may run

Remember it: 644 = "files", 755 = "folders and scripts", 600 = "secrets".

Changing permissions: chmod

chmod changes a file's permissions (change mode).

👉 Making a script runnable, or locking down a private file.

echo 'echo "deploying..."' > deploy.sh./deploy.shchmod +x deploy.sh./deploy.shchmod 600 notes.txtls -l
SHELLbashutf-8 · 6L
bash: ./deploy.sh: Permission denieddeploying...
OUTPUTterminalutf-8 · 2L
  • chmod +x file adds "execute" for everyone.
  • chmod 755 file sets the exact mode.
  • chmod -R 755 folder sets it for everything inside. Be careful: this makes every file runnable too.

Changing ownership: chown

chown changes who owns a file (change owner).

👉 Deploying an app that runs as its own user. The app's user must own its folder.

sudo chown app:app /opt/app
SHELLbashutf-8 · 1L

chown user:group path sets the owner and group. chown -R does it for everything inside.

sudo: borrowing root for one command

sudo runs one command as root (or as another user).

Safer than logging in as root all day. You only use admin power when you need it, and every sudo command is logged.

sudo apt update          # run one command as rootsudo -u postgres psql    # run a command as another usersudo -i                  # open a root shell (use rarely)
OUTPUTterminalutf-8 · 3L

sudo asks for your password, not root's. Who may use sudo is set in /etc/sudoers. Always edit that file with visudo, which checks for mistakes before saving. A broken sudoers file can lock everyone out of admin access.

Managing users and groups

👉 Adding a teammate, or creating a dedicated user for a service such as deploy or app.

sudo useradd -m deploy
SHELLbashutf-8 · 1L
CommandWhat it does
useradd -m nameCreate a user with a home folder (-m = make home)
passwd nameSet a user's password
usermod -aG docker nameAdd a user to a group. -a (append) is vital: without it, the user is removed from all their other groups
groupadd devsCreate a group
userdel -r nameDelete a user and their home folder

Group changes take effect at the user's next login.

Special permissions (just recognise them)

MarkNameWhat it does
s where the owner's x issetuidThe program runs as its owner. For example, passwd runs as root so it can update the password file
s where the group's x issetgidNew files in this folder get the folder's group
t where the others' x issticky bitIn a shared folder like /tmp, people can only delete their own files

Real-world scenario: a deploy fails with "Permission denied"

The CI job copies files to /opt/app as the user deploy, and it fails. You check:

ls -ld /optwhoami
SHELLbashutf-8 · 2L

The folder is owned by root with mode 755, so only root can write there.

  • Wrong fix: chmod 777. Now anyone on the server can change your app.
  • Right fix: give the folder to the service's own user:
sudo chown -R deploy:deploy /opt/app
OUTPUTterminalutf-8 · 1L

Common mistakes

  • chmod 777 "to make it work". It lets every user and program change the file. Never do this on a server.
  • usermod -G without -a. It removes the user from every other group, including sudo.
  • SSH key permissions too open. SSH refuses to use a private key that others can read. It must be 600.
  • Forgetting x on folders. You need x on a folder to cd into it, even if the files inside are readable.

Practice

  1. Make deploy.sh with an echo command in it, try to run it, then fix it with chmod +x.
  2. Make notes.txt readable and writable only by you. Check with ls -l.
  3. Turn these into numbers: rwxr-x---, rw-r-----, rwx------.
  4. Explain why ~/.ssh/id_ed25519 should be 600 and ~/.ssh should be 700.

Cheat sheet

CommandWhat it does
idShow your user, groups and IDs
ls -l / ls -ld dirShow permissions of files / of the folder itself
chmod 644 f / chmod +x fSet the mode / make it runnable
chown user:group fChange the owner
sudo cmdRun one command as root
usermod -aG group userAdd a user to a group

Key takeaways

  • Every file has an owner, a group, and rights for everyone else.
  • The rights are read (4), write (2) and run (1). Add them up to get numbers like 644.
  • Use 644 for files, 755 for folders and scripts, and 600 for secrets.
  • chmod changes the rights and chown changes the owner.
  • Fix "Permission denied" by giving the right user access. Never use chmod 777.