Skip to content

Regular users, root, and privileged actions

Module
Your First Linux Session
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.4 LTS (VM) and 24.04.5 LTS (lab system), GNU Bash 5.2.21
On this page

Your Linux system has another home directory besides your own: /root. You ask the terminal to list what is in it, and it refuses:

Screen
learner@rootvolt:~$ ls /root
ls: cannot open directory '/root': Permission denied

A web search finds a one-line answer: put sudo in front of the command. You try it, and the terminal asks for a password. Whose password? What does that one word allow, and should you put it in front of the next command that fails too?

By the end of this lesson, you can say which account you are using, read a refusal like this one, run a single command as the administrator, and decide when that is the right thing to do.

  • Terminal, console, shell, and TTY (B03.2). Open a terminal in rootvolt-lab; the prompt is learner@rootvolt:~$.
  • This lesson runs whoami, id, ls, and sudo. Type each command exactly as shown and press Enter; module B04 explains how commands are built. Every command here only reads. None of them changes your system.

The screens were captured in Ubuntu 24.04.4 LTS in VirtualBox 7.2.18 on a Windows host.

Type this and press Enter:

Terminal window
whoami

In the tested VM, it printed:

Screen
learner@rootvolt:~$ whoami
learner

A user account is a name the system knows, with a number, a home directory, and files it owns. whoami prints the account you are working as: every program you start from this prompt runs as learner, the account you created in lesson B02.3.

Now ask for more detail:

Terminal window
id

In the tested VM, it printed one long line, which the window wrapped:

Screen
learner@rootvolt:~$ id
uid=1000(learner) gid=1000(learner) groups=1000(learner),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),100(users),114(lpadmin)

uid=1000(learner) is your user ID: the system knows you by the number 1000. A group is a named set of accounts, so that one rule can apply to several accounts. gid is your main group, and groups lists every group you are in. Module B11 explains groups.

Find one name in the list: 27(sudo). You are in the group sudo, which matters in a moment. The group numbers and the full list may differ on another installation; the name sudo is what counts.

learner is a regular user. A regular user can read many of the system’s files, but can change only its own, such as the files in its home directory, /home/learner.

The administrator account is root, which the sudo manual page calls the superuser. Root may read and change any file and any setting. /root is root’s home directory.

Now run the command from the opening:

Terminal window
ls /root

In the tested VM, the answer was the refusal you saw at the start:

Screen
learner@rootvolt:~$ ls /root
ls: cannot open directory '/root': Permission denied

It starts with ls:, so the program ls ran, but it cannot open directory '/root': Permission denied. The system refused this action for your account. Nothing is broken. Module B11 explains how each file says who may do what.

sudo runs one command as another user, root by default. Its manual page says it “allows a permitted user to execute a command as the superuser or another user”. Type sudo, a space, and the command:

Terminal window
sudo ls /root

sudo asks for a password first. In the tested VM, it showed this line:

Screen
[sudo] password for learner:

Nothing appears while you type the password, not even dots: the sudoers manual page says that sudo turns off echo “until the user hits the return (or enter) key”. Read the line: password for learner. sudo wants your password, the one you chose in lesson B02.3, not a password for root. Type it and press Enter. In the tested VM, the screen then read:

Screen
learner@rootvolt:~$ sudo ls /root
[sudo] password for learner:
snap

This time ls could open the directory, because it ran as root. It listed one name, snap; what that is does not matter here.

Not every account may do this. Ubuntu’s sudo_root manual page says that administrators are added “to the group ‘sudo’”: the 27(sudo) in your id line.

Now run whoami with sudo in front, in the same window. Guess first: what will it print, and will sudo ask for your password again?

Terminal window
sudo whoami
Run it, then compare with your guess

In the tested VM, it printed:

Screen
learner@rootvolt:~$ sudo whoami
root

sudo ran whoami as root, so whoami printed root. And there was no password prompt: the sudo manual page says that, by default, sudo “caches credentials on a per-terminal basis for 15 minutes”. A new terminal window asks again.

Look at the prompt after root: learner@rootvolt:~$ again. Only the one command after sudo ran as root. The next command runs as learner, unless it starts with sudo too.

One command crosses the account boundary with sudo In the account learner, ls /root stops at the account boundary with Permission denied, before it reaches the directory /root. sudo ls /root goes through sudo, which sits on the boundary: sudo checks your password, then runs ls as root, and ls reads /root. Only that one command runs as root. As learner As root ls /root sudo ls /root Permission denied reads sudo checks your password /root Runs ls as root
One command crosses the account boundary with sudo In the account learner, ls /root stops at the account boundary with Permission denied, before it reaches the directory /root. sudo ls /root goes through sudo, which sits on the boundary: sudo checks your password, then runs ls as root, and ls reads /root. Only that one command runs as root. As learner As root ls /root sudo ls /root Permission denied reads sudo checks your password /root Runs ls as root
Simplified model. ls /root, run as learner, stops at the account boundary with Permission denied; sudo ls /root crosses it for one command, after sudo checks your password.

In words: run as learner, ls /root stops at the boundary between the accounts, with Permission denied, and never reaches the directory. sudo ls /root goes through sudo, which sits on the boundary: it checks your password, then runs ls as root, and ls reads /root. Only that one command crosses.

