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
- Never edit
/etc/sudoersdirectly — usesudo visudo - Don't share the sudo password
- Be careful with
sudo rm -rf! - Use
sudo -lto see what commands you can sudo