【问题标题】:Using Net::SSH Ruby library to remote command execution that requires sudo su - another user使用 Net::SSH Ruby 库远程执行需要 sudo su 的命令 - 另一个用户
【发布时间】:2012-12-01 09:54:11
【问题描述】:

我的环境为用户配置了 SSH 无密码身份验证 节点服务器(Rundeck 服务器)和节点目标(远程 Solaris 主机)之间的“主”。

在 Target 上,我想用用户 appmanager 执行脚本 /app/acme/stopApp.sh

通常,当我需要手动运行脚本时,我会使用:

ssh master@server sudo su - appmanager

或者简单地说:

ssh -t master@server 'sudo su - appmanager'

无需密码即可运行并最终运行(作为 appmanager):

/app/acme/stopApp.sh

但我无法弄清楚如何使用 Net::SSH 重现这些步骤。 当我执行sudo su - appmanager 然后/app/acme/stopApp.sh, 我是在子shell中做的,对吧?

require 'rubygems'
require 'net/ssh'
require 'net/scp'
require 'crypt/blowfish'
require 'yaml'

#
# ...
#

Net::SSH.start( host, user, :password => password ) do |session|

  # It's possible to proceed in this way?
  cmd = 'sudo su - appmanager;/app/acme/stopApp.sh'  
  ses = session.exec!( cmd )

end

我意识到,如果我尝试在目标服务器上执行类似的操作:

sudo su -c /app/acme/stopApp.sh appmanager

我收到以下消息:

We trust you have received the usual lecture from the local System Administrator. 

It usually boils down to these three things: 

 #1) Respect the privacy of others. 
 #2) Think before you type. 
 #3) With great power comes great responsibility. Password:

Password:

【问题讨论】:

  • 感谢铁皮人对文本的改进。我不是以英语为母语的人,所以你的行为可以帮助我更多地了解这种语言。

标签: shell ssh remote-access net-ssh rundeck


【解决方案1】:

这有点像系统管理员的回答,但我认为您要进行两次身份验证:一次以“master”身份登录(使用 master 的密钥对),然后第二次“master”sudo-ing su 到“ appmanager”,但使用密码(因此是“讲座”消息)。但我认为你没有第二次回答密码挑战。有几种方法可以解决这个问题:

1) 使用该帐户的密钥对直接以 appmanager 身份登录。如果您担心 appmanager 帐户的安全性,可以restrict ssh remote commands 等。

2) 作为 master,调用一个二进制文件 (not a script!),即 setuid 作为“appmanager”,它只是调用 stopApp.sh 脚本。一个example

3) 在 /etc/sudoers 中将 master 所在的相应组设置为 NOPASSWD

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2022-06-06
    • 2011-09-05
    • 2017-06-10
    • 2017-05-14
    相关资源
    最近更新 更多