Permissions and sudo
Why the terminal says Permission denied, how to read ls -l, how chmod fixes it, and what sudo really does. Part 4 of the Terminal Basics series.
By the end of this tutorial you will know how to:
- read the long listing from
ls -l, including the cryptic-rw-r--r-- - understand what "Permission denied" actually means
- change permissions with
chmod, using letters and using numbers - run a script of your own
- use
sudowithout feeding yourself to the monsters
Prerequisites: you can move around and list files (part 1), and cat a file (part 3).
How this was checked: every command was run on Ubuntu 24.04 with bash 5.2 and GNU coreutils 9.4, in a real terminal session, and the output shown is the real output. All of it was run as an ordinary (non-admin) user, except the three sudo examples. Our test user was deliberately given no sudo rights, so those were run from an administrator account, where sudo runs the command exactly as written. On your computer the terminal will usually ask for your password first, and you will see no characters as you type it. Your username and group will differ from ours (gerald).
To follow along: type each command and press Enter. Everything that changes permissions happens inside the practice mansion.
1. Every file has a lock
Every file and directory on a Linux system has three things attached to it: an owner (one user), a group (a set of users), and a list of permissions that say what each kind of person may do.
There are three permissions: read (look inside), write (change or delete), and execute (run it as a program, or, for a directory, step inside). And there are three kinds of person: the owner, the group, and everyone else.
So each file is a small grid of nine yes-or-no answers. Who may read it? Who may write to it? Who may run it? When the terminal says Permission denied, it is not broken. It is a locked door. You are a visitor, and the lock is doing its job.
Ghosts walk through walls. You, regrettably, do not.
2. Rebuild the mansion
Start from a fresh mansion with the setup script from part 1 (the whole script is in that post if you do not have it):
bash ~/mansion-setup.shThe mansion is built. Try not to wake anyone.cd ~/mansion3. Who am I?
Permissions are about who is asking, so first, find out who you are:
whoami
groupsgerald
geraldwhoami is your username. groups lists the groups you belong to. On many Linux systems each user also gets a group with the same name as their username, which is why you may see the same word twice later on.
4. Reading ls -l
Plain ls shows names. ls -l (a lowercase L, for "long") shows details, including permissions:
ls -l librarytotal 8
-rw-r--r-- 1 gerald gerald 374 Oct 31 03:12 diary.txt
-rw-r--r-- 1 gerald gerald 58 Oct 31 03:12 guests.txtIgnore the total line. Each other line describes one file. Take the first one apart, left to right:
| Part | Meaning |
|---|---|
-rw-r--r-- | The type and the permissions (decoded below) |
1 | A link count. Ignore it for now |
gerald | The owner |
gerald | The group |
374 | The size in bytes |
Oct 31 03:12 | When it was last changed |
diary.txt | The name |
Now the permissions. The ten characters split like this:
- rw- r-- r--
| | | |
| | | +-- everyone else: read only
| | +------- the group: read only
| +------------ the owner: read and write
+---------------- the type: a dash means a file, a d means a directoryEach trio of letters is r (read), w (write) and x (execute), in that order, with a dash where a permission is missing. So the diary can be read and written by its owner, and only read by everyone else.
Directories have permissions too. Here is the mansion itself, where d marks the directories:
ls -ltotal 20
drwxr-xr-x 3 gerald gerald 4096 Oct 31 03:12 attic
drwxr-xr-x 2 gerald gerald 4096 Oct 31 03:12 cellar
drwxr-xr-x 2 gerald gerald 4096 Oct 31 03:12 kitchen
drwxr-xr-x 2 gerald gerald 4096 Oct 31 03:12 library
-rw-r--r-- 1 gerald gerald 55 Oct 31 03:12 readme.txtOn a directory, x means "you may step inside it". A directory you can read but not execute is a room you can see into, but never enter.
5. When a file will not run
Let us try to run something. The cellar has a script called vault.sh. A script is a text file of commands that the terminal can run like a program:
cd cellar
cat vault.sh#!/bin/bash
echo "The vault is open. It was never locked."Run it by typing its name:
vault.shbash: vault.sh: command not found"Command not found", although we can see it right there. By default, the terminal does not look in the directory you are standing in when you type a bare name. If you want to run something from here, say so with ./, where the dot means "this directory":
./vault.shbash: ./vault.sh: Permission deniedNow it is found, and refused. This is the lock at work. Look at the permissions on the script:
ls -l vault.sh-rw-r--r-- 1 gerald gerald 59 Oct 31 03:12 vault.sh-rw-r--r--. There is no x anywhere, so nobody may run it, not even its owner.
6. chmod: change the lock
chmod means change mode (a file's mode is its permissions). The quickest form says what to add or remove. +x adds the execute permission:
chmod +x vault.sh
ls -l vault.sh-rwxr-xr-x 1 gerald gerald 59 Oct 31 03:12 vault.shThree x marks have appeared. Now try again:
./vault.shThe vault is open. It was never locked.You should see "The vault is open. It was never locked." If you get Permission denied, run ls -l vault.sh and check for the x. If you get "command not found", you left out the ./.
Taking permissions away
You can say exactly who you mean. The letters are u (the owner, "user"), g (the group), o (others) and a (all), followed by + to add or - to remove, then the permissions. Make the diary private by removing every permission from the group and others:
chmod go-rwx ../library/diary.txt
ls -l ../library/diary.txt-rw------- 1 gerald gerald 374 Oct 31 03:12 ../library/diary.txtNow only the owner can read it. Everyone else is locked out.
The numbers version
You will see chmod 755 and chmod 644 everywhere, so learn to read them. Each permission has a value: read is 4, write is 2 and execute is 1. Add up the ones you want for each person, and write the owner, group and others as three digits.
| Digit | Permissions | Sum |
|---|---|---|
| 7 | rwx | 4 + 2 + 1 |
| 6 | rw- | 4 + 2 |
| 5 | r-x | 4 + 1 |
| 4 | r-- | 4 |
| 0 | --- | nothing |
So 644 is owner rw-, group r--, others r--, and 755 is rwxr-xr-x. Try 600, which means "owner reads and writes, nobody else gets anything":
chmod 600 ../library/diary.txt
ls -l ../library/diary.txt-rw------- 1 gerald gerald 374 Oct 31 03:12 ../library/diary.txtThe result is the same as the letters version a moment ago. They are two ways of saying one thing, and you will meet both.
Locking yourself out
You can use permissions against yourself. Remove all permission, even from the owner, and see what happens:
chmod 000 ../library/guests.txt
cat ../library/guests.txtcat: ../library/guests.txt: Permission deniedYou are the owner, and you cannot read your own file. Permissions apply to everybody, including whoever set them. As owner you are allowed to change the lock, so put it back:
chmod 644 ../library/guests.txt
cat ../library/guests.txtgerald
brenda
gerald
casper
brenda
gerald
morticia
casperDirectories obey the same rules. Take away the x from the library and you cannot step inside it:
chmod -x ../library
cd ../librarybash: cd: ../library: Permission deniedPut it back, and the door opens again:
chmod +x ../library
cd ../library
pwd
cd ../cellar/home/gerald/mansion/library7. Why system files say no
So far we changed locks on our own files, because we own them. Other files belong to other users. The system's settings live in /etc, which is owned by the all-powerful administrator account, called root. Try to create a file there:
touch /etc/ghost.txttouch: cannot touch '/etc/ghost.txt': Permission deniedYou are not root, so the lock holds. That is the point: it stops you, or a program running as you, from breaking the whole system by accident.
8. sudo: borrowing the keys
When you do need to do something as root, there is sudo, short for superuser do. Put it in front of a command and that one command runs as root:
sudo whoamirootWithout sudo you are you. With it, the command runs as root. That is the whole idea. Create the file that was refused a moment ago:
sudo touch /etc/ghost.txtIt printed nothing, which as we know means success. Check that it is really there:
ls /etc/ghost.txt/etc/ghost.txtNow tidy up after ourselves:
sudo rm /etc/ghost.txtsudo does not unlock one door. It unlocks all of them, including the ones behind which the monsters are kept. Root is allowed to delete anything, including the files the whole system needs to start.
So the rules are short:
- If a command says
Permission denied, work out why before reaching forsudo. Usually the answer is a wrong path, not a missing key. - Only use
sudowith a command you understand, from a source you trust. - A mistake made with
sudohas no trash can and nobody to complain to. That includes a typo.
9. Common errors
| What you see | What it usually means | Fix |
|---|---|---|
Permission denied when running a script | The script has no x permission | chmod +x script.sh, then ./script.sh |
command not found for a script that is right there | You left out ./ | Run it as ./script.sh |
Permission denied when reading or writing a file | The file's permissions, or its directory's, do not allow you | ls -l to see them. chmod if the file is yours |
Permission denied in a system directory | The file belongs to root | Only use sudo if you are sure what you are doing |
Practice
Predict the result before you run each one. Answers follow. Start from a fresh mansion:
bash ~/mansion-setup.sh
cd ~/mansion/cellarThe mansion is built. Try not to wake anyone.Exercise 1. What permissions does chmod 750 vault.sh give? Work it out in your head, then check.
Exercise 2. Make ../library/diary.txt readable and writable by you, and readable by nobody else. What number is that?
Exercise 3. Which number gives rw-r--r--?
Answers
Exercise 1. 7 is rwx for the owner, 5 is r-x for the group, and 0 is nothing for others:
chmod 750 vault.sh
ls -l vault.sh-rwxr-x--- 1 gerald gerald 59 Oct 31 03:12 vault.shExercise 2. Read plus write is 4 + 2 = 6 for the owner, and 0 for the group and others, so 600:
chmod 600 ../library/diary.txt
ls -l ../library/diary.txt-rw------- 1 gerald gerald 374 Oct 31 03:12 ../library/diary.txtExercise 3. rw- is 6, r-- is 4 and r-- is 4, so 644:
chmod 644 ../library/diary.txt
ls -l ../library/diary.txt-rw-r--r-- 1 gerald gerald 374 Oct 31 03:12 ../library/diary.txtWhat you learned
- Every file has an owner, a group and permissions: read, write and execute, for the owner, the group and everyone else.
ls -lshows them.-rw-r--r--means a file the owner can read and write, and everyone else can only read.Permission deniedis a lock doing its job, not a broken computer.chmod +xmakes a script runnable, and you run it with./. Numbers work too: read is 4, write is 2 and execute is 1, so755isrwxr-xr-xand644isrw-r--r--.sudoruns one command as root. It is a skeleton key, so understand the command before you use it.
Next in the series: Pipes and redirects. You will learn to chain commands together and save what they print, which is where the terminal stops being a list of tools and starts being a workshop.