ssh + sudo + su进入登录shell


11

我经常有几台计算机要交换,仅用于将sudo su我的会话的剩余时间用于某些特殊用途的用户。常规工作流程为:

mymachine:~ me$ ssh me@othermachine
othermachine:~ me$ sudo su - specialuser # note: no password needed
othermachine:~ specialuser$ # do stuff

我想将其简化为一个可以别名的直线,因此我可以为每台计算机设置一个别名,并通过单个命令到达需要的位置,而无需键入sudo su - specialuser样板。我可能会成立me@othermachinesudo su上登录,但我想继续作为操作的灵活性me,如果我需要。

请注意:我无法控制othermachine或设置方式;这是我在受聘时进入的既定工作流程。)

我的第一个念头是

ssh me@othermachine "sudo su - specialuser"

这样的作品,但是却没有任何提示,要^C杀死它然后将我注销,我认为其他各种情况也可能是错误的。

在阅读了具有完全登录Shell的Run Remote ssh命令之后,我尝试了一些更奇特的操作,例如

ssh me@othermachine 'bash -l -c "sudo su - specialuser"'

ssh me@othermachine 'bash -l -c "sudo su - specialuser"; bash'

-我都不希望工作,他们也不希望工作,但是我认为我应该尝试使其完整(并避免重复);它们产生的相同提示无壳(具有一个额外的好处提示无壳的第二对meexit从一个用于-ing specialuser)。我试过了

ssh me@othermachine "sudo su - specialuser -c bash -l"

但这只是让我

sudo: no tty present and no askpass program specified

更好的主意?


~/.profile短暂延迟后将sudo命令添加到该怎么办?
亚历克斯(Alex)

我的想法是在不希望的情况下sudo我应该打^C吗?如果我无法提出更好的建议,我可以尝试一下。
大卫·摩尔

1
你可以su mespecialuser。或.profile.bashrc,如果你不遵循sudoexit,你的第一个exit将带你回me,用第二结束会话。甚至使用标志文件,因此sudo由之前[ -f ~/.keep.me ] && del ~/.keep.me和之后[ \! -f ~/.keep.me ] && exit:那么你只需要一个脚本或别名命令me:>~/.keep.me; exit。现在exit将结束您的会话并me返回您的登录会话。
AFH 2015年

1
为了安全起见,请考虑使用最终版本/bin/bash而不是简单的版本bash(以避免trojan)。尤其是如果有sudo之前...
Hastur

Answers:


11

这条线应该适合你

ssh -t me@machine "sudo su - specialuser" 

此解决方案根据-t开关是否提示我

ssh -t me@machine "/bin/bash -l"   # Give me a prompt

ssh  me@machine "/bin/bash -l"     # Give me NO prompt
ssh  me@machine                    # Give me NO prompt

来自的注释 man ssh

-t
强制伪tty分配。这可用于在远程计算机上执行任意基于屏幕的程序,这可能非常有用,例如在实现菜单服务时。 即使ssh没有本地tty,多-t选项也会强制tty分配


这对我有用...也可以通过特定于主机的〜/ .ssh / config设置来完成吗?

1
@where,是的,您可以使用RequestTTY force(等于-t)和RemoteCommand sudo su - specialuser!但是,请注意,其他使用SSH的程序(例如scp,rsync等)将无法再对此主机正常运行。
罗伯特·里德尔
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.