Anime-Sanctuary

Users, sudo and file permissions

A fresh server hands you the root account, which can do anything, including wreck everything with one typo. Let's create a sudo user, alice, and learn to read file permissions.

Course
Tutorial 9 of 35
Time
25 min to read
Tested with
Commands: tested on Debian 13.7 (trixie)
What you will be able to do
  • Explain why you avoid working as root.
  • Create a user with adduser and give it sudo.
  • Read a file's permissions with ls -l.
  • Change permissions with chmod and the owner with chown.

Before you start: Your first SSH connection, The terminal for beginners.

Contents
  1. Why you avoid working as root
  2. Create the user alice
  3. Give her sudo
  4. Log back in as alice and test sudo
  5. Read a file's permissions
  6. Change permissions with chmod
  7. Change the owner with chown
  8. If it fails
  9. What's next
  10. Sources
A row of mailboxes, each with its own name and its own key

How do you create a sudo user on Debian, and why do it right away? Because the account that came with your server, root, never asks a question before acting. We'll create an ordinary account, alice, that borrows the powers of root only when it has to. Then we'll learn to read a file's permissions, because they decide who can read, change or run what. You already logged in with the help of Your first SSH connection, and you know the first commands from The terminal for beginners.

Why you avoid working as root

The root account is the machine's absolute administrator: no file is off limits, and no command ever asks "are you sure?". That's handy and dangerous, like carrying a master key in your hand all day. One badly copied command, and a system folder vanishes with no recycle bin to catch it. A bot or an intruder who found its password would get the whole machine too.

You're logged in as root right now, and the whoami command ("who am I?") confirms it.

On the server
whoami
Expected output
root

The terminal reminds you in its prompt, the bit of text before your commands: it ends with # for root and with $ for an ordinary user. The fix is to work with an ordinary account and to borrow the powers of root only for one specific command. That loan is called sudo. Let's start by creating the account.

1Create the user alice

The adduser command creates a new user: an account with its own name, its own password and its own folder. Run it as root, with the name alice.

On the server
adduser alice
Expected output
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for alice
Enter the new value, or press ENTER for the default
    Full Name []: Alice
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n] Y

The command first asks for alice's password, twice. Nothing shows while you type, and that's on purpose: type it, then press Enter. Pick a long phrase rather than a short word, and keep it in a password manager, or on paper somewhere safe.

Then come a few questions about alice. Only the full name is useful: type Alice and press Enter, then press Enter alone for the others. Answer Y to the last question. Alice now has a home folder, /home/alice, where she keeps her things. But she has no administrator powers yet.

2Give her sudo

For alice to use sudo, we add her to the sudo group, a list of accounts that share the same rights. First check that the program is installed: command -v sudo asks "where is the sudo command?".

On the server
command -v sudo
Expected output
/usr/bin/sudo

If the command prints nothing, install it with apt-get install sudo: that command is covered in Updates and packages with apt. Now add alice to the sudo group.

On the server
usermod -aG sudo alice

The command prints nothing, which is a good sign. usermod modifies an account. In -aG, the G means "group" and the a ("append") keeps the groups alice already had. Without that a, she would lose them. Let's check with id, which lists an account's groups.

On the server
id alice
Expected output
uid=1000(alice) gid=1000(alice) groups=1000(alice),27(sudo),100(users)

The word sudo now appears in the list of groups. The numbers in parentheses are internal IDs, which you can ignore here. Time to try the account for real.

3Log back in as alice and test sudo

To test alice, open a new connection with her name in place of root. The command runs on your computer, and the server asks for alice's password. Replace 203.0.113.10, an example address, with your server's address; yours will be different.

On your computer
ssh alice@203.0.113.10

A few welcome lines scroll by, then the prompt becomes alice@mon-serveur:~$. The $ replaces the #: you're no longer root. The ~ means "in my home folder". To try sudo, put that word in front of a command: it then runs with the powers of root.

On the server
whoami
Expected output
alice
On the server
sudo whoami
Expected output
[sudo] password for alice:
root

Without sudo, the machine sees you as alice. With sudo, it asks for alice's password (not the one for root), then runs the command as root. For a few minutes, it doesn't ask again. So you get an everyday account and a repair tool, each in its place. Now let's see what alice can touch without sudo.

4Read a file's permissions

Every file carries its own permissions, and the ls -l command shows them. First we create an empty file with touch, then look at it: -l asks for the detailed view, one line per file.

