2

Setting up SSH keys for a new device - GitHub

 3 years ago
source link: https://dev.to/deltanboi/setting-up-ssh-keys-for-a-new-device-github-4f20
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Cover image for Setting up SSH keys for a new device - GitHub

Setting up SSH keys for a new device - GitHub

Sep 4

・3 min read

I got a new laptop and had to go through the pain for setting up my entire dev environment from scratch. After hours of downloading and installing tools I was ready to go. I tried to pull one of my old repos from GitHub and I got the good ol' Permission denied (publickey). That really sucked, not gonna lie, completely forgot about GitHub's SSH keys. Which is why I decided to write this post today.

I remember the first day I ran into this problem, it took me forever to find a solution. I hope this would make it a little easier for someone out there to get up and running in no time.

STEP 1

We'd first start by setting up our own public/private key pair set. This can use either DSA or RSA. Which means basically any key we set up will work, most systems use ssh-keygen.

First, we'd want to enter the .ssh directory. Open the terminal and run this command:

cd ~/.ssh
Enter fullscreen modeExit fullscreen mode

Next, we need to generate our own personalised ssh key, by running this command:

ssh-keygen
Enter fullscreen modeExit fullscreen mode

Now that our key has been generated, we'll need to copy it.

Note: the text that shows up after the command has been run is not what we need.

To copy the key, we'll need to run a command that is specific to whatever OS (and in some cases terminal) is running on the system so pick the one that fits your need.

Windows (Cygwin/Git Bash):

cat id_rsa.pub | clip
Enter fullscreen modeExit fullscreen mode

Windows (PowerShell):

Get-Content id_rsa.pub | Set-Clipboard
Enter fullscreen modeExit fullscreen mode
cat id_rsa.pub | pbcopy
Enter fullscreen modeExit fullscreen mode

Linux:

cat id_rsa.pub | xclip
Enter fullscreen modeExit fullscreen mode

STEP 2

Next, we head over to GitHub to add the key to our account.
When we get there, we log in (if you aren't already logged in). Once logged in we hit the profile icon in the top left corner of the page, this will open a menu like this:

Go ahead and select the Settings option.

In settings there's a side menu on the left, select SSH and GPG keys.

Now we're in the SSH and GPG keys section, we can now add our new SSH key which we copied earlier.
Top right on the page you'll see a button that says New SSH Key, click on it.
We'll get a page like this:

In the Title field we'll give the SSH key a good description so we can know which device it belongs too. I normal use the make and year of the device, e.g. MacBook Pro 2020.

And in the Key field/textarea, that's where we paste the key we copied. Once we've done this, we can now hit the Add SSH Key button.

Once that is done, we'll see the key added to our list of keys:

STEP 3

We now need to configure our .gitconfig.
Open the terminal and run these commands.

To add our username to our git config we'll run:

git config --global user.name "example"
Enter fullscreen modeExit fullscreen mode

Replace "example" with your own username.

To add our email to our git config we'll run:

git config --global user.email [email protected]
Enter fullscreen modeExit fullscreen mode

Replace [email protected] with your own email.

Once this is done, restart the terminal and we should be good to go to.

I hope this was of help 😊.

PS: I know some of you use Personal Access Tokens, maybe I'll make a post for those too


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK