sudo

On a server you work as a normal user: sudo runs one command as root. Who may use it, sudo -u, and the > trap.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • sudo -l
  • sudo touch /srv/site/index.html
  • ls -l /srv/site
  • echo "hello" | sudo tee /srv/site/index.html
  • sudo useradd -m -s /bin/bash intern
  • sudo -u intern whoami

How it works

On real servers you log in as a normal user (the prompt ends in $). Files outside your home folder belong to root, so changing them says "Permission denied". sudo COMMAND runs that one command as root.

Only users in the sudo group (or listed in /etc/sudoers.d) may use it — id shows your groups. sudo -u USER runs a command as someone else instead.

Syntax

sudo [-u USER] COMMAND  |  sudo -l

Options

OptionWhat it does
COMMANDrun it as root
-u USERrun it as that user instead
-lwhat you are allowed to run
| sudo tee FILEwrite a root-owned file (the > alone can't)

Good to know

  • sudo echo hi > /etc/motd still fails: the > is done by your own shell, before sudo runs. Use echo hi | sudo tee /etc/motd.
  • Avoid sudo -i (a whole root shell) for everyday work: one command at a time is safer and easier to read back.