Skip to content

Instantly share code, notes, and snippets.

@mildronize
Forked from my-wrm/README.md
Last active November 20, 2021 08:43
Show Gist options
  • Save mildronize/77cf42fbbca24719974e4d8f14f09a67 to your computer and use it in GitHub Desktop.
Save mildronize/77cf42fbbca24719974e4d8f14f09a67 to your computer and use it in GitHub Desktop.
Setup SSH keys for use with GitHub/GitLab/BitBucket etc

Create a new repository, or reuse an existing one.

Generate a new SSH key:

ssh-keygen -t rsa -C "your_email@example.com"

Copy the contents of the file ~/.ssh/id_rsa.pub to your SSH keys in your GitHub account settings (https://github.com/settings/keys).

Test SSH key:

$ ssh -T git@github.com
Hi developius! You've successfully authenticated, but GitHub does not provide shell access.

Change directory into the local clone of your repository (if you're not already there) and run:

git remote set-url origin git@github.com:username/your-repository.git

Now try editing a file (try the README) and then do:

$ git commit -am "Update README.md"
$ git push

You should not be asked for a username or password. If it works, your SSH key is correctly configured.

For Windows 10

Ref: https://newbedev.com/how-to-run-ssh-add-on-windows

Make sure enable OpenSSH on Windows 10

OpenSSH is available as part of Windows 10 which makes using SSH from cmd/powershell much easier in my opinion. It also doesn't rely on having git installed, unlike my previous solution.

  1. Open Manage optional features from the start menu and make sure you have OpenSSH Client in the list. If not, you should be able to add it.

  2. Open Services from the start Menu

  3. Scroll down to OpenSSH Authentication Agent > right click > properties

  4. Change the Startup type from Disabled to any of the other 3 options. I have mine set to Automatic (Delayed Start)

  5. Open cmd and type where ssh to confirm that the top listed path is in System32. Mine is installed at C:\Windows\System32\OpenSSH\ssh.exe. If it's not in the list you may need to close and reopen cmd.

  6. Optional step/troubleshooting: If you use git, you should set the GIT_SSH environment variable to the output of where ssh which you ran before (e.g C:\Windows\System32\OpenSSH\ssh.exe). This is to stop inconsistencies between the version of ssh you're using (and your keys are added/generated with) and the version that git uses internally. This should prevent issues that are similar to this

Some nice things about this solution:

  • You won't need to start the ssh-agent every time you restart your computer
  • Identities that you've added (using ssh-add) will get automatically added after restarts. (It works for me, but you might possibly need a config file in your c:\Users\User.ssh folder)
  • You don't need git!
  • You can register any rsa private key to the agent. The other solution will only pick up a key named id_rsa

How to Setup (Powershell)

Create a new repository, or reuse an existing one.

Generate a new SSH key:

ssh-keygen -t rsa -C "your_email@example.com" -f $env:UserProfile/.ssh/id_rsa

Copy the contents of the file ~/.ssh/id_rsa.pub to your SSH keys in your GitHub account settings (https://github.com/settings/keys).

Test SSH key:

PS> ssh -i ~/.ssh/id_rsa -T git@github.com
Hi mildronize! You've successfully authenticated, but GitHub does not provide shell access.

Change directory into the local clone of your repository (if you're not already there) and run:

git remote set-url origin git@github.com:username/your-repository.git

Now try editing a file (try the README) and then do:

$ git commit -am "Update README.md"
$ git push

You should not be asked for a username or password. If it works, your SSH key is correctly configured.

How to make Powershell remember the SSH key passphrase.

https://gist.github.com/danieldogeanu/16c61e9b80345c5837b9e5045a701c99

Troubleshooting

SSH-Keygen "no such file or directory"

https://newbedev.com/ssh-keygen-no-such-file-or-directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment