Regular users, root, and privileged actions
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:
learner@rootvolt:~$ ls /rootls: cannot open directory '/root': Permission deniedA 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.
Before you start
Section titled “Before you start”- Terminal, console, shell, and TTY (B03.2). Open a terminal in
rootvolt-lab; the prompt islearner@rootvolt:~$. - This lesson runs
whoami,id,ls, andsudo. 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.
Ask which account you are
Section titled “Ask which account you are”Type this and press Enter:
whoamiIn the tested VM, it printed:
learner@rootvolt:~$ whoamilearnerA 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:
idIn the tested VM, it printed one long line, which the window wrapped:
learner@rootvolt:~$ iduid=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.
Two kinds of account
Section titled “Two kinds of account”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:
ls /rootIn the tested VM, the answer was the refusal you saw at the start:
learner@rootvolt:~$ ls /rootls: cannot open directory '/root': Permission deniedIt 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.
Run one command as root
Section titled “Run one command as root”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:
sudo ls /rootsudo asks for a password first. In the tested VM, it showed this line:
[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:
learner@rootvolt:~$ sudo ls /root[sudo] password for learner:snapThis 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?
sudo whoamiRun it, then compare with your guess
In the tested VM, it printed:
learner@rootvolt:~$ sudo whoamirootsudo 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.
The account boundary
Section titled “The account boundary”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).
Why root has no password
Section titled “Why root has no password”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.
The message in a new terminal
Section titled “The message in a new terminal”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:
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:
# sudo hintif [ ! -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 esacfiYou 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.
When to use sudo
Section titled “When to use sudo”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.
Mistakes to recognize
Section titled “Mistakes to recognize”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:
learner@rootvolt:~$ ls /rootsls: cannot access '/roots': No such file or directoryNo 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.
Security Angle
Section titled “Security Angle”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.
Try it yourself
Section titled “Try it yourself”Goal: Show that sudo remembers your password for one terminal only, and that only the command after sudo runs as root.
- Open a new terminal window with Ctrl+Alt+T.
- Run
whoami, thensudo whoami, thenwhoamiagain.
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.
whoamiprints the account you work as;idadds your user ID and groups. In the course VM, you arelearner, user ID 1000, in the groupsudo.- root, the administrator, may change anything; a regular user changes only its own files.
Permission deniedmeans the system refused your account, not that it is broken. sudoruns 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 firstsudocommand. - Use
sudoonly 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.
Further reading
Section titled “Further reading”- Ubuntu 24.04 manual pages: sudo_root(8), on the locked root password, the group
sudo, and the reasons forsudo; sudo(8), section DESCRIPTION, on caching credentials; sudoers(5), the optionstimestamp_timeout,timestamp_type,passwd_tries,badpass_message,pwfeedback, andadmin_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.