“Sudo -Hiu”有什麼用?

“Sudo -Hiu”有什麼用?

我按照教學安裝 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退出。

相關內容