logo
search
list

Table of Content

Basic Usage
Useful Options You Will Use Often
Add a User to Sudo (Ask Ubuntu Classic)
sudo -i vs sudo su vs sudo /bin/bash
Passwordless Sudo (NOPASSWD) — Use Carefully
Classic Trap: sudo + Shell Redirection
Common Ubuntu Tasks with Sudo
Troubleshooting
Security Habits
Quick Cheat Sheet
After Admin Tasks: User-Facing Documents on Linux
FAQ

Sudo Command in Linux: Run Commands as Root (Ubuntu Guide)

Posted by Algirdas Jasaitis

calendar

2026-07-27

views

873

likes

4

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.

Concept illustration of the Linux sudo command prompting for a password to run a privileged command in a terminal
Sudo runs a command as root (or another user) after you authenticate—check privileges with sudo -l and edit policy only with visudo.

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

OptionMeaning
sudo -lList your sudo privileges
sudo -vRefresh the credential cache (no command)
sudo -k / sudo -KInvalidate / clear cached credentials
sudo -u userRun as another user (not only root)
sudo -iLogin shell as the target user (usually root)
sudo -sShell as target, using $SHELL / invoking user’s shell
sudo -EAsk to preserve environment (policy may deny)
sudo -nNon-interactive; fail instead of prompting
sudoedit file / sudo -e fileEdit 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:

CommandRough effect
sudo -iTarget user’s login shell (reads login profiles; home often becomes root’s)
sudo su -Similar login-style root shell via su
sudo suRoot interactive shell; often stay in your previous directory
sudo /bin/bash or sudo -sRoot 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

ProblemWhat to try
user is not in the sudoers fileLog in as an admin; sudo adduser you sudo, then re-login
Wrong passwordUse your password, not root’s (Ubuntu’s root account is often locked)
sudo: unable to resolve hostFix /etc/hosts so hostname matches (common Ask Ubuntu hit)
Broken /etc/sudoersBoot recovery / live USB; repair with visudo from a root shell
Redirection permission deniedUse tee or sudo bash -c '… > file'
Need to drop cachesudo -k then next command re-prompts
sudo -l                 # am I allowed?
id                      # groups?
getent group sudo       # who is in sudo?

Security Habits

  • Prefer sudo command over living in a root shell.

  • Use visudo / visudo -f only.

  • Prefer narrow NOPASSWD command 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.

WPS Office Writer on a Linux desktop editing an incident report DOCX with a familiar ribbon toolbar
WPS Writer on Linux: turn setup notes, terminal findings, and troubleshooting steps into a DOCX or PDF that teammates can open easily.
What you need nextWhat WPS gives you
Create user-facing documents after privileged system workStrong .docx / .xlsx / .pptx layout fidelity when files move between Linux, Windows, and macOS
One place for notes, sheets, slides, and PDFsFree core suite: Writer, Spreadsheet, Presentation, plus PDF view/edit/convert tools
A familiar desktop UI for switchersRibbon-style apps with a lighter footprint than many heavy suites
A trusted install pathOfficial 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.

Algirdas Jasaitis

15 years of office industry experience, tech lover and copywriter. Follow me for product reviews, comparisons, and recommendations for new apps and software.