Linux File Permissions, Users and Groups

DevOps and Cloud Engineering: Complete Course from Beginner to Advanced · Linux Fundamentals · 9 min read · Updated

Who can do what

  1. The owner of a file has the first set of rights. With mode 755 the owner gets 7: read, write and execute.
  2. The group gets the middle number. Here it is 5: read and execute, but no write.
  3. Everyone else gets the last number, also 5 here.
  4. Together the three numbers give the mode of the file: 755.

Linux was built for many people to share one machine, so every file has an owner and a set of rules about who may read, change or run it. These rules are the first line of defence on a server. They also cause a large share of the beginner errors you will see, such as Permission denied. If you understand them once, you will fix those errors in seconds, and you will stop reaching for the dangerous shortcut of giving every file full access.

Why do we need it?

A server is shared by many people and programs. Permissions decide who may read, change or run each file, so one mistake or one attacker cannot damage everything. They are also the cause of most Permission denied errors, so understanding them saves a lot of time.

A real-life example

Think of an office building. Everyone has a key card. Staff can open the shared kitchen, only the finance team can open the finance room, and only the manager can open the safe. Linux files work the same way: owner, group and everyone else each get their own set of keys: read, write and execute.

Owner, group and others

Every file has three kinds of people attached to it. The owner is usually the user who created it. The group is a named set of users. Others is everyone else on the system. For each of these three, there are three possible rights.

  • r (read): see the contents of a file, or list the names in a folder.
  • w (write): change a file, or add and remove files inside a folder.
  • x (execute): run a file as a program, or enter a folder with cd.

Run ls -l and you will see a line like -rwxr-xr-- 1 asha devs 120 Mar 1 notes.sh. The first character is the type: a dash for a file, d for a folder. The next nine characters are three groups of three: owner rwx, group r-x, others r--. Then come the owner name, the group name, the size, the date and the file name.

The numbers: 644, 755, 600

Each right has a value: read is 4, write is 2 and execute is 1. Add them for each group. So rwx is 4 + 2 + 1 = 7, r-x is 4 + 1 = 5, r-- is 4, and rw- is 6. A permission of 755 means owner 7, group 5, others 5. You will meet a few numbers again and again.

  • 644 (rw-r--r--): normal files. The owner can edit, everyone can read.
  • 755 (rwxr-xr-x): programs and scripts, and many folders. Everyone can run, only the owner can edit.
  • 600 (rw-------): private files such as SSH keys. Only the owner can use them.
  • 770 (rwxrwx---): a shared folder for a group, with nothing for others.

A real run

This small script creates a few files in a temporary folder, sets different permissions, and prints the result with stat. It was run in the course lab. The script in this lesson was run on Linux in the course lab (bash 5, Python 3.13). The output shown is real. Your numbers (memory, disk, dates) will be different.

#!/usr/bin/env bash
# perm-demo.sh - shows how chmod numbers map to rwx. Runs in a temporary folder.
set -euo pipefail
DIR=$(mktemp -d)
cd "$DIR"
touch notes.txt run.sh secret.key
mkdir shared

chmod 644 notes.txt      # owner read+write, everyone else read
chmod 755 run.sh         # owner all, everyone else read+execute
chmod 600 secret.key     # owner read+write, nobody else
chmod 770 shared         # owner and group all, others nothing

stat -c '%a  %A  %n' notes.txt run.sh secret.key shared
cd /
rm -rf "$DIR"
644  -rw-r--r--  notes.txt
755  -rwxr-xr-x  run.sh
600  -rw-------  secret.key
770  drwxrwx---  shared

Changing permissions and owners

chmod 644 notes.txt        # numbers
chmod u+x script.sh        # add execute for the user (owner)
chmod go-w shared.txt      # remove write for group and others
chmod -R 755 site/         # apply to a folder and all inside it
sudo chown asha:devs report.txt   # change owner and group
sudo chown -R www-data:www-data /var/www/site

Letters work too: u is the user (owner), g the group, o others and a all. A plus adds a right and a minus removes it. A common beginner mistake is chmod 777, which gives everyone full control. It makes the error go away, and leaves the door open. Work out which user needs which right instead. For example, a web server that must read your site files needs read access, not write access.

