0

Hello I am trying to figure out how to make ssh use a different key id. I can ssh into the server fine but if I have a script that is run with sudo privileges I get prompted for the password. I can get around this by copying my ssh keys into roots .ssh folder but I know there is a better way to do this I just can't seem to find the answer. Any suggestions would be greatly appreciated.

EDIT: I have already tried setting sudo to keep environmental variables ie. HOME

5
  • 1
    It seems to me that you may want your script to use sudo -u youruser ssh ... to do the ssh command. Root shouldn't really be used to access other systems. You could obviously also just use the -i option to ssh to pick up the correct key, but you never say why this isn't an option.
    – Kusalananda
    Mar 7, 2021 at 20:01
  • I guess I could use the -i flag I had this working before re installation I just can't remember how we had it done before, until then thanks for the reminder of the -i flag. I am fairly certain there is a configuration of maybe it was a variable you can set to have ssh use the same key for everything. Mar 7, 2021 at 20:07
  • 1
    The issue here is not so much to get ssh to "use the same key for everything", but the fact that you are using different users to do things (your ordinary user, and root). Things like SSH keys are dependent on the user you're using ssh as.
    – Kusalananda
    Mar 7, 2021 at 20:09
  • Yeah I guess I'll just use your original idea of the -i flag I know we did this differently I just cant remember but it works if it comes to be Ill come back and leave a message thanks. Mar 7, 2021 at 20:25
  • 1
    The -i key is the way to do what you want. You can specify the same option, called identity_file, in the ssh config file. No version of SSH I've used in the past 20 years or so allows you to do anything like this with an environment variable, though you could put identity_file=${SOMEENVVAR} in the config file and then set the appropriate var.
    – alzee
    Mar 7, 2021 at 20:27

1 Answer 1

0

Hello I am trying to figure out how to make ssh use a different key id.

Either use -i, or set up ~/.ssh/config to mention a certificate. The last one needs to happen in the HOME directory of the current user.

I can ssh into the server fine but if I have a script that is run with sudo privileges I get prompted for the password

I agree with Kusalananda to (1) either switch back to the user, (2) don't run the whole script as sudo in the first place, but use sudo inside the script (which depends of course on what the script does, which you didn't tell us anything about).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .