Skip to content

The sudo command

The sudo command runs one command as the administrator, root, as in sudo apt update…

The sudo command runs one command as the administrator, root, as in sudo apt update; it asks for your own password and records what you ran.

Taught in Advanced, lesson 41

A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.

Usage

sudo COMMAND [ARGUMENT]...
sudo -i
sudo -u USER COMMAND
sudo -l

Run a command as the administrator (root). Runs one command as root, the administrator, if you are in the sudo group. Installing software, managing services, changing other users and editing files in /etc all need it. A real sudo asks for your own password and remembers it for 15 minutes; this practice shell asks for nothing and stores nothing. One trap: in sudo echo hi > /etc/motd the > is done by your own shell before sudo runs, so it fails; use echo hi | sudo tee /etc/motd. sudo -i opens a root shell (the prompt ends in #); exit leaves it. Everything here is simulated in the page: no real accounts, passwords or software are involved.

In the practice terminal:The practice sudo never asks for a password: it shows the prompt, says so, and carries on. Nothing is typed, sent or stored.

How to use sudo

Run a command as root

sudo asks for your password (not root’s) the first time, then remembers it for about 15 minutes.

sudo whoami
[sudo] password for learner: 
root
sudo apt update
[sudo] password for learner: 
Hit:1 http://archive.ubuntu.com/ubuntu noble InRelease
Get:2 http://archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
Get:3 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Fetched 252 kB in 1s (252 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

Read a file only root can read

cat /etc/shadow
cat: /etc/shadow: Permission denied
sudo head -n 2 /etc/shadow
[sudo] password for learner: 
root:*:20465:0:99999:7:::
learner:$y$simulated$:20465:0:99999:7:::

Write to a protected file: sudo tee

sudo echo hi > /etc/motd fails, because your own shell opens the file for > before sudo runs. Pipe into sudo tee instead.

sudo echo hi > /etc/motd
bash: /etc/motd: Permission denied
echo "Welcome" | sudo tee /etc/motd
[sudo] password for learner: 
Welcome

Run as another user: sudo -u

sudo -u www-data whoami
[sudo] password for learner: 
www-data

A root shell: sudo -i

The prompt ends in # while you are root. Type exit to go back, and keep it short: it is easy to forget you are root.

sudo -i
[sudo] password for learner: 

What you are allowed to run: sudo -l

sudo -l
Matching Defaults entries for learner on sandbox:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, use_pty

User learner may run the following commands on sandbox:
    (ALL : ALL) ALL

Options

-ia root login shell
-sa root shell, staying in this folder
-u USERrun as another user instead of root
-llist what you may run
-kforget the remembered password

More examples

sudo apt install tree
Install a program
[sudo] password for learner: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  tree
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 47.9 kB of archives.
After this operation, 116 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu noble/main amd64 tree amd64 2.1.1-2ubuntu3 [47.9 kB]
Fetched 47.9 kB in 0s (47.9 kB/s)
Selecting previously unselected package tree.
… and 4 more lines
echo "hello" | sudo tee /tmp/note.txt
Write a file as root
[sudo] password for learner: 
hello

Questions about sudo

What is the difference between sudo and su?

sudo runs one command as root with your own password, and logs it. su switches to another account, root by default, with that account’s password. Ubuntu leaves root without a password, so sudo is the usual way there.

What does "user is not in the sudoers file" mean?

Your account isn’t allowed to use sudo. An administrator can allow it with sudo usermod -aG sudo yourname (the group is wheel on Fedora); it works after you log in again.

Why can’t I see my password as I type it?

sudo hides it on purpose, without even showing stars. Type it and press Enter.

What does sudo !! do?

It runs your last command again with sudo in front, handy after "Permission denied". The practice shell doesn’t expand !!, so press the up arrow and add sudo instead.

Learn it step by step

Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.