【问题标题】:Can you use capistrano if you need to sudo su into another user?如果您需要 sudo su 进入另一个用户,您可以使用 capistrano 吗?
【发布时间】:2013-05-10 20:54:45
【问题描述】:

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

【问题讨论】:

  • 我在 Capistrano 和那边的人谈过了。显然这是不可能的。我让我的系统管理员更改了我们的全局 sudoer 文件以允许 sudo -u 并修复它。谢谢大家。
  • 我希望这能回答你的问题:stackoverflow.com/questions/22054076/…

标签: ruby-on-rails ruby-on-rails-3 deployment capistrano sudo


【解决方案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 -u your_user mkdir some_folder,而不是运行sudo mkdir some_folder,它会以该用户的身份执行所有命令。

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

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

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

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

【讨论】:

  • 不幸的是,这对很多事情都不起作用。例如,当它尝试运行 rake assets:precompile 命令时,它会以您登录的用户身份运行它。但是由于 gems 是安装在共享用户帐户下的,所以它失败了。
【解决方案2】:

查看以下答案:

How can I switch user in a Capistrano task?

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

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

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

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 2021-11-14
    • 2010-10-31
    • 2020-11-15
    • 2020-07-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    相关资源
    最近更新 更多