Import public key from github to local ubuntu account
I'm setting up a new Ubuntu (18.04) server and noticed during install that I could import public keys from github. I've now finished setup and would like to create some more users. It seems ideal to keep importing their public keys from github if possible. Is there a way to trigger that feature of importing keys from a public github profile for a user outside of the installation process?
Solution 1:
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.
Solution 2:
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