sudo Command
The sudo (superuser do) command in Linux allows a permitted user to execute a command as the superuser or another user, as specified by the security policy. It provides a secure way to grant elevated privileges to users without sharing the root password.
Syntax
Description
sudo executes a command as another user. If no user is specified, it defaults to the superuser (root). The command is executed with the effective user ID and group IDs of the target user. Users must be listed in the sudoers file (typically edited with visudo) to be able to use sudo.
Common uses include:
- Executing administrative commands that require root privileges
- Running commands as a different user for testing or security purposes
- Temporarily elevating privileges for specific tasks
- Maintaining a log of commands executed with elevated privileges
Common Options
| Option | Description |
|---|---|
-u, --user=user |
Run the command as the specified user |
-s, --shell |
Run the shell specified by the SHELL environment variable |
-i, --login |
Run the shell as a login shell (environment variables are set as if the user had logged in) |
-k, --reset-timestamp |
Invalidate the user's cached credentials |
-l, --list |
List user's privileges or check a specific command |
-v, --validate |
Update the user's cached credentials, keeping them valid for another N minutes |
Examples
Run a command as root
Updates the package lists with superuser privileges.
Run a command as a different user
Creates a new file as the 'www-data' user.
Open a root shell
Opens a new shell as the root user.
Edit the sudoers file safely
Opens the sudoers file for editing using the 'visudo' command, which provides syntax checking.
List your sudo privileges
Lists the commands you are allowed to run with sudo.