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 herenothing runs on your computer
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
| Option | What it does |
|---|---|
COMMAND | run it as root |
-u USER | run it as that user instead |
-l | what you are allowed to run |
| sudo tee FILE | write a root-owned file (the > alone can't) |
Good to know
sudo echo hi > /etc/motdstill fails: the>is done by your own shell, before sudo runs. Useecho 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.