command line method or programmatically add ssh key to github.com user account

Update 2020

As stated in developer changes, Password authentication is going to be deprecated at:

November 13, 2020 at 16:00 UTC

Additionally, as @trysis asked in the comments, we need a solution for 2FA.

The new way is to use a personal access token: enter image description here

For our specific example (adding a ssh key), we only need write permissions (read permissions are added automatically on using write permissions): enter image description here

The updated command (via curl):

curl -H "Authorization: token YourGeneratedToken" --data '{"title":"test-key","key":"ssh-rsa AAA..."}' https://api.github.com/user/keys

This does also work when 2FA is enabled.


OLD

Auth with username and password is supported by github api:

There are three ways to authenticate through GitHub API v3. ...
Basic Authentication
$ curl -u "username" https://api.github.com
...

So just choose a lib in the language you prefer and use the implemented version of the Create a Public Key "Public Key" API Section:

Creates a public key. Requires that you are authenticated via Basic Auth, or OAuth with at least [write:public_key] scope.

INPUT
POST /user/keys

{
    "title": "octocat@octomac",
    "key": "ssh-rsa AAA..."
}

If you want to use it from command line (via curl):

curl -u "username" --data '{"title":"test-key","key":"ssh-rsa AAA..."}' https://api.github.com/user/keys

or even without prompting for password:

curl -u "username:password" --data '{"title":"test-key","key":"ssh-rsa AAA..."}' https://api.github.com/user/keys

here is a nice little tutorial for using curl to interact with github API


Similar to xx4h's answer, this is how I do it in scripts for automating new VM setups.

ssh-keygen -t rsa -b 4096 -C "[email protected]"
curl -u "myusername" \
    --data "{\"title\":\"DevVm_`date +%Y%m%d%H%M%S`\",\"key\":\"`cat ~/.ssh/id_rsa.pub`\"}" \
    https://api.github.com/user/keys

It gives you a new SSH key, includes it in the curl call and gives a unique but still easily identifiable name for each one on the GitHub side (e.g. running now would give DevVm_150602142247).


#!/bin/bash

set -xe
myemail="your-email"

#your personal access token
git_api_token="befdf14c152d6f2ad8cff9c5affffffffffffffffff"

#We'll use the HTTPS to push a ssh key to git, SSH for pull/push configuration
gitrepo_ssh="[email protected]:person/repo.git"
gitrepo_https="https://github.com/person/repo.git"

#Generating SSH key:
ssh-keygen -f "${HOME}/.ssh/id_rsa" -t rsa -b 4096 -C "${myemail}" -N ''
sslpub="$(cat ${HOME}/.ssh/id_rsa.pub |tail -1)"

#git API path for posting a new ssh-key:
git_api_addkey="https://api.$(echo ${gitrepo_https} |cut -d'/' -f3)/user/keys"

#lets name the ssh-key in get after the hostname with a timestamp:
git_ssl_keyname="$(hostname)_$(date +%d-%m-%Y)"

#Finally lets post this ssh key:
curl -H "Authorization: token ${git_api_token}" -H "Content-Type: application/json" -X POST -d "{\"title\":\"${git_ssl_keyname}\",\"key\":\"${sslpub}\"}" ${git_api_addkey}