On the server
touch notes.txt
ls -l notes.txt
Expected output
-rw-rw-r-- 1 alice alice 0 Nov  4 22:40 notes.txt

Your date will be today's. Let's read the line from left to right. The first sign says whether it's a file (-) or a folder (d). Then come nine letters, in three groups of three. Then alice twice: the file's owner, then its group. After that come the size in bytes (0: the file is empty), the date and the name.

Each group of three letters answers the same question for one kind of people. In order: the owner, the group, then everyone else. Each letter is a permission, or a dash when it's missing:

LetterMeaningFor a file
rreadsee its contents
wwritechange it
xexecuterun it as a program

Here, rw- for alice, rw- for her group and r-- for everyone else: all can read, only alice and her group can change it, and nobody can run the file. Let's see what that last point means in practice.

5Change permissions with chmod

The chmod command ("change mode") adds or removes permissions. To see it at work, we create a tiny one-line program and try to run it. Writing ./ in front of the name means "the one in this folder".

On the server
echo "echo bonjour" > bonjour.sh
./bonjour.sh
Expected output
-bash: ./bonjour.sh: Permission denied

Permission denied means the file lacks the x permission, so the machine refuses to run it. Let's add that permission for the owner only. In chmod u+x, read aloud as "u plus x", the u stands for the owner (the user) and +x adds the right to run.

On the server
chmod u+x bonjour.sh
ls -l bonjour.sh
./bonjour.sh
Expected output
-rwxrw-r-- 1 alice alice 13 Nov  4 22:40 bonjour.sh
bonjour

The x appeared in the first group, and the program says hello (in French, since the file says bonjour). The same idea takes a permission away. Here, o-r, read as "o minus r", removes reading for others (o for others), so that notes.txt stays private.

On the server
chmod o-r notes.txt
ls -l notes.txt
Expected output
-rw-rw---- 1 alice alice 0 Nov  4 22:40 notes.txt

The last three letters turned into dashes: other accounts can't see anything now. You'll also meet chmod with a number, which the next box explains.

Permissions as numbers: chmod 600

Each permission is worth a number: r is 4, w is 2, x is 1. You add them up for each kind of people. So 6 equals 4 + 2, which is rw-. A file that only its owner can read and write is therefore 600: 6 for the owner, then 0 and 0 for the group and everyone else.

On the server
chmod 600 notes.txt
ls -l notes.txt
Expected output
-rw------- 1 alice alice 0 Nov  4 22:40 notes.txt

Changing permissions settles the "what". Still open is the "who": what do you do with a file that belongs to someone else?

6Change the owner with chown

The chown command ("change owner") gives a file to another account, and only root can do that, so we go through sudo. To try it, we have root create a file in alice's folder, then we try to write in it.

On the server
sudo touch rapport.txt
ls -l rapport.txt
echo "premier essai" >> rapport.txt
Expected output
-rw-r--r-- 1 root root 0 Nov  4 22:41 rapport.txt
-bash: rapport.txt: Permission denied

The file belongs to root, and everyone else can only read it: alice can't write in it. The >> sign adds text at the end of a file. Let's give the file to alice, naming the new owner and then the group, separated by a colon.

On the server
sudo chown alice:alice rapport.txt
ls -l rapport.txt
echo "premier essai" >> rapport.txt
cat rapport.txt
Expected output
-rw-r--r-- 1 alice alice 0 Nov  4 22:41 rapport.txt
premier essai

The owner is now alice, and the write goes through. cat prints the text you added. So you can now create an account, lend it the powers of root and decide who can do what. Let's look at what can go wrong.

If it fails

When a command refuses to run, the message almost always says why. Here are the four cases that come up, with the exact message:

  • alice is not in the sudoers file. The sudo group is missing from alice. Log back in as root and run usermod -aG sudo alice again.
  • bash: sudo: command not found. The program isn't installed. As root, type apt-get install sudo.
  • sudo refused right after usermod, even though id alice shows the group. Alice was already logged in, and a connection that is already open doesn't reread its groups. Type exit, then log in again with ssh.
  • Permission denied when running a program or writing to a file. Read the permissions with ls -l. Either a permission is missing, which chmod fixes, or the owner is wrong, which chown fixes.

What's next

You now have an everyday account and a way to borrow the powers of root when needed. You'll use it right away: a fresh system is never up to date, and updating it takes exactly those powers. The next article, Updates and packages with apt, shows how to do it cleanly with sudo apt.