This is a simplified model. It leaves out where the rules are kept: in the settings of sudo, and in each file’s permissions (module B11).

In lesson B02.3, you unticked VirtualBox’s unattended installation, which would have given root the same password as your user. Ubuntu’s own installer does something else. The sudo_root manual page says:

By default, the password for the user “root” (the system administrator) is locked. This means you cannot login as root or use su. Instead, the installer will set up sudo to allow the user that is created during install to run all administrative commands.

So root exists, but nobody can log in as root with a password; administration goes through sudo and your own password. Among its reasons, the page notes that attackers know a system “has an account named root and will try that first”. With root’s password locked, there is no root password to guess.

For an account in the group sudo that has never used sudo, each new terminal window shows a message above the first prompt. Your VM may no longer show it. On the course’s lab system, a new Bash for such an account printed:

Output (lab system)
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

man sudo_root opens the manual page quoted above; lesson B04.3 explains manual pages. The message comes from /etc/bash.bashrc, a file that Bash reads each time it starts in a new terminal window. These are its lines on the lab system:

Output (lab system)
# sudo hint
if [ ! -e "$HOME/.sudo_as_admin_successful" ] && [ ! -e "$HOME/.hushlogin" ] ; then
case " $(groups) " in *\ admin\ *|*\ sudo\ *)
if [ -x /usr/bin/sudo ]; then
cat <<-EOF
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
EOF
fi
esac
fi

You do not need to read this language yet. In plain words: Bash prints the message if your home directory has no file named .sudo_as_admin_successful (or .hushlogin), you are in the group admin or sudo, and sudo is installed.

The sudoers manual page says that file is “created the first time a user that is a member of the sudo or admin groups runs sudo”. After your first sudo command, new terminals stop showing the message. The dot at the start of the name hides the file; lesson B04.1 shows hidden files.

Use sudo when the task is administration, such as installing software (module B12) or changing a system setting, and you can say what the command does; lesson B04.1 teaches you to read one.

Do not use it for your own files, which learner may already change, or as a reflex after an error: read the message first, because most errors are not about permission. And do not use it to get a shell in which every command runs as root. One command at a time keeps each privileged action visible, and this course never needs more.

Reading Permission denied as a broken system. In ls: cannot open directory '/root': Permission denied, the program ran and the system refused your account. Ask whether the task needs root before you reach for sudo.

Waiting at the password prompt, or typing a root password. After [sudo] password for learner:, nothing appears while you type. Type your own password, the one you log in with, and press Enter. If it is wrong, the sudoers manual page says that sudo shows Sorry, try again. and, by default, gives you three tries.

Adding sudo to a different error. A typo in the name gives another message. On the lab system:

Error output (lab system)
learner@rootvolt:~$ ls /roots
ls: cannot access '/roots': No such file or directory

No such file or directory says that /roots does not exist. Root cannot list a directory that does not exist either, so sudo would not help. Fix the name; lesson B04.4 practices reading messages like this.

Answers on the web often contain a line that starts with sudo. When you paste one and type your password, the command after sudo runs as root: it may read and change any file on the system, not only yours. With Ubuntu’s settings, sudo checks who you are, not what the command does. The sudo_root page says the password prompt “should make you think about the consequences of what you are doing”. If you cannot say what the line does, do not type your password.

Goal: Show that sudo remembers your password for one terminal only, and that only the command after sudo runs as root.

  1. Open a new terminal window with Ctrl+Alt+T.
  2. Run whoami, then sudo whoami, then whoami again.

Expected: learner; then a password prompt and root; then learner.

Hint

sudo remembers your password “on a per-terminal basis”. Is the new window the same terminal as the one you used before? Lesson B03.2 showed what tty prints in each window.

Solution

whoami prints learner. sudo whoami asks [sudo] password for learner: even if you used sudo in another window a minute ago, because each window is its own terminal. After your password, it prints root. The last whoami prints learner: only one command ran as root.

  • whoami prints the account you work as; id adds your user ID and groups. In the course VM, you are learner, user ID 1000, in the group sudo.
  • root, the administrator, may change anything; a regular user changes only its own files. Permission denied means the system refused your account, not that it is broken.
  • sudo runs one command as root, after your own password, which does not appear as you type. By default, it remembers the password in that terminal for 15 minutes.
  • Ubuntu’s installer locks root’s password and lets the first user administer with sudo. The message in new terminals stops after your first sudo command.
  • Use sudo only for administration, and only for a command you can read.

Next, in Shutdown, restart, and session recovery (B03.4), you shut down and restart the system safely, and get your prompt back when a command waits.

  • Ubuntu 24.04 manual pages: sudo_root(8), on the locked root password, the group sudo, and the reasons for sudo; sudo(8), section DESCRIPTION, on caching credentials; sudoers(5), the options timestamp_timeout, timestamp_type, passwd_tries, badpass_message, pwfeedback, and admin_flag.
  • Ubuntu 24.04 manual pages: whoami(1), id(1), and bash(1): section INVOCATION says when Bash reads /etc/bash.bashrc, and section FILES calls it “The systemwide per-interactive-shell startup file”.
  • Filesystem Hierarchy Standard 3.0: /root, the home directory of the root user.