Skip to content

Using sudo and su

Module
Users and Basic Permissions
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
On this page

You put sudo before a command, but writing its output to a protected practice file still fails. The command and the redirection do not necessarily run with the same identity.

By the end, you can distinguish delegated execution from account switching, recognize which password a tool requests, and identify the process that opens a redirected destination.

Finish Changing ownership and permissions, including its account reset. Keep your B04 lab directory. Use only disposable ~/lab/b11/privileges and a clean practice account name sam:

Terminal window
mkdir -p ~/lab/b11/privileges
cd ~/lab/b11/privileges

These experiments use the Ubuntu lab’s authorized learner account. Replace that name in account-specific commands if your VM uses another name. Use Reset this lesson before repeating the exercise. Nothing here writes to /etc, changes root’s password, or edits sudo policy.

Ask sudo to run one read-only identity check:

Terminal window
sudo whoami

Under this lab’s policy, sudo asks for the invoking learner’s password, not root’s password. The terminal shows nothing as you type, including no asterisks. Press Enter after the password; the command reports root.

sudo authenticates the invoking account
learner@rootvolt:~/lab/b11/privileges$ sudo whoami[sudo] password for learner: rootlearner@rootvolt:~/lab/b11/privileges$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. sudo authenticates learner and runs one identity check as root without showing the password.
Steps (6)
  1. Type sudo whoami
  2. Press Enter
  3. Output: [sudo] password for learner:
  4. Type the password (the terminal shows nothing)
  5. Press Enter — Type the password (not shown) and press Enter
  6. Output: root

Authentication checks who you are. Authorization decides whether policy allows the requested action and target identity. Knowing your password does not by itself authorize arbitrary sudo commands. The lab account has Ubuntu’s administrative group membership; another account or organization can have narrower rules.

sudo defaults to root in this lab. It starts the requested command with the allowed target identity, then returns control to your existing shell. It does not permanently turn the terminal into root.

Choose another account and a login environment

Section titled “Choose another account and a login environment”

Create the isolated practice account as in B11.4; it needs no password for these sudo tests:

Terminal window
sudo useradd -m -s /bin/bash sam

Compare a command as sam with a command in sam’s login environment:

Terminal window
sudo -u sam whoami
Captured output
sam
Terminal window
sudo -u sam -i pwd
Captured output
/home/sam

-u sam selects the target account. -i initializes a login environment and starts in that account’s home directory. Because pwd is supplied, this invocation runs that command and returns; it does not leave a shell waiting for input.

Now check the original shell’s identity:

Terminal window
whoami
Captured output
learner
Run a command or login environment as sam
learner@rootvolt:~/lab/b11/privileges$ sudo -u sam whoamisamlearner@rootvolt:~/lab/b11/privileges$ sudo -u sam -i pwd/home/samlearner@rootvolt:~/lab/b11/privileges$ whoamilearnerlearner@rootvolt:~/lab/b11/privileges$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Commands run as sam, a login environment starts in sam's home, and the invoking shell remains learner.
Steps (9)
  1. Type sudo -u sam whoami
  2. Press Enter
  3. Output: sam
  4. Type sudo -u sam -i pwd
  5. Press Enter
  6. Output: /home/sam
  7. Type whoami
  8. Press Enter
  9. Output: learner

With no command after sudo -i, you would enter a root login shell and stay there until exit. sudo -u sam -i without a command similarly enters sam’s login shell. This lesson uses bounded commands so the return to the learner prompt is explicit.

su also runs a substitute identity, but its ordinary password authentication concerns the target account. The - requests a login environment; -c supplies one command to its shell instead of keeping an interactive shell open.

Use your own lab account as the target for a successful password demonstration:

Terminal window
su - learner -c 'id -un'

Enter learner’s password at Password:. The captured command reports learner and returns. This example authenticates an account with a known lab password; it does not configure a password for sam.

Predict first: will that same lab password let su authenticate as root when no target account is named?

Terminal window
su - -c whoami
Run it, then compare with your guess

The controlled recording enters only the documented lab learner password. The attempt ends with su: Authentication failure; no root command runs. This is not a request to guess or change root’s password.

Ubuntu leaves root password authentication locked by default. A working sudo account therefore does not imply a working root password for su -. Contrast the failed authentication with an explicitly delegated login-environment command:

Terminal window
sudo -i whoami
su authenticates the target; sudo uses delegation
learner@rootvolt:~/lab/b11/privileges$ su - learner -c 'id -un'Password: learnerlearner@rootvolt:~/lab/b11/privileges$ su - -c whoamiPassword: su: Authentication failurelearner@rootvolt:~/lab/b11/privileges$ sudo -i whoamirootlearner@rootvolt:~/lab/b11/privileges$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. su authenticates learner successfully, rejects the root attempt, and sudo separately delegates a root login-environment command.
Steps (15)
  1. Type su - learner -c 'id -un'
  2. Press Enter
  3. Output: Password:
  4. Type the password (the terminal shows nothing)
  5. Press Enter — Type the password (not shown) and press Enter
  6. Output: learner
  7. Type su - -c whoami
  8. Press Enter
  9. Output: Password:
  10. Type the password (the terminal shows nothing)
  11. Press Enter — Type the password (not shown) and press Enter
  12. Output: su: Authentication failure
  13. Type sudo -i whoami
  14. Press Enter
  15. Output: root

The final command reports root. Sudo’s authorization path works without unlocking a password for root. Defaults and authentication policy can differ on other systems; read the prompt and the target account instead of trying one password everywhere.

Prepare a root-owned directory and file only inside this exercise:

Terminal window
sudo mkdir rootbox
sudo touch rootbox/note.txt
sudo chmod 755 rootbox
sudo chmod 644 rootbox/note.txt
Terminal window
ls -ld rootbox
Captured output
drwxr-xr-x 2 root root 4096 Nov 14 15:57 rootbox
Terminal window
ls -l rootbox/note.txt
Captured output
-rw-r--r-- 1 root root 0 Nov 14 15:57 rootbox/note.txt

The owner is root. Learner can traverse the directory and read the file, but cannot write its contents or replace its directory entry. Predict whether putting sudo before printf changes who performs >:

Terminal window
sudo printf 'Blocked update\n' > rootbox/note.txt
Run it, then compare with your guess
Captured output
bash: rootbox/note.txt: Permission denied

It does not. The invoking Bash opens the destination before starting the command. Bash is still learner, so opening fails and sudo never starts for this line. The diagnostic names Bash, not printf.

Keep the text producer unprivileged and let a narrowly selected writer open the protected destination:

Terminal window
printf 'Approved update\n' | sudo tee rootbox/note.txt
Captured output
Approved update

tee receives the text on stdin, writes it to the named file, and copies it to stdout. Sudo runs tee as root here, so tee can open the destination. The pipe itself carries bytes; it does not elevate printf or the parent shell.

Who opens the destination? The learner shell opens a redirection before sudo can run. A delegated tee process opens the piped destination as root. sudo cmd > file Learner shell opens: denied cmd | sudo tee file Root tee opens: allowed
Who opens the destination? The learner shell opens a redirection before sudo can run. A delegated tee process opens the piped destination as root. sudo cmd > file Learner shell opens: denied cmd | sudo tee file Root tee opens: allowed
Simplified model. With sudo cmd > file the learner shell opens the destination; with cmd | sudo tee file the delegated tee process opens it.

In words, identify the writer that opens the file. In the first route it is the learner shell, which is denied. In the second it is root’s tee process, which is allowed. The diagram isolates the destination open; it does not represent every process or descriptor in the pipeline.

The process opening the destination needs permission
learner@rootvolt:~/lab/b11/privileges$ sudo printf 'Blocked update\n' > rootbox/note.txtbash: rootbox/note.txt: Permission deniedlearner@rootvolt:~/lab/b11/privileges$ printf 'Approved update\n' | sudo tee rootbox/note.txtApproved updatelearner@rootvolt:~/lab/b11/privileges$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Bash cannot open the protected destination, while sudo tee opens it and records the supplied text.
Steps (6)
  1. Type sudo printf 'Blocked update\n' > rootbox/note.txt
  2. Press Enter
  3. Output: bash: rootbox/note.txt: Permission denied
  4. Type printf 'Approved update\n' | sudo tee rootbox/note.txt
  5. Press Enter
  6. Output: Approved update

Verify the saved text and ownership rather than relying only on tee’s echoed output:

Terminal window
cat rootbox/note.txt
Captured output
Approved update
Terminal window
ls -l rootbox/note.txt
Captured output
-rw-r--r-- 1 root root 16 Nov 14 15:57 rootbox/note.txt

Reading every missing password prompt as passwordless policy. Sudo can reuse a recent successful authentication. The reset below demonstrates invalidating that cached authentication before testing whether a password is needed.

Confusing su’s target password with sudo’s invoking password. Follow the tool, account, and policy. This Ubuntu lab never needs a root password.

Adding more sudo prefixes without identifying the opener. For redirection failures, first ask whether the invoking shell or a delegated process opens the file.

Goal: Append a second line, Verified audit, to the same protected practice file, preserving Approved update. Read back both lines without changing ownership.

Hint

B09 introduced appending. Tee’s -a requests append mode; Bash’s >> would still be opened by the learner shell.

Solution
Terminal window
printf 'Verified audit\n' | sudo tee -a rootbox/note.txt
Captured output
Verified audit
Terminal window
cat rootbox/note.txt
Captured output
Approved update
Verified audit

The first read established one original line. This read proves that both original and appended text remain in the separate protected file.

Remove only the practice account created above, then inspect the exercise directory:

Terminal window
sudo userdel -r sam
getent passwd sam
cd ~/lab/b11
ls -AR privileges

The account lookup prints no entry and returns status 2. As in B11.4, the container reports an absent mail spool while userdel removes the account and home.

Terminal window
sudo rm -r ~/lab/b11/privileges
sudo -k

With authentication invalidated, -n refuses to prompt and reports the actual requirement:

Terminal window
sudo -n whoami
Captured output
sudo: a password is required

The capture verifies removal. Return to Before you start to repeat the exercise from absent paths and account names.

  • Sudo delegates an authorized command; it does not permanently change the invoking shell.
  • -u selects a target and -i requests its login environment.
  • Su normally authenticates the target; Ubuntu’s locked root password is separate from sudo authorization.
  • Bash opens > itself; a delegated tee can open a specifically selected protected destination.

Next, Practice: Users and Basic Permissions combines identity, access changes, account selection, and a protected receipt.

  • On your system: man sudo, man su, man tee, and man bash, section REDIRECTION.
  • Ubuntu: User management.
  • Ubuntu 24.04 manual: sudo.
  • Util-linux: su.
  • GNU Bash: Redirections.