March 9th, 2023
Set up personal SSH keys on Linux
				
					#=========================
#   ON CLIENT
#=========================

# Go to this location and create the private and public keys
cd ~/.ssh
ssh-keygen -t ed25519 -b 4096 -C "{username@emaildomain.com}" -f {ssh-key-name}

# This will create two files
{ssh-key-name} — the private key.
{ssh-key-name}.pub — the public key.

# start the agent
eval $(ssh-agent)

# Invoke this command to add the keys in config
ssh-add ~/.ssh/{ssh-key-name}

# Ensure correct ssh key is configured, and add ip and key location
vim ~/.ssh/config

Host 10.21.172.143
  AddKeysToAgent yes
  IdentityFile ~/.ssh/{ssh-key-name}


#=========================
#   ON SERVER
#=========================

# Go to this location
cd ~/.ssh

# Paste the public key in the "authorize_keys" file
authorized_keys
				
			

Install OpenSSH on Linux

 
 

To install OpenSSH, we recommend using the OpenSSH package provided by your Linux distribution.

  • For Debian, Ubuntu, Linux Mint, and other Debian-based distributions:

    1 sudo apt update && sudo apt install openssh-client
  • For Fedora, CentOS, Red Hat Enterprise Linux, Oracle Linux, and other Fedora-based distributions:

    1 sudo dnf install openssh-clients
  • For Arch Linux and other Arch Linux-based distributions:

    1 sudo pacman -Sy openssh
  • For SUSE Linux, openSUSE Linux, and other SUSE-based distributions:

    1 sudo zypper install openssh
 
 

In the terminal, check that OpenSSH has been successfully installed by running the following command:

1 ssh -V

The output should show the installed version of OpenSSH.

Start the SSH agent

 
 

To allow git to use your SSH key, an SSH agent needs to be running on your device.

To check if it is already running, run the ps command. If the ssh-agent is already running, it should appear in the output, such as:

1 2 $ ps -auxc | grep ssh-agent myusername 3291 0.0 0.0 6028 464 ? Ss 07:29 0:00 ssh-agent

To start the agent, run:

1 eval $(ssh-agent)

You may need to add this command to your ~/.bashrc, ~/.zshrc, ~/.profile, or equivalent shell configuration file. Adding this command to a shell configuration file will ensure the agent is running when you open a terminal.

Create an SSH key pair

 
 

To create an SSH key pair:

  1. Open a terminal and navigate to your home or user directory using cd, for example:

    1 cd ~
  2. Generate a SSH key pair using ssh-keygen, such as:

    1 ssh-keygen -t ed25519 -b 4096 -C "{username@emaildomain.com}" -f {ssh-key-name}

    Where:

    • {username@emaildomain.com} is the email address associated with the Bitbucket Cloud account, such as your work email account.

    • {ssh-key-name} is the output filename for the keys. We recommend using a identifiable name such as bitbucket_work.

  3. When prompted to Enter passphrase, you can either provide a password or leave the password empty. If you input a password, you will be prompted for this password each time SSH is used, such as using Git command that contact Bitbucket Cloud (such as git push, git pull, and git fetch). Providing a password will prevent other users with access to the device from using your keys.

Once complete, ssh-keygen will output two files:

  • {ssh-key-name} — the private key.

  • {ssh-key-name}.pub — the public key.

Add your key to the SSH agent

 
 

To add the SSH key to your SSH agent (ssh-agent):

  1. Run the following command, replacing the {ssh-key-name} with the name of the private key:

    1 ssh-add ~/{ssh-key-name}
  2. To ensure the correct SSH key is used when connecting to Bitbucket, update or create your SSH configuration file (~/.ssh/config) with the following settings:

    1 2 3 Host bitbucket.org AddKeysToAgent yes IdentityFile ~/.ssh/{ssh-key-name}

    Where {ssh-key-name} is the location of the private key file once it has been added to the ssh-agent.

Provide Bitbucket Cloud with your public key

 
 

To add an SSH key to your user account:

  1. At bitbucket.org, select your avatar (Your profile and settings) from the navigation bar at the top of the screen.

  2. Under Settings, select Personal settings.

  3. Under Security, select SSH keys.

  4. Select Add key.

  5. In the Add SSH key dialog, provide a Label to help you identify which key you are adding. For example, Work Laptop <Manufacturer> <Model>. A meaning full label will help you identify old or unwanted keys in the future.

  6. Open the public SSH key file (public keys have the .pub file extension) in a text editor. The public key should be in the .ssh/ directory of your user (or home) directory. The contents will be similar to:

    1 ssh-ed25529 LLoWYaPswHzVqQ7L7B07LzIJbntgmHqrE40t17nGXL71QX9IoFGKYoF5pJKUMvR+DZotTm user@example.com
  7. Copy the contents of the public key file and paste the key into the Key field of the Add SSH key dialog.

  8. Select Add key.

    • If the key is added successfully, the dialog will close and the key will be listed on the SSH keys page.

    • If you receive the error That SSH key is invalid, check that you copied the entire contents of the public key (.pub file).

Check that your SSH authentication works

 
 

To test that the SSH key was added successfully, open a terminal on your device and run the following command:

1 ssh -T git@bitbucket.org

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

1 2 3 authenticated via ssh key. You can use git to connect to Bitbucket. Shell access is disabled
 

Reference:

https://support.atlassian.com/bitbucket-cloud/docs/set-up-personal-ssh-keys-on-linux/

Leave a Reply

Your email address will not be published. Required fields are marked *