Hi everyone. In the previous article every apt command started with sudo, and running apt without it failed with are you root?. This guide looks at sudo itself on Ubuntu 26.04: what it changes, how to check who is allowed to use it, how to give a new user sudo access and take it away again, and two traps that confuse almost everyone at first, the redirect that fails even with sudo and the difference between sudo and a root shell.
Ubuntu 26.04 ships sudo-rs, a rewrite of sudo in Rust, so a few messages look different from older tutorials. All output below comes from my own terminal. On this lab machine sudo is set not to ask for a password so the screenshots stay short. On a normal Ubuntu install sudo asks for your own password, not the root password, and then remembers it for a few minutes.
If you have not read it yet, the previous article on the apt command in Ubuntu shows what happens when apt runs without sudo.
Distro : Ubuntu 26.04.1 LTS (Desktop)
Admin user : mamangdigital
Test user : alice (no sudo yet)What sudo Changes
whoami with and without sudo

whoami
sudo whoami
id -nGWithout sudo the command runs as my own user, mamangdigital (number 1). With sudo in front, the same command runs as root (number 2). sudo works for me because my account is in the sudo group (number 3). On Ubuntu, the first user created during installation is added to this group automatically.
A file only root can read

cat /etc/shadow | head -n 2
sudo head -n 2 /etc/shadow | cut -c 1-40/etc/shadow stores password hashes, so a normal user gets Permission denied (number 1). With sudo the first lines are readable (number 2). This is the whole idea of sudo: you stay a normal user and borrow root power only for one command at a time.
Checking Who Can Use sudo
sudo -l -U for another user

id alice
sudo -l -U alicealice is a regular user who belongs only to her own group and the team group from the permissions lab (number 1). sudo -l -U shows what a user may run with sudo, and for alice the answer is clear: not allowed to run sudo (number 2).
What alice sees when she tries

sudo -u alice sudo -n whoamiThis runs sudo as alice. -n stops sudo from waiting for a password, so it fails immediately if it would have to ask. sudo-rs answers I’m sorry alice. I’m afraid I can’t do that (number 1). Older sudo versions say alice is not in the sudoers file instead. Both mean the same thing, and the attempt is recorded in the system log.
Giving a User sudo Access
Add the user to the sudo group

sudo usermod -aG sudo alice
id -nG alice
sudo -l -U alice | tail -n 2usermod -aG adds alice to the sudo group without removing her other groups. Forgetting the -a is a classic mistake, because -G alone replaces every supplementary group. alice is now in sudo (number 1), and sudo -l shows (ALL : ALL) ALL, which means she may run any command as any user (number 2).
Now sudo wants her password

sudo -u alice sudo -n whoamiThe message changed to interactive authentication is required (number 1). alice is allowed now, and sudo only needs her own password, which -n prevents it from asking for. If alice is logged in already, she has to log out and back in before the new group applies to her session.
Running a Command as Another User
sudo -u

sudo -u alice whoami
sudo -u alice touch /home/alice/notes.txt
sudo ls -l /home/alice/notes.txtsudo is not only for root. With -u it runs a command as any user, here alice (number 1). A file created this way belongs to alice, not to me or to root (number 2). This is handy when you fix files in another user’s home folder and do not want to leave root-owned files behind.
The Redirect That Fails Even with sudo
sudo echo with > and the tee fix

sudo echo lab > /etc/lab.txt
echo lab | sudo tee /etc/lab.txt
ls -l /etc/lab.txtThe first line still gives Permission denied (number 1). The reason is that the > redirect is handled by my own shell before sudo even starts, and my shell has no right to write in /etc. sudo only raised the echo, which did not need it. The fix is to let a command running under sudo do the writing, and tee is made for that (number 2). The file now exists and belongs to root (number 3). Use sudo tee -a to append instead of overwrite.
sudo -i: A Root Shell
Opening and leaving a root shell

sudo -i
whoami; pwd
exitsudo -i opens a login shell as root. The prompt changes from $ to # (number 1), whoami says root and the working folder is /root (number 2). Every command now runs as root without sudo, which is exactly why it is risky to stay there. Type exit to return to your own user (number 3).
Warning: Use sudo -i only for a short series of admin commands and exit right away. A forgotten root terminal is the easiest way to delete or overwrite something important by accident.
Where sudo Is Logged
journalctl for sudo

sudo journalctl _COMM=sudo -n 4 --no-pager -o cat | cut -c 1-90Every sudo command is written to the system journal with the user, the folder, the target user, and the command that ran (number 1), plus the session that opened as root (number 2). I cut the lines at 90 characters to keep them on screen. This is where to look when you want to know who did what on a shared machine.
Removing sudo Access
gpasswd -d

sudo gpasswd -d alice sudo
sudo -l -U alice
sudo rm /etc/lab.txtgpasswd -d removes alice from the sudo group (number 1), and sudo -l confirms she is back to not allowed (number 2). I also deleted the test file from /etc.
Common Problems and How to Fix Them
user is not in the sudoers file / I’m sorry, I can’t do that
The account is not in the sudo group. An admin has to run sudo usermod -aG sudo username, and the user has to log in again.
Permission denied with sudo and >
The redirect runs in your own shell. Use command | sudo tee file, or sudo tee -a file to append.
sudo asks for a password I do not know
sudo wants your own account password, not a root password. On Ubuntu the root account has no password by default.
A user lost all groups after usermod
usermod -G was used without -a, which replaced the groups. Add the missing groups back with usermod -aG.
That covers sudo on Ubuntu: what it changes, checking access with sudo -l, giving and removing access with the sudo group, running commands as another user, the redirect trap, root shells, and the log. Next in the series is adding and removing users with useradd and adduser. If sudo shows a message I did not cover, paste it in the comments.












