I have a script that runs and will ssh into another server to restart its dhcpd.

In the script, the command literally is:

sudo ssh -n $SERVER /sbin/service dhcpd restart

This works fine when the script is called, but when I try to manually run the command myself at the command line, it prompts and waits for me to input the root password.

How does it not prompt for the root password simply because the command was called from within the script?

I've also checked to ensure I was running both as the same user. I didn't create the script, so I'm not sure if there is some magic going on when the script gets called that allows this type of thing.


You must set the SSH based on keys in your situation. Whatever it's called from, the SSH tool itself just reads the config AND keys, AND if the remote server accepts key-only auth, you won't have a password prompt. Just make a key on a remote host under the user you wish to execute your command, enable key-based auth, on your local host AND make "su" to the user you will invoke this string under(regardless of the method : cron, shell, etc. ) - and insert the remote key locally as a key for this host. That's it!

  • I think I'm still confused. If the script can run the command without authentication as userA, that must mean the SSH keys already exist for userA and the server is already setup to allow key only authentication. So then why would running the exact same command from command line as userA not do the same thing? – krb686 May 27 '15 at 17:17
  • Yes, keys must exist and the server must be configured like that. In your case userA exist on REMOTE host. Yes, sudo can be configured to run some command without password, but ssh cares about it's config ONLY. You can specify which config to use via command line, if you doubt the user-switching process in causing you a command-prompt issue. – Alexey Vesnin May 31 '15 at 14:02

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.