DEV Community

Cover image for Unlocking Seamless Development: Mastering SSH Key Setup for Bitbucket and GitHub in WSL2
Ian A. Drilon
Ian A. Drilon

Posted on

Unlocking Seamless Development: Mastering SSH Key Setup for Bitbucket and GitHub in WSL2

Why use ssh?

Using SSH as more secure option and HTTPS for basic, password-based Git usage. Since SSH is more secure than entering credentials over HTTPS, it is recommended for business dealing with sensitive and critical data. Once you generate you generate and connect your ssh key to your bitbucket/github, you can freely push and pull or make changes to your project repository

Why i'm doing this? πŸ€”πŸ€”

As your fellow developer, i was once struggling to setup my own SSH key to the version control that we are using in a company, by studying and discovering it on my own. I want to share how i setup my own SSH key and connect to the repository with no hassle.

Let's get into it πŸ”₯

  • Make sure openssh-server is installed in your WSL Distro (In my own case i use Ubuntu.22.04 LTS)
 sudo apt install openssh-client
Enter fullscreen mode Exit fullscreen mode
  • Once the openssh-client installation is complete, in the terminal, check that openssh-client has been successfully installed by running the following command:
ssh -V
Enter fullscreen mode Exit fullscreen mode

the output will show the version of openssh you installed

Start the SSH agent

To allow git to use your SSH key, an SSH agent needs to be running in your machine. to do this, write this command in your terminal

ps -a | grep ssh-agent
Enter fullscreen mode Exit fullscreen mode

to start the agent:

eval $(ssh-agent)
Enter fullscreen mode Exit fullscreen mode

you might need this to add in your ~/.bashrc so that if you restart your terminal it will automatically run in the background

Create an SSH key πŸ”‘ pair (private/public)

  • Open your terminal and navigate to your home by writing this command
cd ~
Enter fullscreen mode Exit fullscreen mode
  • Generate your SSH key pair using ssh-keygen, write this command:
ssh-keygen -t ed25519 -b 4096 -C "username@emaildomain.com" -f ssh-key-name
Enter fullscreen mode Exit fullscreen mode
  • The username@emaildomain.com is the email you are using in bitbucket or github

  • the ssh-key-name is the name of you ssh key example work-ssh

After adding those command, the terminal will ask for you passphrase, you can either provide a phrase for a password or leave it blank, if you input a password each time SSH is used, such as using git command that contact Bitbucket/Github (such as, pull and fetch). Providing password will prevent other users with access to the device from using your keys.

Once you completed the command above, it will output two files such as

  • ssh-key-name - the private key
  • ssh-key-name.pub - the public key

These files will be stored in your home directory, the ssh-key-name.pub that is the one we will be using to connect in bitbucket/github

Add your key πŸ”‘ to the SSH agent

We will now add our key to the ssh agent, write this command:

ssh-add ~/ssh-key-name
Enter fullscreen mode Exit fullscreen mode

Ater adding our ssh key to ssh agent, we will now be able to add our ssh public key to the bitbucket/github

Provide Bitbucket Cloud with your public key πŸ”‘

  1. At bitbucket, select your avatar (Your profile and settings) from the upper right screen of your monitor

  2. Under Settings, Select Personal Settings

  3. Under Security tab, select SSH key

  4. in the Add SSH Key it will prompt you a dialog box, where in you can add your ssh key example:

ssh-ed25529 LLoWYaPswHzVqQ7L7B07LzIJbntgmHqrE40t17nGXL71QX9IoFGKYoF5pJKUMvR+DZotTm user@example.com
Enter fullscreen mode Exit fullscreen mode

you will find this key inside of your ssh-key-name.pub file just copy and paste, don't forget your label. For example Work Machine. this will help you identify old or unwanted keys in the future.

after adding your ssh public key, if you received an error that SSH key is invalid, double check that you copied the entire contents of the public key (.pub file)

Check that your SSH authentication works

To test your ssh key if successfully, open a terminal on your machine and add this command

ssh -T git@bitbucket.org
Enter fullscreen mode Exit fullscreen mode

if SSH can successfully connect with Bitbucket using your SSH keys, the command will produce output similar to

authenticated via ssh key.

You can use git to connect to Bitbucket. Shell access is disabled
Enter fullscreen mode Exit fullscreen mode

Provide Github with your public key πŸ”‘

Adding SSH key in your github is more simple than you expected

  • Copy your SSH public key in your .pub file

  • In the upper right corner in your github profile you can click your avatar, and click the Settings

github settings

  • In the SSH and GPG keys Click the New SSH key

  • it will redirect you to the page like this

SSH key and GPG keys

  • Similar to the bitbucket we mentioned earlier, add your public key to the key section and some descriptive title that will help your determine the old and new keys in the future

  • Select the type of key, either authentication or signing. For more information about commit signing you can refer to this link here.

  • after adding all the necessary information, click the add SSH key

  • If everything went well, github will send you the confirmation access directly to your account. For more information, see Sudo mode

Congratulations πŸ‘πŸ‘πŸ‘

Congratulations on adding your first SSH key to your favorite or chosen version control system. By this blog post, i hope we can help more developers who are struggling on setting up their first SSH keys on your side projects, side hustles or company project.

Top comments (0)