Sudo

来自WordPress中文文档
跳转至: 导航搜索

Linux命令:sudo

功能说明:以其他身份来执行指令。
语  法:sudo [-bhHpV][-s ][-u <用户>][指令] 或 sudo [-klv]

补充说明:sudo可让用户以其他的身份来执行指定的指令,预设的身份为root。在/etc/sudoers中设置了可执行sudo指令的用户。若其未经授权的用户企图使用sudo,则会发出警告的邮件给管理员。用户使用sudo时,必须先输入密码,之后有5分钟的有效期限,超过期限则必须重新输入密码。

参  数:
-b  在后台执行指令。 
-h  显示帮助。 
-H  将HOME环境变量设为新身份的HOME环境变量。 
-k  结束密码的有效期限,也就是下次再执行sudo时便需要输入密码。 
-l  列出目前用户可执行与无法执行的指令。 
-p  改变询问密码的提示符号。 
-s  执行指定的shell。 
-u<用户>  以指定的用户作为新的身份。若不加上此参数,则预设以root作为新的身份。 
-v  延长密码有效期限5分钟。 
-V  显示版本信息。 


英文版本,欢迎翻译:

ABOUT

Allows a user with proper permissions to execute a command as the superuser or other user.

SYNTAX

sudo -K | -L | -V | -h | -k | -l | -v

sudo [-HPSb] [-a auth_type] [-c class|-] [-p prompt] [-u username|#uid] {-e file [...] | -i | -s | command}

-H The -H (HOME) option sets the HOME environment variable to the homedir of the target user (root by default) as specified in passwd. By default, sudo does not modify HOME (see set_home and always_set_home in sudoers). -K The -K (sure kill) option is like -k except that it removes the user's timestamp entirely. Like -k, this option does not require a password. -L The -L (list defaults) option will list out the parameters that may be set in a Defaults line along with a short description for each. This option is useful in conjunction with grep. -P The -P (preserve group vector) option causes sudo to preserve the invoking user's group vector unaltered. By default, sudo will initialize the group vector to the list of groups the target user is in. The real and effective group IDs, however, are still set to match the target user. -S The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. -V The -V (version) option causes sudo to print the version number and exit. If the invoking user is already root the -V option will print out a list of the defaults sudo was compiled with as well as the machine's local network addresses. -a The -a (authentication type) option causes sudo to use the specified authentication type when validating the user, as allowed by /etc/login.conf. The system administrator may specify a list of sudo-specific authentication methods by adding an "auth-sudo" entry in /etc/login.conf. This option is only available on systems that support BSD authentication where sudo has been configured with the --with-bsdauth option. -b The -b (background) option tells sudo to run the given command in the background. Note that if you use the -b option you cannot use shell job control to manipulate the process. -c The -c (class) option causes sudo to run the specified command with resources limited by the specified login class. The class argument can be either a class name as defined in /etc/login.conf, or a single '-' character. Specifying a class of - indicates that the com- mand should be run restricted by the default login capabilities for the user the command is run as. If the class argument specifies an existing user class, the command must be run as root, or the sudo command must be run from a shell that is already root. This option is only available on systems with BSD login classes where sudo has been configured with the --with-logincap option. -e The -e (edit) option indicates that, instead of running a command, the user wishes to edit one or more files. In lieu of a command, the string "sudoedit" is used when consulting the sudoers file. If the user is authorized by sudoers the following steps are taken:

1. Temporary copies are made of the files to be edited with the owner set to the invoking user. 2. The editor specified by the VISUAL or EDITOR environment variables is run to edit the temporary files. If neither VISUAL nor EDITOR are set, the program listed in the editor sudoers variable is used. 3. If they have been modified, the temporary files are copied back to their original location and the temporary versions are removed.

-h The -h (help) option causes sudo to print a usage message and exit. -i The -i (simulate initial login) option runs the shell specified in the passwd entry of the user that the command is being run as. The command name argument given to the shell begins with a - to tell the shell to run as a login shell. sudo attempts to change to that user's home directory before running the shell. It also initializes the environment, leaving TERM unchanged, setting HOME, SHELL, USER, LOGNAME, and PATH, and unsetting all other environment variables. Note that because the shell to use is determined before the sudoers file is parsed, a runas_default setting in sudoers will specify the user to run the shell as but will not affect which shell is actually run. -k The -k (kill) option to sudo invalidates the user's timestamp by setting the time on it to the epoch. The next time sudo is run a password will be required. This option does not require a password and was added to allow a user to revoke sudo permissions from a .logout file. -l The -l (list) option will list out the allowed (and forbidden) commands for the user on the current host. -p The -p (prompt) option allows you to override the default password prompt and use a custom one. The following percent (`%') escapes are supported:

%u expanded to the invoking user's login name %U expanded to the login name of the user the command will be run as (defaults to root) %h expanded to the local hostname without the domain name %H expanded to the local hostname including the domain name (on if the machine's hostname is fully qualified or the fqdn sudoers option is set) %% two consecutive % characters are collapsed into a single % character

-s The -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in passwd. -u The -u (user) option causes sudo to run the specified command as a user other than root. To specify a uid instead of a username, use #uid. Note that if the targetpw Defaults option is set (see sudoers) it is not possible to run commands with a uid not listed in the password database. -v If given the -v (validate) option, sudo will update the user's timestamp, prompting for the user's password if necessary. This extends the sudo timeout for another 5 minutes (or whatever the timeout is set to in sudoers) but does not run a command. -- The -- flag indicates that sudo should stop processing command line arguments. It is most useful in conjunction with the -s flag.

EXAMPLES

su -u hope ls ~hope

List the contents of the hope directory as the hope user.