I know I can do the following to copy an SSH key
ssh-copy-id user@server.com
but when I deploy a new server, I need to authorize multiple SSH keys to it. Is there anyway to make life easier for authorizing multiple SSH keys?
I know I can do the following to copy an SSH key
ssh-copy-id user@server.com
but when I deploy a new server, I need to authorize multiple SSH keys to it. Is there anyway to make life easier for authorizing multiple SSH keys?
Reading the man-page of ssh-copy-id
, it seems that it should copy all the keys from the ssh-agent that aren't accepted for login. So, load all the keypairs into ssh-agent using ssh-add
and use ssh-copy-id
after that.
If that doesn't work, you can try specifying the keys for ssh-copy-id
:
for i in ~/.ssh/*.pub; do
ssh-copy-id -i $i user@host
done
If that still doesn't work for you, maybe take a look at something like ansible:
# sshkeys.yml
---
- hosts: all
remote_user: foo
tasks:
- authorized_key:
user: foo
key: '{{lookup("file", item)}}'
with_items:
- id_rsa.pub
- id_rsa_alternate.pub
Use like:
ansible-playbook -i remote.host.com, -k sshkeys.yml
Be sure to read more about ansible if you choose to go this route.
This way beats ssh copy id by miles as you can copy the keys to any user, for an ssh server with any port, not just 22. oh and u can have multiple keys in your authorized_keys.org that will get appended to the authorized_keys file on the server. Make sure authorized_keys.org has one ssh public key per line.
There are many ways you can construct these lines to your likeing. I suggest never forgetting this cat | ssh cat
method or the similar echo | ssh ssh cat
method, as they are perfect for writing on remote servers. cat SOMETHING | ssh SERVER "cat - > REMOTEFILE"
or you can use echo like so echo "SOMETHING" | ssh SERVER "cat - > REMOTEFILE"
these are perfect methods to read in something locally & then write it or append it on another server. Write it like this cat - > REMOTEFILE
or append it like this cat - >> REMOTEFILE
cat authorized_keys.org | ssh -p 22 root@server.com "cat - >> /root/.ssh/authorized_keys"
or
cat authorized_keys.org | ssh -p 22 root@server.com "cat - >> ~/.ssh/authorized_keys"
Also if you have the ssh key (one ssh key):
cat id_rsa.pub | ssh -p 22 root@server.com "cat - >> /root/.ssh/authorized_keys"
or
cat id_rsa.pub | ssh -p 22 anyuser@server.com "cat - >> ~/.ssh/authorized_keys"
That should be enough to make sense of it all.
NOTE: Read my comments below on the fact that your not limited to using "root". Here you can see all parts of it are modifyable:
[input of ssh keys or keys (as long as new line sepearted)] | ssh -p PORT USER@SERVER "cat - >> AUTHORIZEDKEY"
BONUS: lets say that ssh server doesnt allow password logins and only allows SSH keys, yet you want to let it know about more key[s].
[input of ssh keys or keys (as long as new line sepearted)] | ssh -i PRIVATE -p PORT USER@SERVER "cat - >> AUTHORIZEDKEY"