简体   繁体   English

在远程服务器上使用 ssh 执行 sudo su 不起作用

[英]Doing sudo su with ssh on a remote server not working

This seems to be a popular question on stackoverflow but nothing seems to be working for me这似乎是关于 stackoverflow 的一个流行问题,但似乎对我没有任何作用

I will explain my problems first and then go the the solutions I have tried我会先解释我的问题,然后去我尝试过的解决方案

What I need to do is to ssh to serverB from serverA .我需要做的是sshserverBserverA for this I have set up an rsa encryption on the servers and I can successfully ssh to serverB为此,我在服务器上设置了 rsa 加密,我可以成功 ssh 到 serverB

I use我用

ssh user@hostname

Now I want execute certain commands on serverB .现在我想在serverB上执行某些命令。 The first one is to switch to app user.第一个是切换到app用户。 For this I need to run sudo su - app command but I also want to provide the password in the same line so that it doesnt prompt for the password again.为此,我需要运行sudo su - app命令,但我也想在同一行中提供密码,以便它不会再次提示输入密码。

So I have tried to first directly run sudo su - app command on serverB with password to test it out所以我尝试首先使用密码在serverB上直接运行sudo su - app命令来测试它

I have tried the following我已经尝试了以下

echo "password" | sudo su - app

sudo -S  <<< "password" su - app

echo "password" | sudo -S su - app

echo 'passowrd' | sudo 'su -c - app'

However none of the above solutions work for me.但是,上述解决方案都不适用于我。

The closest I could get was with我能得到的最接近的是

echo "password" | script -c "sudo su - app"

where it accepts the password and shows me它接受密码并显示给我的地方

app@hostname [/app]
$

however when I run the command whoami it still shows me user instead of app .但是,当我运行whoami命令时,它仍然向我显示user而不是app however when I directly run sudo su - app and the provide pass and then run whoami it gives me app但是,当我直接运行sudo su - app和提供传递然后运行whoami它会给我app

I am trying to run command with ssh like我正在尝试使用 ssh 运行命令,例如

ssh user@hostname -t 'echo "password" | script -c "sudo su - app"'

PS the user user doesnt have root access and also I cannot make use of any plugin as I don't have permission to do the same PS用户user没有root访问权限,而且我也不能使用任何插件,因为我无权这样做

My server is Redhat 6.2我的服务器是Redhat 6.2

I hope I could explain it properly.我希望我能正确解释它。 Looking for some answers that can help.寻找一些可以提供帮助的答案。

Sorry for my bad English.对不起,我的英语不好。 Thanks for help.感谢帮助。

If we set up ssh using rsa key encryption then we don't need to use the password.如果我们使用 rsa 密钥加密设置 ssh,则不需要使用密码。

In order to enable ssh with public/private key I follow为了使用公钥/私钥启用 ssh,我遵循

  1. Genrate the public/private ke y for user on serverA为 serverA 上的用户serverA public/private ke

     ssh-keygen -t rsa
  2. Go to .ssh/id_rsa.pub file and copy the public key转到.ssh/id_rsa.pub文件并复制公钥

  3. Login to ServerB and then do sudo su - app to change to app user.登录到ServerB ,然后执行sudo su - app更改为应用程序用户。 Here in the file .ssh/authorized_key copy the public key.在文件.ssh/authorized_key复制公钥。

  4. Try ssh to serverB now from serverA like试着sshserverB从现在serverA

    ssh app@hostnameServerB

It works without asking for a password.它无需密码即可工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM