Skip to content

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

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

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?”

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.

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:

$ whoami
ubuntu
$ id
uid=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 is sudo: 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/passwd
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin

They 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.

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 -l
total 8
-rw-rw-r-- 1 ubuntu ubuntu 6 Oct 12 13:29 notes.txt
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 12 13:29 project

The 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.

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.

When a program opens a file, the system asks, in order:

  1. Are you root? You can do almost anything; permissions don’t count.
  2. Are you the owner? The first three letters are used, and nothing else.
  3. Are you in the file’s group? The middle three are used.
  4. 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/shadow
cat: /etc/shadow: Permission denied

ubuntu 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.sh
Hello from course

Without 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) or a (all).
  • The operation: + adds, - removes and = sets exactly those.
  • Examples: chmod o-r notes.txt takes reading away from everyone else; chmod go= secret.txt takes 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.sh

With 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

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 uploads
chown: changing ownership of 'uploads': Operation not permitted
$ sudo chown www-data uploads
$ ls -ld uploads
drwxrwxr-x 2 www-data ubuntu 4096 Oct 12 13:29 uploads

Only 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.)

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):

$ whoami
ubuntu
$ sudo whoami
root
$ sudo head -1 /etc/shadow
root:*: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.

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 80
nc: Permission denied

With 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 nginx
USER PID CMD
root 2887 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 2888 nginx: worker process

The 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.

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.

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:

Ventana de terminal
brew install --cask multipass

It will ask for your Mac’s password. If you don’t have Homebrew, download the installer from the Multipass website.

1. Create the virtual machine

Ventana de terminal
multipass launch 24.04 --name course

What you will see:

Launched: course

launch 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

Ventana de terminal
multipass shell course

You’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.

3. Who are you here?

Ventana de terminal
whoami
id
sudo whoami

What you will see:

ubuntu
uid=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)
root

Notice 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

Ventana de terminal
sudo useradd -m -s /bin/bash luis
id luis
ls -l /home

What you will see:

uid=1001(luis) gid=1001(luis) groups=1001(luis)
total 8
drwxr-x--- 2 luis luis 4096 Oct 12 13:29 luis
drwxr-x--- 2 ubuntu ubuntu 4096 Oct 12 13:29 ubuntu

Creating 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

Ventana de terminal
sudo -iu luis

sudo -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

Ventana de terminal
echo "We launch the shop on Monday" > plan.txt
echo "Today I broke the website by accident" > diary.txt
ls -l

What 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

Ventana de terminal
ls /home/luis
cat /home/luis/plan.txt

What you will see:

ls: cannot open directory '/home/luis': Permission denied
cat: /home/luis/plan.txt: Permission denied

Both 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

Ventana de terminal
sudo cat /home/luis/plan.txt

What you will see:

We launch the shop on Monday

It 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):

Ventana de terminal
chmod o+x ~
chmod 600 diary.txt
exit

His folder ends up as drwxr-x--x, and the diary as -rw-------. Now as ubuntu:

$ cat /home/luis/plan.txt
We launch the shop on Monday
$ cat /home/luis/diary.txt
cat: /home/luis/diary.txt: Permission denied
$ ls /home/luis
ls: cannot open directory '/home/luis': Permission denied

The 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

Ventana de terminal
sudo userdel -r luis

What you will see:

userdel: luis mail spool (/var/mail/luis) not found

userdel 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.

  • On your Mac. The ana staff in ls -l in 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, staff and 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 -g sometimes answers EACCES: 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 a chmod +x, git diff shows old mode 100644 and new mode 100755: the permissions as numbers, after a 100 that means “regular file.”
  • chmod 777 to get rid of a Permission 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.
  • sudo for everything. What you create with sudo belongs to root, and afterwards you can’t change it without sudo. Use it only on the command that needs it.
  • sudo with >. sudo echo "hello" > /etc/greeting.txt answers -bash: /etc/greeting.txt: Permission denied. The redirection is done by your shell, which isn’t root, before sudo starts (you saw this in lesson 3). Use echo "hello" | sudo tee /etc/greeting.txt: tee copies whatever reaches it into a file, and here it does so as root.
  • A folder without x. chmod -R 644 (-R applies it to everything inside) takes the x away 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 as rwxrwxrwx and chmod may not change anything. Practice in ~.
  • Every file has an owner, a group and three sets of permissions: the owner (u), the group (g) and everyone else (o). ls -l shows them at the start of each line.
  • r is read, w change and x run. On a folder, x means being able to go in and reach what’s inside.
  • chmod changes permissions with letters (u+x) or with numbers (r = 4, w = 2, x = 1: 644, 600, 755). chown changes the owner, and only root can do it.
  • root bypasses permissions. You work as a normal user and use sudo for 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.
You’re ubuntu, in the sudo group but not in the www-data group. ls -l shows: -rw-r----- 1 www-data www-data 1200 Oct 12 10:00 config.php. What can you do with that file without typing sudo?

Marta’s folder has drwxr-x--x, and inside it is report.txt with -rw-r--r--. You’re not Marta and you’re not in her group. What happens with ls /home/marta and with cat /home/marta/report.txt?

You keep a file on the server with the database password. Only your program has to read it, and it runs as your user. What permissions do you give it?

Your web server has to listen on port 80, but when you start it as its own user it answers Permission denied. What do you do?

You type sudo echo '127.0.0.1 shop.local' >> /etc/hosts and it answers Permission denied. Why, if you used sudo?

The shop’s program runs as its own user, shop, and has to save its logs in /var/log/shop, a root folder with drwxr-xr-x. What’s the right fix?

  • 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.
phase-1 · lesson 4/10