Skip to content

Generating Multiple SSH Key Pairs

SSH key pairs are used for secure access to remote servers. You might need multiple key pairs for different purposes or services. Here's a short guide on generating unique SSH key pairs.

Step 1: Open a Terminal

Open your terminal application. This process works on Linux, macOS, and Windows (using Git Bash or WSL).

Step 2: Generate a New SSH Key Pair

Use the ssh-keygen command to generate a new key pair. You can specify a unique name for each key pair to avoid overwriting existing ones.

sh
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_unique

Explanation:

  • -t rsa: Specifies the type of key to create, in this case, RSA.
  • -b 4096: Specifies the number of bits in the key (4096 bits).
  • -f ~/.ssh/id_rsa_unique: Specifies the file name and location for the new key pair.

Step 3: Enter a Passphrase (Optional)

You will be prompted to enter a passphrase. This adds an extra layer of security. You can press Enter to skip this step if you don't want to set a passphrase.

sh
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Step 4: View Your Key Pair

Your new SSH key pair will be saved in the specified location (~/.ssh/id_rsa_unique and ~/.ssh/id_rsa_unique.pub).

To view your public key, use:

sh
cat ~/.ssh/id_rsa_unique.pub

Step 5: Add Your SSH Key to the SSH Agent

Start the SSH agent and add your new SSH key.

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

Step 6: Add the Public Key to Remote Servers

Copy the content of your public key (~/.ssh/id_rsa_unique.pub) and add it to the ~/.ssh/authorized_keys file on the remote server you want to access.

You can use ssh-copy-id to do this easily:

sh
ssh-copy-id -i ~/.ssh/id_rsa_unique.pub username@remote_host

Step 7: Update SSH Config (Optional)

To manage multiple SSH keys more easily, you can create or edit the SSH config file (~/.ssh/config) to specify which key to use for each host.

sh
nano ~/.ssh/config

Add the following configuration:

sh
Host remote_host_alias
    HostName remote_host
    User username
    IdentityFile ~/.ssh/id_rsa_unique

Step 8: Connect Using Your SSH Key

Now you can connect to your remote server using the alias you defined in the SSH config file.

sh
ssh remote_host_alias

Summary

You have generated a unique SSH key pair, added it to your SSH agent, configured it for a specific remote server, and optionally updated your SSH config for easier access. Repeat the above steps with different filenames to create multiple unique SSH key pairs as needed.