Sudo Command on Ubuntu 26.04 (2026): Examples + Cheat Sheet

Share

The sudo command lets you run administrative tasks on Ubuntu 26.04 LTS and Ubuntu 26.10 without staying logged in as root. These examples use systemctl, apt and safe file operations you will reuse often.

Before You Start
Ubuntu version
sudo is configured by default for the first desktop user on Ubuntu 26.04 and 26.10.
Password
Your account password is required unless an admin set NOPASSWD rules. Failed attempts are logged.
Least privilege
Use sudo for single commands. Prefer systemctl over legacy service wrappers when managing units.
KEY TAKEAWAYS
01Root without root login
sudo elevates one command (or a shell) while keeping an audit trail.
02apt and systemctl
Everyday Ubuntu admin work is sudo apt and sudo systemctl.
03Cheat sheet included
Download the free sudo cheat sheet in PDF or ebook formats.
04Edit sudoers safely
Always use visudo so a syntax error cannot lock you out.
05Logs tell the story
Check journalctl or /var/log/auth.log when privilege prompts fail.
06Related reading
See the security commands guide for SSH hardening next.
Free Cheat Sheet

Download the Sudo Command Cheat Sheet

Sudo examples from this guide in one file. Pick PDF for printing, or EPUB / DOCX / MOBI / AZW3 for your reader.

Also see Linux security commands and Top 40 Linux commands.

How to use sudo on Ubuntu

Basic form: sudo command. Ubuntu prompts for your user password, then caches credentials briefly.

sudo -v
sudo -v
Sample terminal output of id showing user and group IDs on Ubuntu
Sample output: id showing your user and groups (generated for this guide). Compare with sudo -u www-data id.

Common sudo examples

1. Update and upgrade packages

Refresh indexes and apply upgrades with apt.

sudo apt update
sudo apt update
sudo apt upgrade

2. Install a package

Install software from Ubuntu repositories.

sudo apt install curl
sudo apt install curl

3. Remove a package

Remove a package you no longer need.

sudo apt remove package-name
sudo apt remove package-name

4. Restart a service

Use systemctl on Ubuntu 26.04, not the legacy service command.

sudo systemctl restart ssh
sudo systemctl restart ssh
sudo systemctl status ssh

5. Create a directory as root

Useful for paths outside your home.

sudo mkdir -p /opt/myapp
sudo mkdir -p /opt/myapp
sudo chown $USER:$USER /opt/myapp

6. Change ownership

Fix files created by root that your user should own.

sudo chown $USER:$USER /opt/myapp/config.toml
sudo chown $USER:$USER /opt/myapp/config.toml

7. Run as another user

Execute a command as www-data or another account.

sudo -u www-data id
sudo -u www-data id

8. Preserve environment selectively

Keep needed variables with care; prefer explicit env passing.

sudo –preserve-env=PATH env | head
sudo --preserve-env=PATH env | head

9. Edit sudoers safely

Never edit /etc/sudoers with a normal editor.

sudo visudo
sudo visudo

10. List allowed privileges

Show rules that apply to your account.

sudo -l
sudo -l

11. Follow auth logs

See recent sudo attempts on Ubuntu.

journalctl -u ssh –no-pager | head
journalctl -u ssh --no-pager | head
sudo grep sudo /var/log/auth.log | tail

Useful sudo options

Option Meaning
-u user Run as another user
-i Login shell as target user
-s Shell with elevated privileges
-k Invalidate cached credentials
-v Refresh timestamp without a command
-l List privileges
-E / –preserve-env Preserve environment (use sparingly)

Tips and best practices

Prefer full paths when scripts call sudo. Avoid sudo su – for routine tasks; elevate only the command that needs it. After changing sudoers, open a second terminal and verify sudo -l before closing your working session.