Lesson 3 of 8 in Linux and the shell
Users, groups, permissions and sudo
Every file and process on Linux belongs to a user and a group, and every file carries read, write and execute bits for its owner, its group and everyone else. sudo runs one command with administrator rights after checking that you are allowed to.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: The Linux directory tree and where things live
The problem this solves
A server runs many things at once: your shell, a web server, a database, a backup job. If any of them could read or change anything, one bug or one stolen password would expose the whole machine.
Linux limits damage by giving each process an identity and checking it against the owner, group and permission bits of every file it touches. The web server can read its own config and cannot read your SSH keys.
Most beginner errors on a server, from Permission denied to a service that cannot write its logs, come back to this check. Once you can read it, you can fix them without opening everything up.
How it works
- Runs code
- Security
- Other
When a process opens a file the kernel compares the process's user and groups with the file and applies the first matching set of permission bits.
- Process to Kernel check: 1 opens a file
- Kernel check to Owner bits: 2 owner? use these
- Kernel check to Group bits: 3 else in group? use these
- Kernel check to Other bits: 4 else everyone else
- People and clients
- Security
- Data
- Runs code
You run a command through sudo, sudo checks the sudoers rules, and if you are allowed it runs the command as root.
- You to sudo: 1 run with sudo
- sudo to /etc/sudoers: 2 check the rules
- sudo to Command as root: 3 allowed: run it
Each file has one owner and one group. ls -l shows ten characters at the start of each line, for example -rwxr-x---. The first says what kind of thing it is, and the next nine are three groups of read, write and execute for the owner, the group and everyone else.
The kernel picks one set, not all three. If you are the owner it uses the owner bits, even when the group bits would allow more. If you are not the owner but are in the group, it uses the group bits. Everyone else gets the last three.
On a directory, read means you can list its names, write means you can add or remove entries, and execute means you can pass through it to reach what is inside. A directory without execute blocks access to everything below it.
Permissions are also written as octal numbers, where read is 4, write is 2 and execute is 1. So 640 means the owner can read and write, the group can read, and others get nothing.
Root, user ID 0, skips these checks. You rarely log in as root. sudo runs a single command as root after checking the rules in /etc/sudoers, which also leaves a log entry of who did it.
Try it yourself
These steps run in a throwaway folder in your home directory. Run them as a normal user, not as root, because root ignores the checks you are trying to see.
- 1See who you are
id whoami
idlists your user ID, your main group and every group you belong to. - 2Create a file and read its permissions
mkdir -p ~/learn-linux && cd ~/learn-linux touch report.txt ls -l report.txt
The left column is the permission string. Next come the link count, the owner, the group, the size and the date.
- 3Change the permissions with numbers
chmod 640 report.txt stat -c '%A %a %U:%G %n' report.txt
You should see
-rw-r----- 640 <you>:<group> report.txt
<you>and<group>will show your own names.stat -cprints the permission string and the octal form together so you can compare them. - 4Make a script executable
printf '#!/bin/sh\necho hello\n' > hi.sh ./hi.sh chmod u+x hi.sh ./hi.sh
The first
./hi.shfails with a permission error because new files are not executable. Afterchmod u+xit prints hello.u+xmeans add execute for the owner. - 5See what execute means on a directory
mkdir d && touch d/f chmod -x d ls d/f chmod +x d ls d/f
With execute removed,
ls d/ffails with Permission denied even though the file itself is fine. Restoring it fixes that. - 6Use sudo and then clean up
sudo whoami cd ~ && rm -r ~/learn-linux
You should see
root
sudo whoamiasks for your password and prints root. The last line removes the practice folder.
What breaks, and where to look
bash: ./deploy.sh: Permission denied
- WHY IT HAPPENS
- The file is not marked executable, or one of the directories on the way to it blocks you. The message looks the same for both.
- CHECK FIRST
- Run
ls -l deploy.shand look for anx. If it is there, runnamei -l $(pwd)/deploy.shto see the permissions of every folder on the path. Fix withchmod u+x deploy.sh.
sudo echo "x" > /etc/myapp.conf fails with: bash: /etc/myapp.conf: Permission denied
- WHY IT HAPPENS
- The redirect is done by your own shell before sudo starts, so the file is opened without root rights. Only the
echoran as root. - CHECK FIRST
- Let a root program do the writing, for example
echo "x" | sudo tee /etc/myapp.conf. Add-atoteeto append instead of replace.
You added yourself to a group such as
docker, but commands still say permission denied.- WHY IT HAPPENS
- A process receives its groups when you log in. Your current session still has the old list.
- CHECK FIRST
- Compare
idwithgetent group docker. Log out and back in, or start a shell with the new group usingnewgrp docker.
When not to use this
- Do not fix a permission error with
chmod 777orchmod -R 777. It makes the file writable by every account on the machine. Find which user the program runs as and change the owner or the group instead. - Basic bits cannot express rules such as two different groups with different access. Access control lists, set with
setfacl, handle that, but use them only when plain permissions are not enough.
Check yourself
A file shows -rw-r-----. Who can read it and who can write it?
The owner can read and write it, the group can only read it, and everyone else has no access.
Why can a directory without execute permission block you from a file inside it?
Execute on a directory lets you pass through it to reach what is inside, so without it the path cannot be followed.
Why does sudo echo hi > /etc/x fail?
Your shell opens the file for the redirect before sudo runs, so the write happens without root rights. Use | sudo tee instead.
Keep going
Lesson 10 of 92 in the DevOps path