我有几台机器,我定期 ssh 进入,只是为了使用sudo su我的会话的其余部分作为某个特殊用途的用户登录。一般工作流程是:
mymachine:~ me$ ssh me@othermachine
othermachine:~ me$ sudo su - specialuser # note: no password needed
othermachine:~ specialuser$ # do stuff
Run Code Online (Sandbox Code Playgroud)
我想把它归结为一个可以别名的单行代码,这样我就可以为每台机器设置一个别名,然后在一个命令中到达我需要的地方,而无需键入sudo su - specialuser样板。我也许me@othermachine可以sudo su在登录时设置为,但我想保持操作的灵活性,就像me我需要的那样。
(注意:我无法控制othermachine或控制它的设置方式;这是我在受雇时采用的既定工作流程。)
我的第一个想法是
ssh me@othermachine "sudo su - specialuser"
Run Code Online (Sandbox Code Playgroud)
和这种工作,但它没有提示我,^C杀死它并将我注销,我认为其他各种事情也可能是错误的。
在阅读Run Remote ssh command with Full Login Shell 之后,我尝试了一些更奇特的东西,比如
ssh me@othermachine 'bash -l -c "sudo su - specialuser"'
Run Code Online (Sandbox Code Playgroud)
和
ssh me@othermachine 'bash -l -c "sudo su - specialuser"; bash'
Run Code Online (Sandbox Code Playgroud)
——这两个我都不希望工作,他们没有,但我认为我应该尝试它们的完整性(并避免重复);他们生成了相同的无提示外壳(第二个带有额外的无提示外壳 for meafter exit-ing 来自 for specialuser)。我试过
ssh me@othermachine "sudo su - specialuser -c bash -l"
Run Code Online (Sandbox Code Playgroud)
但它只是让我
sudo: no tty present and no askpass program specified
Run Code Online (Sandbox Code Playgroud)
更好的想法?
Has*_*tur 12
这条线应该适合你
ssh -t me@machine "sudo su - specialuser"
Run Code Online (Sandbox Code Playgroud)
这个解决方案给我一个提示或不取决于-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
Run Code Online (Sandbox Code Playgroud)
笔记来自 man ssh
-t
强制伪 tty 分配。这可用于在远程机器上执行任意基于屏幕的程序,这非常有用,例如在实现菜单服务时。 多个 -t 选项强制 tty 分配,即使 ssh 没有本地 tty。