Linux Comm Sudo
# Linux sudo Command
[ Linux Command Manual](#)
sudo stands for Super User DO, allowing authorized users to execute commands as another user (typically the root user). This means that commands executed via sudo are run as if executed by root itself.
Usage Permission: Users listed in /etc/sudoers.
### Syntax
sudo command
**For example:**
sudo apt update # Update package list with root privileges sudo vim /etc/hosts # Edit a file requiring root privileges
**Parameter Description**:
* -i: Simulate initial login, loading the target user's environment
* -s: Run a shell
* -u user: Run as the specified user
* -l: List sudo commands the current user can execute
* -v: Verify user credentials (extend sudo session)
* -k: Invalidate sudo credential cache
### Examples
Execute a single command with root privileges:
sudo apt update sudo systemctl restart nginx sudo mkdir /opt/myapp
Switch to root user shell:
sudo -i # Login shell, load root environment variables sudo -s # Non-login shell sudo su - # Alternative method
Execute a command as another user:
sudo -u username command sudo -u postgres psql # Run psql as the postgres user
Check available permissions:
sudo -l
Edit system files:
sudo nano /etc/hosts sudo vim /etc/nginx/nginx.conf
### Configuration File
sudo configuration is stored in the /etc/sudoers file, which should be edited using the visudo command:
sudo visudo
Common configuration examples:
# Allow a user to execute all commands without password username ALL=(ALL) NOPASSWD: ALL # Allow a user group to execute specific commands%wheel ALL=(ALL) /bin/systemctl, /usr/bin/apt
### Common Issues
**User not in sudoers file:**
# Need root user to add the target user to the sudo group: usermod -aG sudo username # Ubuntu/Debian usermod -aG wheel username # CentOS/RHEL
**Adjusting cache time:**
Modify timestamp_timeout in /etc/sudoers (unit: minutes):
Defaults timestamp_timeout=30
* * *
## Difference between su and sudo
| Feature | su | sudo |
| --- | --- | --- |
| Password Required | Target user's password | Current user's password |
| Session Duration | Until manual exit | Single command or short-term cache |
| Configuration Complexity | Simple | Requires sudoers configuration |
| Security | Requires knowing root password | More granular permission control |
| Auditing | Limited | More detailed logs |
While the su command is simple and direct, in modern system administration, sudo is usually the recommended choice because it provides better security and auditing capabilities.
[ Linux Command Manual](#)
YouTip