17 min read
Linux file permissions: chmod, chown and sudo
In one sentence: On Linux, every file has an owner, a group and three sets of permissions, rwx: what its owner, its group and everyone else can read, change or run. root bypasses every permission, and that's why it's used as little as possible.
What you will learn
- Create your practice server, a virtual machine running Ubuntu.
- Find out who you are with whoami and id, and read the permissions ls -l shows.
- Change permissions with chmod, with letters and with numbers, and a file's owner with chown.
- Use sudo knowing what it does, and explain why each server runs as its own user and not as root.
Before you start, read: 03-pipes-and-redirection.md
The problem
Section titled “The problem”Imagine a friend’s online shop has a bug in the form where customers upload their photo. Someone discovers it and manages to make the server run their commands. What can they do from there? It depends on one thing only: which user the server program runs as.
If it runs as the administrator, the attacker can do everything: read the passwords, copy the database, delete it and install whatever they want. If it runs as a user of its own that can only touch its folder, the damage stays in that folder.
In your day-to-day work you’ll see it differently: a Permission denied when you try to read a file or start a program. The temptation is to fix it with sudo or with chmod 777, and that opens exactly the holes from the first paragraph. Here you’ll learn to read permissions and to grant them carefully, on your own practice server.
The analogy
Section titled “The analogy”The analogy
Imagine an office building. Each person has a badge with their name, and the badge also says which departments they belong to. Each office has a sign on the door with three lines: what its owner, its department and anyone else can do: read the papers, change them or use what’s inside. To reach an office, you go through hallways with doors, and each door has its own sign.
The building manager has a master key that opens everything. If you need something your badge doesn’t open, you ask him for it just for that one thing, and he writes down in a logbook who asked for it and what for.
The cleaning company has its own badge, which only opens the rooms it cleans. If someone steals it, they don’t get into the safe.
On Linux, the badges are users; the departments, groups; the signs, permissions; and the building manager, root.
Where the analogy breaks down
- The sign only has three lines: the owner, one group and everyone else. You can’t write “Luis yes, Marta no.”
- Your badge travels with what you do. Every program you start carries a copy, and it can do everything you can. In a building, the courier you send on an errand doesn’t carry your badge.
- The master key doesn’t just open doors: root can change the signs, make new badges or tear down the building, and nobody asks it “are you sure?”
How it really works
Section titled “How it really works”The examples in this section are from an Ubuntu server like the one you’ll create in “Try it.” If you want to try them while you read, create it first.
Every person and every program is a user
Section titled “Every person and every program is a user”On Linux, everything that happens is done by a User (in Linux)An identity on the system, with a name and a number. Each person has their own, and each program on a server usually has another of its own. Every file has a user as its owner, and every process runs on behalf of one.Go to definition: every ProcessA running program. When you start a server, the operating system creates a process that stays alive waiting for requests.Go to definition runs on behalf of one, and every file has one as its owner. Two commands tell you who you are:
$ whoamiubuntu$ iduid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev)whoami gives your name. id gives more details:
uid=1000(ubuntu): your user number. To the system, a user is a number; the name is for people.gid=1000(ubuntu): your primary group. On Ubuntu, every user has one with the same name.groups=…: all your Group (in Linux)A set of users with a name, to give them all permissions at once. Every file belongs to a group, and a user can be in several.Go to definition. A group brings users together so they can be given permissions all at once. The one that matters here issudo: whoever is in it can act as the administrator.
The list of users is in a text file, /etc/passwd. Each line is one user, with their details separated by ::
$ grep www-data /etc/passwdwww-data:x:33:33:www-data:/var/www:/usr/sbin/nologinThey are the name, an x (the password is stored in another file, /etc/shadow), the user number, the group number, a description, the home folder and the ShellThe program that reads what you type in the terminal, interprets it and runs it. The most used are bash, the one on almost every Linux, and zsh, the one on macOS.Go to definition. www-data isn’t a person: it’s the user for web servers, and its shell, nologin, doesn’t let anyone log in as it.
The ten letters of ls -l
Section titled “The ten letters of ls -l”In lesson 1 you saw letters like drwxr-xr-x at the start of ls -l. They’re the PermissionsWhat each user can do with a file or a folder. In Linux they are three letters, rwx (read, write and execute), for three cases, the owner, its group and everyone else, as in “rw-r--r--”.Go to definition. This is ubuntu’s home folder, with a file and a folder:
$ ls -ltotal 8-rw-rw-r-- 1 ubuntu ubuntu 6 Oct 12 13:29 notes.txtdrwxrwxr-x 2 ubuntu ubuntu 4096 Oct 12 13:29 projectThe ten letters read like this, with notes.txt:
- |
rw- |
rw- |
r-- |
|---|---|---|---|
Type: - is a file; d, a folder (directory) |
The owner (user, u) |
The group (g) |
Everyone else (others, o) |
After them come the owner (ubuntu) and the group (ubuntu). Each set of three letters says the same thing, always in the same order:
r(read): read.w(write): write, that is, change.x(execute): run, if it’s a program.
A dash means “no.” So notes.txt can be read and changed by its owner and its group, and everyone else can only read it.
What r, w and x mean on a folder
Section titled “What r, w and x mean on a folder”On a folder, the same letters mean something else:
| Letter | On a file | On a folder |
|---|---|---|
r |
Read its contents | See the list of what’s inside (ls) |
w |
Change its contents | Create, rename and delete files inside |
x |
Run it as a program | Go in (cd) and reach what’s inside |
A folder’s x is the hallway door: without it you can’t reach any file inside, even if the file lets you read it. And it’s the folder’s w, not the file’s, that decides whether you can delete it.
How Linux decides whether you can
Section titled “How Linux decides whether you can”When a program opens a file, the system asks, in order:
- Are you root? You can do almost anything; permissions don’t count.
- Are you the owner? The first three letters are used, and nothing else.
- Are you in the file’s group? The middle three are used.
- If not, the last three.
Look at /etc/shadow, the password file:
$ ls -l /etc/passwd /etc/shadow-rw-r--r-- 1 root root 888 Sep 17 02:17 /etc/passwd-rw-r----- 1 root shadow 502 Sep 17 02:17 /etc/shadow$ cat /etc/shadowcat: /etc/shadow: Permission deniedubuntu isn’t the owner (root) and isn’t in the shadow group, so it gets the last three letters: ---. /etc/passwd, on the other hand, ends in r--: anyone can read it.
Changing permissions with chmod: letters or numbers
Section titled “Changing permissions with chmod: letters or numbers”Imagine you download a script, hello.sh: a file with commands for the shell (you’ll write some in lesson 8). You try to run it:
$ ./hello.sh-bash: ./hello.sh: Permission denied$ ls -l hello.sh-rw-rw-r-- 1 ubuntu ubuntu 42 Oct 12 13:29 hello.sh$ chmod u+x hello.sh$ ls -l hello.sh-rwxrw-r-- 1 ubuntu ubuntu 42 Oct 12 13:29 hello.sh$ ./hello.shHello from courseWithout x, it couldn’t be run. chmod (change mode) changes the permissions, and u+x means “add x for the owner.” Only a file’s owner, or root, can change them.
With letters you say who, which operation and which permission:
- Who:
u(the owner),g(the group),o(everyone else) ora(all). - The operation:
+adds,-removes and=sets exactly those. - Examples:
chmod o-r notes.txttakes reading away from everyone else;chmod go= secret.txttakes everything away from the group and everyone else.
With numbers, each permission is worth an amount: r = 4, w = 2 and x = 1. You add up the ones in each set of three, and you get three digits: the owner, the group and everyone else. rw- is 4 + 2 = 6, and r-x is 4 + 1 = 5:
$ chmod 600 notes.txt$ ls -l notes.txt-rw------- 1 ubuntu ubuntu 6 Oct 12 13:29 notes.txt$ chmod 755 hello.sh$ ls -l hello.sh-rwxr-xr-x 1 ubuntu ubuntu 42 Oct 12 13:29 hello.shWith numbers you set all nine permissions; with letters, only the ones you name. These are the most common:
| Number | Letters | What for |
|---|---|---|
644 |
rw-r--r-- |
A normal file: you change it, everyone reads it |
600 |
rw------- |
A secret, like a password or a key: only you |
755 |
rwxr-xr-x |
A program or a folder: everyone goes in or runs it, only you change it |
700 |
rwx------ |
A private folder: only you go in |
777 |
rwxrwxrwx |
Everyone can do everything. Almost never a good idea |
Changing the owner with chown
Section titled “Changing the owner with chown”chown (change owner) gives a file to another user. Imagine the shop’s website, which runs as www-data, has to save photos in the uploads folder:
$ chown www-data uploadschown: changing ownership of 'uploads': Operation not permitted$ sudo chown www-data uploads$ ls -ld uploadsdrwxrwxr-x 2 www-data ubuntu 4096 Oct 12 13:29 uploadsOnly root can give files away; otherwise, anyone could put their own files in someone else’s name. That’s why you need sudo, which you’ll see next. To change the group too: sudo chown www-data:www-data uploads. (-d makes ls show the folder itself and not what’s inside it.)
root, sudo and least privilege
Section titled “root, sudo and least privilege”rootThe Linux administrator, user number 0. Permissions don’t apply to it and it can do anything, so it’s used as little as possible.Go to definition is the Linux administrator: user number 0, the building manager from the analogy. Permissions don’t affect it: it can do everything, including making mistakes with nothing to stop it.
That’s why nobody works as root. You work as a normal user, and when you need something only the administrator can do, you ask for it for one single command with sudoA command that runs another command as root, only that one. It usually asks for your password and records each use in a log.Go to definition (from superuser do):
$ whoamiubuntu$ sudo whoamiroot$ sudo head -1 /etc/shadowroot:*:20713:0:99999:7:::sudo runs the command that follows it as root, and only that one. On Ubuntu, users in the sudo group can use it. It normally asks for your password, and it records every use in a log: the building manager’s logbook.
Behind all this there’s a rule, Least privilegeThe rule of giving each user and each program only the permissions it needs, and no more. If something fails or someone gets in where they shouldn’t, the damage stays within the little it could touch.Go to definition: every user and every program has only the permissions it needs, and no more. If one day something fails or someone gets in where they shouldn’t, the damage stays within the little that user could touch.
Why each server has its own user
Section titled “Why each server has its own user”Because of that rule, each program on a server has its own user: www-data for the web server, postgres for the database (Phase 5). That way, if someone tricks the web server, as in “The problem,” they can’t read the database’s files.
But there’s a catch. In Phase 0 you saw that the web uses PortA number, from 0 to 65535, that says which program on a computer a connection is for. The computer has an address; each listening program has its port.Go to definition 80. On Linux, ports 0 to 1023 can only be opened by root:
$ nc -l 80nc: Permission deniedWith 8080 there would be no problem. Web servers solve it in two steps: they start as root, open port 80 and hand the work over to processes that run as their own user. This is what nginx looks like, the web server you’ll install in lesson 6:
$ ps -o user,pid,cmd -C nginxUSER PID CMDroot 2887 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;www-data 2888 nginx: worker processThe root process only supervises; the one that handles the requests is the www-data one. In Phase 8 you’ll do something similar with your own program: it will listen on 3000 as its own user, and nginx, on 80, will pass the requests on to it.
Try it
Section titled “Try it”The goal: create a teammate, Luis, on your practice server, and find out what you can see of his and what you can’t. First, the server.
First, your practice server
Section titled “First, your practice server”Your practice server is a Virtual machineA computer simulated inside another, with its own operating system, its own users and its own files. What happens inside doesn’t touch the computer outside.Go to definition: a computer simulated inside yours. Whatever you break there doesn’t touch your computer. You’ll create it with Multipass, from Canonical, the company that makes Ubuntu; the first time takes a few minutes, because it downloads Ubuntu.
Installing Multipass is the only thing in the course that asks for administrator permissions on your computer.
With Homebrew, a program for installing programs from the terminal:
brew install --cask multipassIt will ask for your Mac’s password. If you don’t have Homebrew, download the installer from the Multipass website.
sudo snap install multipasssudo will ask for your password: you’ll see what it does in this very lesson.
Don’t install anything: your WSL Ubuntu is your practice server. Open it from the Start menu and skip to exercise 3. sudo will ask for the password you chose when you installed WSL.
1. Create the virtual machine
multipass launch 24.04 --name courseWhat you will see:
Launched: courselaunch creates and starts a machine with Ubuntu 24.04 (so you see the same as in this lesson) and names it course. Before it finishes, you’ll see progress messages. If it fails, the Multipass documentation explains the most common problems.
2. Go into it
multipass shell courseYou’ll see a welcome message from Ubuntu and a new prompt: ubuntu@course:~$. Now you’re the user ubuntu on the machine course, and everything that follows is typed there.
exit takes you back to your computer, with the machine still running. multipass stop course shuts it down, and multipass shell course takes you back (and starts it if needed). You’ll also use it in lessons 6 and 9.
Luis and his files
Section titled “Luis and his files”3. Who are you here?
whoamiidsudo whoamiWhat you will see:
ubuntuuid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev)rootNotice the 27(sudo): that’s why the third command works. In the virtual machine, sudo doesn’t ask for a password, because Multipass sets it up that way; on WSL and on many servers, it does. Your list of groups may be different: what matters is that sudo is in it. On WSL you’ll see your user instead of ubuntu.
4. Create Luis
sudo useradd -m -s /bin/bash luisid luisls -l /homeWhat you will see:
uid=1001(luis) gid=1001(luis) groups=1001(luis)total 8drwxr-x--- 2 luis luis 4096 Oct 12 13:29 luisdrwxr-x--- 2 ubuntu ubuntu 4096 Oct 12 13:29 ubuntuCreating users is root’s job, so it goes with sudo. useradd creates luis, without printing anything; -m creates his folder, /home/luis, and -s /bin/bash gives him bash as his shell.
luis has number 1001 and a group of his own, without sudo. Look at his folder: drwxr-x---, nothing for everyone else. Your dates will be different, and on WSL the other folder will be yours.
If you get useradd: user 'luis' already exists, you created him earlier: move on to the next step.
5. Become Luis
sudo -iu luissudo -u luis runs a command as luis instead of as root, and -i opens a whole session, as if you logged in as him. It doesn’t print anything, but the prompt changes to luis@course:~$.
6. Luis saves two files
echo "We launch the shop on Monday" > plan.txtecho "Today I broke the website by accident" > diary.txtls -lWhat you will see:
total 8-rw-rw-r-- 1 luis luis 38 Oct 12 13:29 diary.txt-rw-rw-r-- 1 luis luis 29 Oct 12 13:29 plan.txt> saves what echo writes into a file, as in lesson 3. Both files end in r--: everyone else can read them. Will you be able to? Type exit to go back to being ubuntu.
7. Try to read Luis's plan
ls /home/luiscat /home/luis/plan.txtWhat you will see:
ls: cannot open directory '/home/luis': Permission deniedcat: /home/luis/plan.txt: Permission deniedBoth fail, even though plan.txt lets everyone else read it: the problem is the folder. /home/luis is drwxr-x---, with no r (see the list) and no x (reach what’s inside) for everyone else. The hallway door is locked.
If cat shows you the plan, check with whoami that you’ve gone back to being ubuntu.
8. Now, with sudo
sudo cat /home/luis/plan.txtWhat you will see:
We launch the shop on MondayIt works: root doesn’t look at permissions. That’s why sudo is so useful and so dangerous: whoever has it can read everyone’s files.
Now, the challenge. Luis wants you to read plan.txt without sudo. But he doesn’t want you to read his diary, or to see what else he has in his folder.
Challenge: which two chmod commands does Luis have to run? Run them as luis and check as ubuntu.Show answer
As luis (sudo -iu luis):
chmod o+x ~chmod 600 diary.txtexitHis folder ends up as drwxr-x--x, and the diary as -rw-------. Now as ubuntu:
$ cat /home/luis/plan.txtWe launch the shop on Monday$ cat /home/luis/diary.txtcat: /home/luis/diary.txt: Permission denied$ ls /home/luisls: cannot open directory '/home/luis': Permission deniedThe x on the folder lets you reach a file if you know its name, and plan.txt lets you read it. Without the r, you can’t see the list. And the diary, with 600, can only be read by Luis.
To finish, delete Luis and his folder:
9. Clean up
sudo userdel -r luisWhat you will see:
userdel: luis mail spool (/var/mail/luis) not founduserdel deletes the user, and -r, his folder. The warning says Luis had no mailbox: nothing to worry about. Check with ls -l /home that only ubuntu is left.
You have already seen it
Section titled “You have already seen it”- On your Mac. The
ana staffinls -lin lesson 1 are your user and your group. In the Finder, “Get Info” (Cmd + I) ends with “Sharing & Permissions”: the same three lines, for you,staffand everyone else. - When you install something. Your Mac and Windows ask for the administrator password. That’s the idea behind
sudo: you work as a normal user and ask for permission only to touch the system. - On your phone. Each app asks for permission to use the camera or your location, and it only has what you give it: least privilege.
And if you code:
npm install -gsometimes answersEACCES: permission denied: it wants to write to a folder that belongs to root.sudo“fixes” it and leaves root-owned files on your computer; a version manager like nvm installs Node in your folder and avoids it.- Git stores each file’s
x. After achmod +x,git diffshowsold mode 100644andnew mode 100755: the permissions as numbers, after a100that means “regular file.”
Common mistakes
Section titled “Common mistakes”chmod 777to get rid of aPermission denied. It works, and it lets anyone change the file, including an attacker who gets in as the website’s user. Check who the owner is and who needs it, and grant only that.sudofor everything. What you create withsudobelongs to root, and afterwards you can’t change it withoutsudo. Use it only on the command that needs it.sudowith>.sudo echo "hello" > /etc/greeting.txtanswers-bash: /etc/greeting.txt: Permission denied. The redirection is done by your shell, which isn’t root, beforesudostarts (you saw this in lesson 3). Useecho "hello" | sudo tee /etc/greeting.txt:teecopies whatever reaches it into a file, and here it does so as root.- A folder without
x.chmod -R 644(-Rapplies it to everything inside) takes thexaway from the folders, and then nobody can go into them. - On WSL, working in
/mnt/c. Those are the Windows folders, and Linux permissions don’t work the same way there: everything shows up asrwxrwxrwxandchmodmay not change anything. Practice in~.
Summary
Section titled “Summary”- Every file has an owner, a group and three sets of permissions: the owner (
u), the group (g) and everyone else (o).ls -lshows them at the start of each line. ris read,wchange andxrun. On a folder,xmeans being able to go in and reach what’s inside.chmodchanges permissions with letters (u+x) or with numbers (r= 4,w= 2,x= 1:644,600,755).chownchanges the owner, and only root can do it.- root bypasses permissions. You work as a normal user and use
sudofor one single command. - Least privilege: every program runs as its own user with only the permissions it needs. Web servers only use root to open port 80.
Did you get it?
Section titled “Did you get it?”To review:
Further reading
Section titled “Further reading”- The Linux Command Line, by William Shotts: chapter 9, “Permissions,” covers the same ground in more detail, including
umask. - File permissions and attributes on the Arch Linux wiki: a complete reference, with the special permissions this lesson doesn’t cover.