sudo (Substitute User Do)
sudo allows a permitted user to execute a command as the superuser (root) or another user, as specified by the security policy. It is the preferred way to perform administrative tasks in Linux without logging in as root.
1. Basic Usage
sudo [command]
- You must enter your own password, not the root password.
- Once authenticated, sudo remembers you for a short period (usually 5-15 minutes).
2. Key Options
| Option | Name | Description |
|---|---|---|
-u |
user | Run the command as a specific user instead of root. |
-i |
login | Acquire the root user's environment and start a login shell. |
-s |
shell | Start a shell with root privileges but keep the current environment. |
-v |
validate | Extend the sudo timeout period without running a command. |
-l |
list | List the allowed (and forbidden) commands for the invoking user. |
3. Practical Examples
① Installing Software
sudo apt install nginx
② Running a command as another user (not root)
sudo -u deploy_user ./deploy.sh
③ Editing System Config Files
sudo vi /etc/fstab
4. [Tip] Why sudo is better than su
Using sudo is safer because it keeps a log of all commands executed and their authors in /var/log/auth.log. This accountability is crucial for maintaining server security and troubleshooting human errors.