Import public key from github to local ubuntu account

A short while after posting the question I found the answer. There is a special command ssh-import-id which can import ssh keys. The man page for ssh-import-id explains it quite well. It's hosted as ssh-import-id on launchpad as well.

Basic usage:

# import keys from github
ssh-import-id-gh <username>

I hope this helps others.


GitHub provides this via an API Endpoint that you can download and parse. Unfortunately for us, this does return objects in a JSON format, so it's a bit hard to parse at times (especially in the shell).

Fortunately for us, there's another endpoint to get a user's public SSH keys:

https://github.com/<username>.keys

You can use this with wget to download and append keys really simply and without any parsing:

wget -O - https://github.com/myuser.keys >> /home/myuser/.ssh/authorized_keys

Note that in order to use this command, you will need write permissions to that user's homedir. You may optionally use tee -a as well, which would be easier if privileges aren't guaranteed:

wget -O - https://github.com/myuser.keys | sudo tee -a /home/myuser/.ssh/authorized_keys

Tags:

Ssh

18.04