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 aspostgres, 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
sudoin 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.
whoamiidOn 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
dockergroup lets you run Docker without sudo. - Being in the
sudogroup 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-rw-r--r-- 1 lynuxpro lynuxpro 153 Nov 28 09:42 README.txtSplit the first ten characters like this:
- rw- r-- r--type owner group others| Letter | On a file | On a folder |
|---|---|---|
r read | Read what is inside | See the names inside (list it) |
w write | Change what is inside | Add, delete and rename files inside |
x execute | Run it as a program | Go 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.
| Number | Letters | How it adds up | Typical use |
|---|---|---|---|
| 7 | rwx | 4+2+1 | Owner of a script or folder |
| 6 | rw- | 4+2 | Owner of a normal file |
| 5 | r-x | 4+1 | Others who may run or enter |
| 4 | r-- | 4 | Read only |
| 0 | --- | 0 | No access |
Full modes you should know on sight:
| Mode | Letters | Used for |
|---|---|---|
644 | rw-r--r-- | Normal files and configs |
755 | rwxr-xr-x | Scripts, programs and folders |
600 | rw------- | Private files: SSH keys, secrets |
700 | rwx------ | Private folders like ~/.ssh |
750 | rwxr-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 -lbash: ./deploy.sh: Permission denieddeploying...chmod +x fileadds "execute" for everyone.chmod 755 filesets the exact mode.chmod -R 755 foldersets 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/appchown 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)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| Command | What it does |
|---|---|
useradd -m name | Create a user with a home folder (-m = make home) |
passwd name | Set a user's password |
usermod -aG docker name | Add a user to a group. -a (append) is vital: without it, the user is removed from all their other groups |
groupadd devs | Create a group |
userdel -r name | Delete a user and their home folder |
Group changes take effect at the user's next login.
Special permissions (just recognise them)
| Mark | Name | What it does |
|---|---|---|
s where the owner's x is | setuid | The program runs as its owner. For example, passwd runs as root so it can update the password file |
s where the group's x is | setgid | New files in this folder get the folder's group |
t where the others' x is | sticky bit | In 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 /optwhoamiThe 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/appCommon mistakes
chmod 777"to make it work". It lets every user and program change the file. Never do this on a server.usermod -Gwithout-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
xon folders. You needxon a folder tocdinto it, even if the files inside are readable.
Practice
- Make
deploy.shwith an echo command in it, try to run it, then fix it withchmod +x. - Make
notes.txtreadable and writable only by you. Check withls -l. - Turn these into numbers:
rwxr-x---,rw-r-----,rwx------. - Explain why
~/.ssh/id_ed25519should be600and~/.sshshould be700.
Cheat sheet
| Command | What it does |
|---|---|
id | Show your user, groups and IDs |
ls -l / ls -ld dir | Show permissions of files / of the folder itself |
chmod 644 f / chmod +x f | Set the mode / make it runnable |
chown user:group f | Change the owner |
sudo cmd | Run one command as root |
usermod -aG group user | Add 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.
chmodchanges the rights andchownchanges the owner.- Fix "Permission denied" by giving the right user access. Never use
chmod 777.