3

我正在尝试让 capistrano 参与我的团队项目。由于部门限制,我们必须以自己的身份登录,然后 sudo su 进入共享项目用户。我们进入的那个项目用户sudo su - projectuser具有访问项目目录所需的权限。我很难弄清楚如何使用 capistrano 进行这项工作。任何帮助,将不胜感激。

4

2 回答 2

1
man sudo

   -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.

   -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.

Sudo 已经支持以特定用户而不是 root 身份运行命令。因此sudo mkdir some_folder,您可以简单地运行而不是运行sudo -u your_user mkdir some_folder,它只会以该用户身份执行所有命令。

此外,该-i标志将指示 sudo 启动新的 shell 并运行命令。新的 shell 将像登录 shell 一样被初始化(它将切换到用户的主目录,重新初始化 HOME 和其他变量等)。它最接近运行sudo su - your_user

要使用 Capistranosudo -u <your-user> -i而不是仅使用sudo,请在您的 中设置以下配置deploy.rb

set :use_sudo, true
set :sudo, "sudo -u <your-user> -i"

Capistrano 现在将使用给定用户执行所有 sudo 命令,并使用全新的登录 shell。

于 2013-06-24T07:18:15.273 回答
0

查看以下答案:

如何在 Capistrano 任务中切换用户?

当然,您可能希望修改该代码以执行sudo su - projectusersudo 而不是简单的 sudo 到 root。

我自己从来没有这样做过,但它似乎会奏效。您也可以使用它进行测试cap shell。就个人而言,我建议cap shell先玩一下。

此外,这里还有另一个选项,但我认为上面的原始链接提供了更好的工作机会。

于 2013-06-19T15:40:21.647 回答