Automatically install SSH public keys from Github

I tend to switch or reimage computers quite frequently, and it's often a pain to add my new SSH key to all machines I want to connect to.

One place I always add the SSH key for a development machine to, is my GitHub account. I noticed that the Ubuntu Server installer allowed you to specify a github account and it would automatically import all the SSH keys from that account to the new computer, without any authentication.

Turns out there's an API endpoint for downloading all the SSH public keys for a given GitHub account. After a little bit of experimenting, I ended up add this to the crontab of all my computers that I need to access remotely:

0 * * * * (curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/users/wvdschel/keys | jq '.[].key' | cut -d'"' -f2; cat ~/.ssh/authorized_keys) | sort | uniq > ~/.ssh/authorized_keys

It adds all the keys from my github account to the SSH authorized keys on the machine at the start of every hour. Note that it only adds keys, so if you want to remove a key, removing it from GitHub won't remove it from the servers.