Monday, July 22, 2024

GitHub and SSH. How Do I Make It Work?

 If you have a GitHub repo but haven't used it in a while, or if you're just setting one up, you may have noticed the old username/password approach is not an option anymore when trying to push from your local repo.

My weapon of choice (at least, for Java development) is Eclipse, and I wanted to be able to push/pull and all that good stuff using the UI.  There are a few gotchas to that, so here's how to set up ssh with your GitHub account.

First, you'll want to create your RSA key.  This is straightforward.  Just open your favorite console application and type:

ssh-keygen -t ed25519 -C "youremailaddress@somemailprovider.com"

And go through the questions it prompts you.  You DO want to create a passphrase when it asks you.

Now in your user home folder will be a folder called .ssh.  (It may be hidden.)  Go into that folder and you'll see the public key you just created.  (Hint: It's the file that ends in .pub)  Open that up in a text editor and copy the contents of that file onto your clipboard.  An easier way to do that that ensures you won't accidentally pick up any spaces in your clipboard is the command:

clip < filename.pub

This does the same thing.

Now log into your GitHub account.  On your account page, click the little icon with your picture in the upper right corner and select "Settings."



On the left of the settings page, select "SSH and GPG keys."


Click the "New SSH Key" button.


Give it a title and then copy the contents of your clipboard into the "Key" text area.  Finish up by clicking the "Add SSH Key" button.

As Bon Jovi would say, we're halfway there.


Now we need to configure Eclipse.

Open up Eclipse and select "Preferences" under the "Window" menu.


Under General > Network Connections select "SSH2."


The SSH2 Home field should already be populated, if not, make sure it points to the .ssh folder in your home directory (Or, if you like to be a rebel, point it to whatever folder you put your new RSA key in instead of the default.  I won't judge.)

Hit the "Add Private Key..." button and use it to pick your private RSA key.  That's the file that matches the name of the public key but without the ".pub" extension.

Under the Authentication Methods tab, make sure publickey is checked.


Now, back to the list of Preferences.  Select Version Control (Team) > Git.

Make sure "Store SSH key passphrases in secure store" is checked.  The first time you connect to your GitHub repo it will prompt you for the passphrase you gave it when creating the RSA key.  After that, it will be secured in secure store and won't ask you for it again.



Not done yet...

When you first connect to a GitHub repo you'll have some configuration to do on the connection protocol.  If this is a repo that already exists,  you can get to those settings by:

Right click the project in the Package Explorer window.

 Select Team > Remote > Configure push to upstream...



In the line where it shows the URI, click "Change..."


Under Connection, change the Protocol setting to ssh.  Under Authentication, make the username "git."

I know that isn't your GitHub username.  Trust me.  Put "git" in there anyway.


Now test it!



It isn't very complex, but there are some gotchas.  Follow these steps and you should be good to go.


 


No comments:

Post a Comment