How to Run Commands as Root with sudo

Published 2026-08-05 · Learn Linux

The sudo command runs a single command with administrator (root) privileges. sudo apt install nginx installs software without logging in as root. sudo asks for your own password, remembers it for a few minutes, and is the standard way to do admin work on modern Linux.

Run a command with root privileges

Prefix the admin command with sudo. It asks for your password (not root's), then runs the command as root.

$ sudo apt update

Why sudo instead of root?

Logging in as root means every mistake can break the system with no confirmation. sudo gives temporary power, keeps a log of who ran what, and your normal user stays the default. If a command is refused, prepend sudo rather than switching users.

Keep root for a while with sudo -i

For a series of admin commands, start a root shell with sudo -i. Type exit when done. Use it sparingly — one-off sudo is safer.

$ sudo -i

Run a command as another user

With -u, sudo runs a command as any user, not just root. This lets you test what a service user sees:

$ sudo -u www-data which php

Who can use sudo?

Only users in the sudo group (or listed in /etc/sudoers) can use sudo. Add a user to the group with usermod -aG sudo. See sudo for the full option list.

Keep learning

Related guides: How to Install and Manage Packages with apt · Linux User Management: useradd, usermod and passwd · How to Change File Ownership with the Linux chown Command. Read all tutorials on the Learn Linux blog.