Intermediate · 6 min · practise every command in the built-in terminal

sudo — Execute as Superuser

sudo runs a command with root (administrator) privileges.

Basic usage

sudo command
sudo apt-get update
sudo nano /etc/hosts

Why sudo?

Running as root all the time is dangerous — one mistake can break the system. sudo lets you elevate only when needed.

sudo -i and su

sudo -i        # Switch to root shell (use sparingly)
su -           # Switch to root (requires root password)
su username    # Switch to another user

Who can use sudo?

Only users in the sudo group (on Debian/Ubuntu) or wheel group (on RHEL/CentOS).

groups              # See your groups
cat /etc/sudoers    # Sudo configuration (use visudo to edit!)

Sudo without password

In /etc/sudoers:

student ALL=(ALL) NOPASSWD: /usr/bin/apt-get

Important rules

  1. Never edit /etc/sudoers directly — use sudo visudo
  2. Don't share the sudo password
  3. Be careful with sudo rm -rf!
  4. Use sudo -l to see what commands you can sudo

Practice tasks

  1. Check which commands you can run with sudo
  2. Check your current groups
  3. Run a command as root using sudo
  4. Switch to a root shell

Commands in this lesson

Permissions