i try to run some script located on servers specified in list but i have a problem with log in as specified user. To clarify it I log in to server1 as user0 then run this script and I except the script will be switch my user0 to user1 and as user1 perform action from lines below for servers specified in list:

[user0@server1 home]$ /home/user0/my_script.sh

sudo su - user1
#SERV=(serv1 serv2 serv3)

for HOST in ${SERV[@]}; do
   ssh -t user1@${SERV} 'cd /opt/directory1/directory2/var; sudo /opt/custom/scripts/script.sh'

    if [[ $?=0 ]]; then
        echo "Commnad on $SERV executed!"
        echo "$SERV do not exist!"

The proble is with switch user from user which run script to user which will be perform ssh ( user1 have included rsa keys ) but script always ask me about password for user1


If you want user0 to be able to do ssh user1@${SERV} without being prompted for password, then you need user1 to trust user0. In other words, user0's public key needs to be added to user1's authorized_keys file. You can do this by running

ssh-copy-id user1@${SERV}

for each server in that you want user0 to have access as user1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.