部署 sshkit 怎么以 root 用户登录执行操作?

wind0550 · 2014年07月16日 · 最后由 wppurking 回复于 2014年07月24日 · 3519 次阅读

请教一下大家啊,我用 sshkit 登录 aws ec2 之后,怎么才能切换到 root 用户执行操作呢?

我是在写一个脚本,用 sshkit 登录到 linux vm 之后,从 git 上面更新代码,然后重启系统服务。但切换到 root 的时候 sshkit 会出错。

哪位有解决办法的指导一下!谢了

没有把你的用户加入到 sudo 组?

已经加了,正常用户登录用 sudo 没有问题。

这是我的 code:

on hosts, in: :parallel do |host| as('root') do blah blah blah... end end

下面是运行时 sshkit 遇到的错误:

/usr/local/rvm/gems/ruby-2.0.0-p353/gems/sshkit-1.5.1/lib/sshkit/command.rb:97:in `exit_status=': Exception while executing on host ec2-54-89-162-48.compute-1.amazonaws.com: if ! sudo -u root whoami > /dev/null; then echo "You cannot switch to user 'root' using sudo, please check the sudoers file" 1>&2; false; fi exit status: 1 (SSHKit::Runner::ExecuteError) if ! sudo -u root whoami > /dev/null; then echo "You cannot switch to user 'root' using sudo, please check the sudoers file" 1>&2; false; fi stdout: Nothing written if ! sudo -u root whoami > /dev/null; then echo "You cannot switch to user 'root' using sudo, please check the sudoers file" 1>&2; false; fi stderr: sudo: sorry, you must have a tty to run sudo

不知是不是这个问题:sshkit 使用 non-interactive shell,sudo 需要密码,无法输入密码然后失败了。

那我用 sshkit 怎么才能执行需要 sudo 权限的命令呢?比如说重启 nginx service

#4 楼 @wind0550 根据文档 http://capistranorb.com/documentation/getting-started/authentication-and-authorisation/#toc_8

capistrano 希望不要做需要 root 权限的事,如果无法避免,给 sudo 某些命令免密码,但是这会带来风险。

能不能避免 sudo 呢?例如 unicorn/thin 等进程是用用户身份启动的可以用用户身份管理,passenger 是通过 touch 文件触发重启的。

比如说 nginx+passenger 的环境,sshkit 登录后用 git 更新了代码后,怎样重启 nginx 服务器呢?

大家没有遇到过类似的场景吗?

#6 楼 @wind0550 Passenger 重启只要 touch 项目目录的 tmp/restart.txt 文件就行了。

@Rei 这个太好了,我试试看。多谢!

那是不是说 sshkit 或者 capistrano 都应该避免做需要 root 权限的事情?有没有避免不了的情况?

#10 楼 @wind0550 以所需要登陆的服务器为 192.168.1.120 为例子,服务器上有两个账户:root 与 :foo 那么想使用 root 用户操作,则:

require 'sshkit' # sshkit 的所有库
require 'sshkit/DSL'  # sshkit 提供的两个简单 dsl

# 区别在于将用户名字写在 server 路径中
on '[email protected]' do |host|
    execute 'whoami'
    pwd = capture('pwd')
    puts "#{pwd} have #{pwd.size} words"
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号