SSH Key Setup: Generate, Configure & Use SSH Keys

How to Set Up SSH Keys

Using SSH keys is the gold standard for securely connecting to remote servers, pushing code to GitHub, or managing infrastructure. Passwords can be brute-forced; cryptographic key pairs are virtually uncrackable.

In this guide, we will walk you through SSH key generation, how to add an SSH key to a server, and managing multiple keys efficiently.

SSH Key Generation (Ed25519 vs RSA)

When you create an SSH key, you must choose an encryption algorithm. The two most common are RSA and Ed25519.

  • Ed25519 (Recommended): The modern standard. It is faster, more secure, and produces a shorter key length compared to RSA.
  • RSA: The legacy standard. If you must use RSA for older systems, ensure it is at least 4096 bits.

SSH Keygen Tutorial: Creating Your Key Pair

This SSH keygen tutorial works identically across Linux, macOS, and modern Windows (via Command Prompt, PowerShell, or WSL).

Step 1: Open Your Terminal

Launch your preferred command line interface.

Step 2: Run the Generation Command

Execute the following command, replacing the email with your own:

ssh-keygen -t ed25519 -C "[email protected]"

Step 3: Save the Key

You'll be prompted to save the file. Press Enter to accept the default location (usually ~/.ssh/id_ed25519).

Step 4: Enter a Passphrase

It is highly recommended to use a strong passphrase. This encrypts the private key on your local machine, adding a critical layer of defense.

Add SSH Key to Server

Now that your key is generated, you need to add the SSH key to a server to authenticate.

Method 1: Using ssh-copy-id (Mac/Linux)

This is the easiest method. Run this command:

ssh-copy-id username@remote_server_ip

You will be asked for your password one last time. After that, your public key will be added to the server's ~/.ssh/authorized_keys file.

Method 2: Manual Copy (Windows/Any OS)

If ssh-copy-id is not available, you can copy it manually.

1. Display your public key:

cat ~/.ssh/id_ed25519.pub

2. SSH into your server with a password, open ~/.ssh/authorized_keys, and paste the public key on a new line.

Adding to the SSH Agent

If you set a passphrase, you don't want to type it every time. The ssh-agent remembers it for you.

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Configure ~/.ssh/config for Multiple Hosts

Managing multiple servers? Use an SSH config file to simplify your life. Create or edit ~/.ssh/config:

Host myserver
    HostName 192.168.1.50
    User admin
    IdentityFile ~/.ssh/id_ed25519

Host github.com
    User git
    IdentityFile ~/.ssh/github_key

Now, simply typing ssh myserver connects you instantly.

GitHub & GitLab SSH Setup

To push and pull code securely without typing your username and password:

  1. Copy your public key: cat ~/.ssh/id_ed25519.pub
  2. Go to GitHub/GitLab Settings → SSH and GPG keys.
  3. Click New SSH Key, give it a title, and paste your key.
  4. Test the connection: ssh -T [email protected]

Security Best Practices

  • Always use a passphrase: Protects your private key if your laptop is stolen.
  • Key Rotation: Generate new keys periodically (e.g., every year) and remove old ones from servers.
  • Disable Password Authentication: Once your SSH key is working, edit /etc/ssh/sshd_config on the server and set PasswordAuthentication no.
  • Never share your private key: Only the `.pub` file should ever leave your machine.

Troubleshooting Common Issues

Permission denied (publickey): The server does not recognize your key. Ensure the public key is correctly pasted into ~/.ssh/authorized_keys and the file permissions are correct (chmod 600 authorized_keys).

Key not accepted: Ensure the ssh-agent is running and your key is added.

Agent forwarding: If you need to hop from one server to another using your local key, use ssh -A user@server, but only on trusted servers.

Frequently Asked Questions

How to set up ssh keys?

To set up SSH keys, open your terminal and run the command ssh-keygen -t ed25519 -C "[email protected]". Press Enter to save the key to the default location, and enter a strong passphrase. Then, use ssh-copy-id user@hostname to add the key to your server.

What is the best algorithm for ssh key generation?

The recommended algorithm for SSH key generation is Ed25519. It provides better security and performance compared to older algorithms like RSA, while producing much shorter and easier-to-manage keys.

Is there a simple ssh keygen tutorial for beginners?

Yes, our ssh keygen tutorial covers Windows, Mac, and Linux. On all three platforms, you can open a terminal (Command Prompt, Terminal app, or bash shell) and use the built-in ssh-keygen command to generate your key pairs seamlessly.

How do I add ssh key to server securely?

You can add an ssh key to a server automatically using ssh-copy-id username@remote_host. Alternatively, you can manually append the contents of your public key (~/.ssh/id_ed25519.pub) to the server's ~/.ssh/authorized_keys file.

How do I configure SSH for GitHub or GitLab?

After generating your SSH key, copy the public key contents using cat ~/.ssh/id_ed25519.pub. Log into GitHub/GitLab, go to SSH and GPG keys in settings, and paste the key. Test the connection with ssh -T [email protected].