Last active
November 2, 2024 05:49
-
-
Save juanique/4092969 to your computer and use it in GitHub Desktop.
Bash script to add a new SSH key to your GitHub account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rm -rf ~/.ssh/id_rsa ~/.ssh/id_rsa.pub | |
| read -p "Enter github email : " email | |
| echo "Using email $email" | |
| ssh-keygen -t rsa -b 4096 -C "$email" | |
| ssh-add ~/.ssh/id_rsa | |
| pub=`cat ~/.ssh/id_rsa.pub` | |
| read -p "Enter github username: " githubuser | |
| echo "Using username $githubuser" | |
| read -s -p "Enter github password for user $githubuser: " githubpass | |
| curl -u "$githubuser:$githubpass" -X POST -d "{\"title\":\"`hostname`\",\"key\":\"$pub\"}" https://api.github.com/user/keys |
Author
Is there a way to check if the user uses two-factor auth and only ask for it if they do? ideally the script should cover both cases. If you can take care of that part I'll update the gist to your version which looks much better :)
I made a little script that also does this but allows you to keep several names at once, it also contains some more explanation and walkthrough on the page. Hopefully this is useful to someone, as I found this process quite opaque when I first had to do it for my private repositories:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i found the
rm -rfa bit aggressive, wanted to use existing keys if present, and further support two-factor-auth. here's my flavor! thx for the boilerplate :)