Linux command
sudo 命令
安全
权限或系统影响较大,执行前请核对目标。
常用示例
Run a command as root
sudo [command]
Run a command as another user
sudo -u [username] [command]
Start a root shell
sudo -i
Start a shell as another user
sudo -u [username] -s
Run command with preserved environment
sudo -E [command]
Edit a file as root
sudo -e [/etc/hosts]
List allowed commands
sudo -l
Invalidate cached credentials
sudo -k
Run command in background
sudo -b [command]
说明
sudo (superuser do) executes commands with elevated privileges, typically as root. It provides controlled access to privileged operations based on rules defined in /etc/sudoers, allowing fine-grained permission management. After successful authentication, sudo caches credentials for a grace period (typically 5 minutes), allowing subsequent commands without re-entering the password. Use -k to invalidate this cache. The sudoers file controls who can run what commands on which hosts. Users can be granted full root access or restricted to specific commands. The visudo command should be used to edit sudoers safely.
参数
- -u _USER_, --user _USER_
- Run command as specified user (default: root)
- -g _GROUP_, --group _GROUP_
- Run command with specified primary group
- -i, --login
- Run login shell as target user
- -s, --shell
- Run shell (from $SHELL or user's default)
- -e, --edit
- Edit files (sudoedit mode)
- -E, --preserve-env
- Preserve user's environment variables
- -H, --set-home
- Set HOME to target user's home directory
- -b, --background
- Run command in background
- -l, --list
- List allowed commands for user
- -v, --validate
- Refresh credential cache without running command
- -k, --reset-timestamp
- Invalidate cached credentials
- -K, --remove-timestamp
- Remove cached credentials entirely
- -n, --non-interactive
- Non-interactive mode; fail if password required
- -A, --askpass
- Use askpass helper for password
- -S, --stdin
- Read password from stdin
- -p _PROMPT_, --prompt _PROMPT_
- Custom password prompt
- -T _TIMEOUT_
- Command timeout in seconds
- -V, --version
- Display version information
FAQ
What is the sudo command used for?
sudo (superuser do) executes commands with elevated privileges, typically as root. It provides controlled access to privileged operations based on rules defined in /etc/sudoers, allowing fine-grained permission management. After successful authentication, sudo caches credentials for a grace period (typically 5 minutes), allowing subsequent commands without re-entering the password. Use -k to invalidate this cache. The sudoers file controls who can run what commands on which hosts. Users can be granted full root access or restricted to specific commands. The visudo command should be used to edit sudoers safely.
How do I run a basic sudo example?
Run `sudo [command]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -u _USER_, --user _USER_ do in sudo?
Run command as specified user (default: root)