This seems to be a popular question on stackoverflow but nothing seems to be working for me
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
. for this I have set up an rsa encryption on the servers and I can successfully ssh to serverB
I use
ssh user@hostname
Now I want execute certain commands on serverB
. The first one is to switch to app
user. 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.
So I have tried to first directly run sudo su - app
command on serverB
with password to test it out
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
. however when I directly run sudo su - app
and the provide pass and then run whoami
it gives me app
I am trying to run command with ssh like
ssh user@hostname -t 'echo "password" | script -c "sudo su - app"'
P.S. 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
My server is 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.
In order to enable ssh with public/private key I follow
Genrate the public/private ke
y for user on serverA
ssh-keygen -t rsa
Go to .ssh/id_rsa.pub
file and copy the public key
Login to ServerB
and then do sudo su - app
to change to app user. Here in the file .ssh/authorized_key
copy the public key.
Try ssh
to serverB
now from serverA
like
ssh app@hostnameServerB
It works without asking for a password.