Skip to content
Author: ytianle

Create & Connect with SSH⚓︎

This guide helps you create an SSH key pair and connect to a remote server using SSH.

Check for Existing SSH Keys⚓︎

First, check if you already have SSH keys on your local machine:

terminal
ls -al ~/.ssh

If you see files like id_ed25519/id_ed25519.pub or id_rsa/id_rsa.pub, you already have an SSH key pair. If not, proceed to the next step.

Generate a New SSH Key Pair⚓︎

Generate a new SSH key pair using the following command. We recommend using the Ed25519 algorithm for better security:

terminal
ssh-keygen -t ed25519 -C "your_email@example.com"

(You will be prompted to enter a passphrase. This is optional but recommended for added security. Press Enter to skip.)

This will create two files: ~/.ssh/id_ed25519 (private key) and ~/.ssh/id_ed25519.pub (public key).

Add SSH Key to SSH-Agent (Optional)⚓︎

The SSH agent manages your SSH keys. It helps you avoid entering your passphrase every time you use the key. If you don't have passphrase, you can skip this step.

Start the SSH agent in the background:

terminal
eval "$(ssh-agent -s)"

Add your SSH private key to the SSH agent:

terminal
ssh-add ~/.ssh/id_ed25519

Start the SSH agent in the background:

terminal
eval $(ssh-agent -s)

Copy the Public Key to the Remote Server⚓︎

Manually copy the public key:

terminal
cat ~/.ssh/id_ed25519.pub
>>> ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your_email@example.com

Paste the copyboard content into the remote server.

  1. Log in to your GitHub account.
  2. Go to Settings > SSH and GPG keys.
  3. Click New SSH key.
  4. Paste your public key into the "Key" field and give it a title.
  5. Click Add SSH key.
  1. Log in to your Bitbucket account.
  2. Go to Personal settings > SSH keys.
  3. Click Add key.
  4. Paste your public key into the "Key" field and give it a label.
  5. Click Add SSH key.

Test SSH Connection⚓︎

Test the SSH connection to Bitbucket:

terminal
ssh -T git@bitbucket.org
>>> Welcome to Bitbucket, <your-username>!

Test the SSH connection to GitHub:

terminal
ssh -T git@github.com
>>> Hi <your-username>! You've successfully authenticated, but GitHub does not provide shell access.

SSH Config File (Optional)⚓︎

You can create an SSH config file to simplify SSH connections to multiple servers. Create or edit the ~/.ssh/config file:

terminal
nano ~/.ssh/config

Add the following configuration for your server:

~/.ssh/config
Host github.com # (1)
    HostName github.com # (2)
    User git # (3)
    IdentityFile ~/.ssh/id_ed25519

Host your-server-alias
    HostName your.server.com
    User your-username
    IdentityFile ~/.ssh/id_ed25519
  1. Host: An alias for the server. You can use this alias to connect instead of the full hostname.
  2. HostName: The actual hostname or IP address of the server.
  3. User: Your username on the remote server.