Users and groups

  • whoami shows your user, id shows your user number and groups.
  • sudo adduser maya creates a user on Debian and Ubuntu (useradd is the lower-level command).
  • sudo usermod -aG docker maya adds Maya to the group called docker. The -a is important: without it you replace her other groups.
  • sudo passwd maya sets a password.
  • groups maya lists her groups, and getent group docker lists the members of a group.
  • sudo deluser maya removes a user.

Users and groups are stored in /etc/passwd and /etc/group. Passwords are not in these files but in /etc/shadow, which only the administrator can read.

sudo and the root user

The root user can do anything, including destroy the system. Instead of logging in as root, administrators use sudo, which runs one command with root power after checking that you are allowed. Permission to use sudo is set in /etc/sudoers, which you edit only with the visudo command, because it checks your syntax before saving. If it is wrong, you could lock yourself out. Follow the rule of least privilege: give people the smallest rights that let them do their job.

Architecture diagram

Here is the idea as a simple picture. You can use the same diagram in your own notes.

flowchart LR
  F[File: report.txt] --> O[Owner: rw-]
  F --> G[Group: r--]
  F --> X[Others: r--]
  O --> M[Mode 644]
  G --> M
  X --> M

How to check it worked

  • You can turn 750 into letters and back without looking it up.
  • You can explain why chmod 777 is a bad fix.
  • You can add a user to a group without removing their other groups.

How companies use it

Web servers read site files but must not be able to change them. Applications run as their own users with the smallest rights they need. SSH keys must be mode 600 or SSH refuses them. Teams use groups so that members of one project can share a folder without sharing it with the whole company.

Common errors and how to fix them

  • Permission denied when running a script. The file lacks the execute right. Fix it with chmod +x script.sh.
  • SSH says the key is too open. Run chmod 600 ~/.ssh/id_ed25519.
  • Fixing everything with chmod 777. This gives everyone full control. Find which user needs which right and grant only that.
  • Group change does not work. A user must log out and back in after being added to a group.
  • The web server cannot read files. Check the owner and the x right on every parent folder.

Best practices

  • Give the least rights that let the task work.
  • Use groups to share, not world-writable folders.
  • Keep private keys at 600.
  • Use sudo for single commands, not a root shell.
  • Do not run applications as root.

Interview questions with simple answers

  1. What do r, w and x mean? Read, write and execute. On a folder, x means you may enter it.
  2. What does 755 mean? Owner can read, write and execute. Group and others can read and execute.
  3. How do you change the owner of a file? chown user:group file, usually with sudo.
  4. What is the difference between a user and a group? A group is a named set of users that share rights.
  5. How do you add a user to a group safely? usermod -aG group user. The -a keeps the other groups.
  6. What is sudo? Runs one command with administrator rights, if you are allowed.
  7. Which file lists users? /etc/passwd. Password hashes are in /etc/shadow.
  8. What is the rule of least privilege? Give only the access needed to do the job.
  9. What does chmod 600 mean? Only the owner can read and write.
  10. Why is root login over SSH discouraged? Root can do anything, so attackers target it. Use a normal user plus sudo.

Practical exercises

Try each one yourself before you look at the hint. Struggling for a few minutes is how the skill sticks.

  1. Create a file and set it to 640. Predict who can do what, then check with stat. Hint: Owner 6, group 4, others 0.
  2. Make a script executable and run it. Hint: chmod +x then ./script.sh.
  3. Create a group team and a shared folder for it with mode 770. Hint: groupadd, chgrp, chmod.
  4. Try to read a private file as another user and write down the exact error. Hint: su - otheruser or sudo -u.
  5. Convert 750, 640 and 700 into letters. Hint: Add 4, 2, 1 for each digit.

Mini project: Shared project folder

On a practice machine create two users and one group. Make a folder that the group can write to but others cannot even list. Prove it by creating a file as each user, and write a short README saying which commands you used.

Knowledge test

Answer the ten questions in the quiz at the end of this lesson. You pass with 60 percent. If an answer is wrong, read the explanation, then reread the part of the lesson it points to.

Frequently asked questions

What does Permission denied mean?

The user running the command does not have the needed right (read, write or execute) on that file or folder, or on a folder in its path. Check with ls -l and id.

Why does a folder need the x permission?

For a folder, x means you may enter it and reach the files inside. Without x on a folder you cannot cd into it, even if you can list its name.

Should I run everything with sudo?

No. Use sudo only for the commands that need it. Running programs as root makes mistakes and attacks much more damaging.

Practise with interview questions