The sudo command in Linux lets a permitted user run a command as the superuser (or another user), according to a security policy—on Ubuntu, usually sudoers via /etc/sudoers and /etc/sudoers.d/. You authenticate with your password (not root’s), and credentials are cached for a short time (often about 15 minutes per terminal by default).
man sudo sudo -V sudo -l # list what you may run
Official reference: Ubuntu man page sudo(8). Always edit policy with visudo so a typo does not lock you out of sudo. If you already know your goal, jump to: add a sudo user, -i/-u, redirection fix, or NOPASSWD. For writing text into files as root, pair with echo and tee.

For a broader Ubuntu terminal quick reference, see the Linux commands cheat sheet.
Basic Usage
sudo command [args...]
Examples on Ubuntu:
sudo apt update sudo systemctl status ssh sudo nano /etc/hosts
Enter your account password when prompted. Success means the command runs with elevated privileges; failure usually means wrong password, not in the sudo group, or the command is not allowed by policy.
Useful Options You Will Use Often
| Option | Meaning |
|---|---|
sudo -l | List your sudo privileges |
sudo -v | Refresh the credential cache (no command) |
sudo -k / sudo -K | Invalidate / clear cached credentials |
sudo -u user | Run as another user (not only root) |
sudo -i | Login shell as the target user (usually root) |
sudo -s | Shell as target, using $SHELL / invoking user’s shell |
sudo -E | Ask to preserve environment (policy may deny) |
sudo -n | Non-interactive; fail instead of prompting |
sudoedit file / sudo -e file | Edit a file safely as root |
sudo -u www-data whoami # www-data sudo -i # root login shell (home, profile files) sudo -s # root shell; environment closer to your current session
Add a User to Sudo (Ask Ubuntu Classic)
On Ubuntu, members of the sudo group may run any command via sudo because /etc/sudoers includes:
%sudo ALL=(ALL:ALL) ALL
# Preferred: add existing user to sudo group sudo adduser alice sudo # or sudo usermod -aG sudo alice
The change applies on the user’s next login. Check:
groups alice sudo -l -U alice # as an admin
To grant a custom rule, use visudo (or sudo visudo -f /etc/sudoers.d/alice)—do not blindly echo >> /etc/sudoers.
sudo -i vs sudo su vs sudo /bin/bash
Ask Ubuntu’s high-traffic comparison:
| Command | Rough effect |
|---|---|
sudo -i | Target user’s login shell (reads login profiles; home often becomes root’s) |
sudo su - | Similar login-style root shell via su |
sudo su | Root interactive shell; often stay in your previous directory |
sudo /bin/bash or sudo -s | Root shell with more of the calling user’s environment |
Prefer sudo -i when you want a clean root login environment. Prefer single-command sudo … when you only need one privileged action—less risk than staying root in a long session.
Passwordless Sudo (NOPASSWD) — Use Carefully
For automation, restrict specific commands, not full ALL, when possible:
sudo visudo -f /etc/sudoers.d/deploy
Example line:
deploy ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx
Broad form (higher risk):
alice ALL=(ALL) NOPASSWD: ALL
Rules are applied in order; the last matching entry wins (man 5 sudoers). Prefer files under /etc/sudoers.d/ with clear names, and check with sudo -l.
Classic Trap: sudo + Shell Redirection
This is the same pitfall as sudo echo … > file—the shell opens the target before privileges apply. Details and tee patterns also appear in the echo command guide.
This fails with “Permission denied”:
sudo echo "hello" > /etc/motd
Why: the shell opens /etc/motd before sudo runs—as your user. Fixes from Ask Ubuntu:
# tee writes as root echo "hello" | sudo tee /etc/motd >/dev/null # append echo "hello" | sudo tee -a /etc/motd >/dev/null # or a root shell for the whole line sudo bash -c 'echo "hello" > /etc/motd'
Same idea for reading root-only files into a user-owned redirect: open the redirect with a root-aware tool (tee, dd, or bash -c).
Common Ubuntu Tasks with Sudo
sudo apt update && sudo apt upgrade sudo apt install htop sudo systemctl restart networking sudo journalctl -u ssh -n 50 sudo fdisk -l sudo mount /dev/sdb1 /mnt
Graphical apps: prefer not launching GUI tools with normal sudo as your desktop user (Ask Ubuntu warns about root-owned config under your home). Prefer PolicyKit/pkexec or dedicated admin tools when available.
Troubleshooting
| Problem | What to try |
|---|---|
user is not in the sudoers file | Log in as an admin; sudo adduser you sudo, then re-login |
| Wrong password | Use your password, not root’s (Ubuntu’s root account is often locked) |
sudo: unable to resolve host | Fix /etc/hosts so hostname matches (common Ask Ubuntu hit) |
Broken /etc/sudoers | Boot recovery / live USB; repair with visudo from a root shell |
| Redirection permission denied | Use tee or sudo bash -c '… > file' |
| Need to drop cache | sudo -k then next command re-prompts |
sudo -l # am I allowed? id # groups? getent group sudo # who is in sudo?
Security Habits
Prefer
sudo commandover living in a root shell.Use
visudo/visudo -fonly.Prefer narrow
NOPASSWDcommand lists.Do not paste unknown scripts under
sudo.Clear cache on shared machines:
sudo -k.
Quick Cheat Sheet
sudo cmd # run as root sudo -u user cmd # run as user sudo -i # root login shell sudo -l # list privileges sudo -v # refresh timeout sudo -k # invalidate cache sudo visudo # edit sudoers safely sudo adduser name sudo # grant sudo group echo x | sudo tee /path # write file as root
After Admin Tasks: User-Facing Documents on Linux
sudo is for privileged system work. Once the admin task is done, the next file you open is usually a normal note, spreadsheet, checklist, or PDF that should stay in your regular user workflow.
WPS Office for Linux fits that user-facing side of the job with Writer, Spreadsheet, Presentation, and PDF tools that do not require living in a root shell.

| What you need next | What WPS gives you |
|---|---|
| Create user-facing documents after privileged system work | Strong .docx / .xlsx / .pptx layout fidelity when files move between Linux, Windows, and macOS |
| One place for notes, sheets, slides, and PDFs | Free core suite: Writer, Spreadsheet, Presentation, plus PDF view/edit/convert tools |
| A familiar desktop UI for switchers | Ribbon-style apps with a lighter footprint than many heavy suites |
| A trusted install path | Official DEB (Ubuntu/Debian/Mint) and RPM packages from the vendor |
Download the current package from the official WPS Office for Linux download page, install it with sudo apt install ./wps-office_VERSION_amd64.deb, then return to a normal user session for the DOCX, spreadsheet, or PDF files you actually share.
FAQ
What does sudo mean?
“Superuser do”—run a command with elevated privileges allowed by policy.
What password does sudo ask for?
Your user password (if you are allowed), not the root password, on typical Ubuntu desktops.
How do I give someone sudo access?sudo adduser username sudo, then have them log in again.
How do I see my sudo rights?sudo -l
sudo vs su?su switches user (often needs the target password). sudo runs a command (or shell) as another user using your credentials and sudoers rules.
Where is the official documentation?
Run man sudo, man sudoers, and man visudo, or see the Ubuntu sudo(8) man page.




