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
whoamishows your user,idshows your user number and groups.sudo adduser mayacreates a user on Debian and Ubuntu (useraddis the lower-level command).sudo usermod -aG docker mayaadds Maya to the group called docker. The-ais important: without it you replace her other groups.sudo passwd mayasets a password.groups mayalists her groups, andgetent group dockerlists the members of a group.sudo deluser mayaremoves 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
- What do r, w and x mean? Read, write and execute. On a folder, x means you may enter it.
- What does 755 mean? Owner can read, write and execute. Group and others can read and execute.
- How do you change the owner of a file?
chown user:group file, usually with sudo. - What is the difference between a user and a group? A group is a named set of users that share rights.
- How do you add a user to a group safely?
usermod -aG group user. The -a keeps the other groups. - What is sudo? Runs one command with administrator rights, if you are allowed.
- Which file lists users?
/etc/passwd. Password hashes are in/etc/shadow. - What is the rule of least privilege? Give only the access needed to do the job.
- What does chmod 600 mean? Only the owner can read and write.
- 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.
- Create a file and set it to 640. Predict who can do what, then check with
stat. Hint: Owner 6, group 4, others 0. - Make a script executable and run it. Hint:
chmod +xthen./script.sh. - Create a group
teamand a shared folder for it with mode 770. Hint:groupadd,chgrp,chmod. - Try to read a private file as another user and write down the exact error. Hint:
su - otheruserorsudo -u. - 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.