[MacOS 10.12.6 host, Ubuntu 14.04 LTS guest]
I have a local VM setup to only accept publickey authentication. Until now, I've been using a key that requires a passphrase to be entered, and the arrangement works fine. However, I now need to setup a script to automatically perform a scp for me.
As I neither want to have the script pause for me to provide the passphrase nor use expect to do that automatically, I've decided to use a second key that does not have a passphrase.
It appears to me that ssh-copy-id is not able to handle this situation as it uses the -i parameter to tell it which key to upload, and therefore I cannot specify which key to login with.
So, my question is, how to get ssh-copy-id accept a second key that would be used to login in the first place? Should I use ssh-add?
(In light of my above hick-up with ssh-copy-id, I've manually copied and added the key to my authorized_keys file.)
3 Answers
ssh-copy-id appends keys to the remote authorized_keys file. To add several specific keys, run it once per key with -I <key-file-name>.
Update
After your comment, I think I got your question wrong.
You want to use one key to authenticate while installing another on your server.
ssh-copy-id does not offer a command-line option to choose a key for authenticating while executing ssh-copy-id.
But it passes -o through to ssh. So:
ssh-copy-id -i ~/.ssh/<your-new-id-to-install> -o 'IdentityFile ~/.ssh/<your-already-existing-id>' <servername>You can also use ssh-agent, in which case you won't have to enter your passphrase when using ssh, scp, ssh-copy-id, a.s.o. for as long as ssh-agent is running.
Using ssh-add to add your key to the agent is definetly an option.
Otherwise I believe you'd have to set up an entry in the .ssh/config file which includes an IdentityFile option, this will tell ssh how to authenticate for that server and allow ssh-copy-id to do its thing.
As others said, I think your best option is to use ssh-agent, like this:
Enable ssh-agent
eval "$(ssh-agent -s)"Note the
ssh-agent -scommand starts the SSH agent and generates environment variables (that's why we feed the output to theevalcommand).Load the 1st key (the one you currently use to log into the remote system)
ssh-add <1st-key>.pubRun the
ssh-copy-idcommand and pass the new (2nd) key as a parameterssh-copy-id -i <2nd-key>.pub <user>@<host>Et voilà... you have your 2nd key copied (ssh-copy-id used the ssh-agent transparently to authenticate with the first key).
[OPTIONAL] You may now shut down the ssh-agent if you wish:
eval "$(ssh-agent -k)"