Using sudo and su
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.
Before you start
Section titled “Before you start”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:
mkdir -p ~/lab/b11/privilegescd ~/lab/b11/privilegesThese 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.
Delegate a single command
Section titled “Delegate a single command”Ask sudo to run one read-only identity check:
sudo whoamiUnder 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.
learner@rootvolt:~/lab/b11/privileges$ sudo whoami[sudo] password for learner: rootlearner@rootvolt:~/lab/b11/privileges$ Steps (6)
- Type
sudo whoami - Press Enter
- Output:
[sudo] password for learner: - Type the password (the terminal shows nothing)
- Press Enter — Type the password (not shown) and press Enter
- 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:
sudo useradd -m -s /bin/bash samCompare a command as sam with a command in sam’s login environment:
sudo -u sam whoamisamsudo -u sam -i pwd/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:
whoamilearnerlearner@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$ Steps (9)
- Type
sudo -u sam whoami - Press Enter
- Output:
sam - Type
sudo -u sam -i pwd - Press Enter
- Output:
/home/sam - Type
whoami - Press Enter
- 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.
Ask su to authenticate the target account
Section titled “Ask su to authenticate the target account”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:
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?
su - -c whoamiRun 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:
sudo -i whoamilearner@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$ Steps (15)
- Type
su - learner -c 'id -un' - Press Enter
- Output:
Password: - Type the password (the terminal shows nothing)
- Press Enter — Type the password (not shown) and press Enter
- Output:
learner - Type
su - -c whoami - Press Enter
- Output:
Password: - Type the password (the terminal shows nothing)
- Press Enter — Type the password (not shown) and press Enter
- Output:
su: Authentication failure - Type
sudo -i whoami - Press Enter
- 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.
Find who opens the destination
Section titled “Find who opens the destination”Prepare a root-owned directory and file only inside this exercise:
sudo mkdir rootboxsudo touch rootbox/note.txtsudo chmod 755 rootboxsudo chmod 644 rootbox/note.txtls -ld rootboxdrwxr-xr-x 2 root root 4096 Nov 14 15:57 rootboxls -l rootbox/note.txt-rw-r--r-- 1 root root 0 Nov 14 15:57 rootbox/note.txtThe 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 >:
sudo printf 'Blocked update\n' > rootbox/note.txtRun it, then compare with your guess
bash: rootbox/note.txt: Permission deniedIt 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:
printf 'Approved update\n' | sudo tee rootbox/note.txtApproved updatetee 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.
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.
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$ Steps (6)
- Type
sudo printf 'Blocked update\n' > rootbox/note.txt - Press Enter
- Output:
bash: rootbox/note.txt: Permission denied - Type
printf 'Approved update\n' | sudo tee rootbox/note.txt - Press Enter
- Output:
Approved update
Verify the saved text and ownership rather than relying only on tee’s echoed output:
cat rootbox/note.txtApproved updatels -l rootbox/note.txt-rw-r--r-- 1 root root 16 Nov 14 15:57 rootbox/note.txtMistakes to recognize
Section titled “Mistakes to recognize”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.
Try it yourself
Section titled “Try it yourself”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
printf 'Verified audit\n' | sudo tee -a rootbox/note.txtVerified auditcat rootbox/note.txtApproved updateVerified auditThe first read established one original line. This read proves that both original and appended text remain in the separate protected file.
Reset this lesson
Section titled “Reset this lesson”Remove only the practice account created above, then inspect the exercise directory:
sudo userdel -r samgetent passwd samcd ~/lab/b11ls -AR privilegesThe 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.
sudo rm -r ~/lab/b11/privilegessudo -kWith authentication invalidated, -n refuses to prompt and reports the actual requirement:
sudo -n whoamisudo: a password is requiredThe 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.
-uselects a target and-irequests 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.
Further reading
Section titled “Further reading”- On your system:
man sudo,man su,man tee, andman bash, section REDIRECTION. - Ubuntu: User management.
- Ubuntu 24.04 manual: sudo.
- Util-linux: su.
- GNU Bash: Redirections.