“Sudo -Hiu”是什么?

1 linux sudo hudson ubuntu

我按照教程安装了 Hudson 服务器。在教程中有以下 Linux 命令(hudson是 Ubuntu 中的用户名):

sudo -Hiu hudson
Run Code Online (Sandbox Code Playgroud)

sudo -Hiu为了什么?执行此命令后会发生什么?

小智 6

-Hiu 标志是 -H、-i 和 -u 选项的组合。直接来自“男人 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.
Run Code Online (Sandbox Code Playgroud)

那么,这意味着什么?第一个有用的选项是 -u,它使命令(在本例中为 shell)以用户 hudson 而非 root 用户身份运行。-H 选项使主目录在命令期间等于 hudon 的主目录,-i 选项表示模拟用户 hudson 的初始登录(例如源点文件)。总之,这些意味着:使命令在用户 hudson 下运行。由于本例中的命令是 shell,这意味着以用户 hudson 的身份打开一个 shell,就像您直接以用户 hudson 的身份登录一样。


附带说明:除非您知道正在运行哪个命令,否则不要使用 sudo。如果你不小心,sudo 会给你更多的机会来弄乱你的系统配置。这并不是说您无法修复它,但是花 5 分钟阅读手册页可以节省以后修复问题的时间。