I have a script running as a root. It ssh to remote host and run a script as user gol. I have added ssh keys for the user(gol) to remote host. But since the script is run as a root it still prompts for a password. The script content is below

ssh gol@remotehost '/home/scripts/script1.sh'

so i changed the command to switch to local user before running the ssh command (because gol user has ssh keys added) but it fails.

su - gol -c ssh gol@remotehost '/home/scripts/script1.sh'

I need to run this as root as there are other scripts along with this script.

  • It is not clear: what you are trying to achieve, what you have tried, or what went wrong. (I have some ideas, but there are at least two possibilities) Please edit question to make this clearer. May 2, 2019 at 10:17

2 Answers 2


The problem is that by default SSH will try to use root's private key because the client is running as root. If you still want to run as root but want to use gol's private key then this might be possible because root has access to everything. So to call ssh as root but use gol's private key, you only need to tell ssh where the private key is. I don't know your system, but the chances are that it's here:


You can tell ssh which private key to use:

ssh -i /home/gol/.ssh/id_rsa gol@remotehost '/home/scripts/script1.sh'

Alternatively you can use the way you originally tried by quoting it properly:

su - gol -c "ssh gol@remotehost '/home/scripts/script1.sh'"
  • Thx. The second workaround works a treat.My sysadmin also suggested the same.
    – aces
    May 2, 2019 at 10:10
  • 1
    Thanks for the response. Please accept the answer by clicking the tick next to it. ;-) May 2, 2019 at 10:11

I have managed to get this working by adding quotes

su - gol -c "ssh gol@remotehost '/home/scripts/script1.sh'"

Thanks Philip for the update!

I haven't tried Philip workaround as I got this working by adding quotes.

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.