Unlocking Seamless Development: Mastering SSH Key Setup for Bitbucket and GitHub in WSL2
Tags: ubuntu, bash, cli
Published: 2023-06-10 12:29:25


##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)

```javascript
 sudo apt install openssh-client
```

- Once the openssh-client installation is complete, in the terminal, check that openssh-client has been successfully installed by running the following command:

```javascript
ssh -V
```

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

```javascript
ps -a | grep ssh-agent
```

to start the agent:

```javascript
eval $(ssh-agent)
```
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 

```javascript
cd ~
```

- Generate your SSH key pair using ssh-keygen, write this command:

```javascript
ssh-keygen -t ed25519 -b 4096 -C "username@emaildomain.com" -f ssh-key-name
```

   - 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:

```javascript
ssh-add ~/ssh-key-name
```

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: 

```javascript
ssh-ed25529 LLoWYaPswHzVqQ7L7B07LzIJbntgmHqrE40t17nGXL71QX9IoFGKYoF5pJKUMvR+DZotTm user@example.com
```

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

```javascript
ssh -T git@bitbucket.org
```

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

```javascript
authenticated via ssh key.

You can use git to connect to Bitbucket. Shell access is disabled
```

###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbw61wma9f6l8mxq1wjd.png)


- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ivqi066nwxwlxe7ctce.png)

- 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](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification).

- 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](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/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.