我按照教程安装了 Hudson 服务器。教程中有以下 Linux 命令(hudson
是 Ubuntu 中的用户名):
sudo -Hiu hudson
这是做什么sudo -Hiu
用的?执行此命令后会发生什么?
答案1
-Hiu 标志是 -H、-i 和 -u 选项的组合。直接来自“man sudo”:
-H The -H (HOME) option sets the HOME environment variable to
the homedir of the target user (root by default) as
specified in passwd(5). The default handling of the HOME
environment variable depends on sudoers(5) settings. By
default, sudo will set HOME if env_reset or always_set_home
are set, or if set_home is set and the -s option is
specified on the command line.
-i [command]
The -i (simulate initial login) option runs the shell
specified in the passwd(5) entry of the target user as a
login shell. This means that login-specific resource files
such as .profile or .login will be read by the shell. If a
command is specified, it is passed to the shell for
execution. Otherwise, an interactive shell is executed.
sudo attempts to change to that user's home directory
before running the shell. It also initializes the
environment, leaving DISPLAY and TERM unchanged, setting
HOME, MAIL, SHELL, USER, LOGNAME, and PATH, as well as the
contents of /etc/environment on Linux and AIX systems. All
other environment variables are removed.
-u user The -u (user) option causes sudo to run the specified
command as a user other than root. To specify a uid
instead of a user name, use #uid. When running commands as
a uid, many shells require that the '#' be escaped with a
backslash ('\'). Note that if the targetpw Defaults option
is set (see sudoers(5)) it is not possible to run commands
with a uid not listed in the password database.
那么,这一切意味着什么呢?第一个有用的选项是 -u,它使命令(在本例中为 shell)以用户 hudson 而不是 root 用户身份运行。-H 选项使主目录在命令持续时间内等于 hudon 的主目录,-i 选项表示模拟用户 hudson 的初始登录(例如源点文件)。总之,这些意味着:使命令在用户 hudson 下运行。由于本例中的命令是 shell,这意味着以用户 hudson 的身份打开 shell,就像您直接以用户 hudson 身份登录一样。
附注:除非您知道正在运行哪个命令,否则不要使用 sudo。如果您不小心,sudo 会给您带来更多弄乱系统配置的机会。这并不是说您无法修复它,但花 5 分钟阅读手册页可以节省以后修复问题的几个小时。
答案2
您可以man sudo
在终端中输入来查看手册。
按k/j可上下滚动。
点击q退出。