LearnWithShobhan

sudo explained: the root user, sudoers and why you get "Permission denied"

Shobhan Biswas··5 min read
>_From the course CLI Mastery: 57 lessons, lifetime access.

You type a command, and the terminal answers "Permission denied". Someone on a forum says "just add sudo". It works, and you move on without knowing what you just did.

Let's fix that. By the end you will know who you are on the system, what the root user is, why sudo exists, and when to leave it alone.

First, who are you?

Every terminal session runs as a user, and every user belongs to one or more groups. whoami prints your user name, and id prints your numeric user id, your primary group and every group you are in:

bash
$ whoami
shobhan
$ id
uid=501(shobhan) gid=20(staff) groups=20(staff),12(everyone),61(localaccounts)

How users and groups decide who can read, write and run a file is covered in Linux file permissions explained. Here we need only one fact: you are not the owner of the files that run the system.

The problem: normal users cannot change the system

As a normal user you can work freely inside your home directory, and in other places you can only do what the permissions allow. The folders that make the system run (/etc for configuration, /bin and /usr/bin for programs) are owned by someone else. Installing a program for everyone means adding a file to one of those folders, and you do not have permission to.

Try it:

bash
$ cd /etc
$ ls -ld .
drwxr-xr-x 80 root wheel 2560 Nov 12 09:12 .
$ touch hello.txt
touch: hello.txt: Permission denied

Read the ls -ld . line: the owner is root, and your user is neither the owner nor (usually) in the group. So as "others" you get r-x: you can look and enter, but you cannot create or delete anything. That is exactly what should happen. The system is protecting itself from you.

The root user, and the problem with sharing it

Unix has one special user called root (user id 0). Root ignores the permission rules: it can read, change or delete any file on the system. That power is needed for administration: installing software, editing system settings, managing users.

In the early days an administrator simply logged in as root with the root password. It worked for one person, but when you have many administrators it falls apart:

Diagram: left, john, bob and rachel share one root password with full access; right, john runs sudo, the system checks the sudoers file, and root access is granted for one command
One shared password cannot be controlled. sudo gives each person temporary, checked, per-command root access.
  • Everyone knows the same all-powerful password.
  • Nobody can tell who did what.
  • If one person leaves, you have to change the password and tell everyone.

The fix: sudo

sudo stands for superuser do. You stay logged in as yourself, with your own password, and put sudo in front of one command to run it as root.

bash
$ touch hello.txt
touch: hello.txt: Permission denied
$ sudo touch hello.txt
Password:
$ ls -l hello.txt
-rw-r--r-- 1 root wheel 0 Nov 12 09:14 hello.txt

Two details worth noticing:

  1. The password prompt asks for your own password, not the root password. (sudo remembers it for a few minutes, so you will not be asked for every command.)
  2. The new file is owned by root, because root created it. You can see that in the ls -l output.

You can even watch the identity change:

bash
$ whoami
shobhan
$ sudo whoami
root

Who is allowed to use sudo? The sudoers file

Before running your command, sudo checks a configuration file, /etc/sudoers. It decides who may use sudo, and what they may run. With 100 users, you could create an admin group and write one rule: only members of that group can gain root.

On most systems the default is already in place. On Ubuntu and Debian the group is sudo; on macOS it is admin. Edit the file only with the tool made for it:

sudo visudo

visudo checks the syntax before saving. A typo in /etc/sudoers can lock every user out of sudo, and visudo stops you from saving that mistake. To see what your user is allowed to do, run sudo -l.

When not to use sudo

sudo removes the safety net, so treat it as a deliberate step, not a magic word.

  • Do not run everyday tools such as editors or git with sudo. Files they create become owned by root and then you cannot edit them.
  • Prefer a narrow fix: change the permissions of one file instead of running everything as root.
  • On a real server, give each person their own account and let sudo log who did what. Never share the root password.

Key takeaways

  • ✓whoami shows your user. id shows your user id and all your groups.
  • ✓root can do anything. Normal users cannot change system folders such as /etc.
  • ✓sudo runs one command as root, using your own password, so nobody shares the root password.
  • ✓/etc/sudoers decides who may use sudo. Edit it only with visudo.
  • ✓Do not add sudo just to silence an error. First find out why the permission is missing.

Go from copy-and-paste to understanding

Knowing why a command needs sudo is the difference between fixing a server and guessing on one. CLI Mastery builds this up with users, permissions, ownership and processes, then has you configure a real EC2 server over SSH. 57 lessons, lifetime access. You can start with the free lesson on absolute and relative paths.

Free download

Linux command line cheat sheet

One printable page: navigation, files, permissions, pipes, processes, scripts and SSH.

Occasional emails with new tutorials and courses. Unsubscribe any time.

Keep reading