The Ultimate Guide to SSH - Setting Up SSH Keys (2025)

Welcome to our ultimate guide to setting up SSH (Secure Shell) keys. This tutorial will walk you through the basics of creating SSH keys, and also how to manage multiple keys and key pairs.

Create a New SSH Key Pair

Open a terminal and run the following command:

ssh-keygen

You will see the following text:

Generating public/private rsa key pair.Enter file in which to save the key (/home/username/.ssh/id_rsa):

Press enter to save your keys to the default /home/username/.ssh directory.

Then you'll be prompted to enter a password:

Enter passphrase (empty for no passphrase):

It's recommended to enter a password here for an extra layer of security. By setting a password, you could prevent unauthorized access to your servers and accounts if someone ever gets a hold of your private SSH key or your machine.

After entering and confirming your password, you'll see the following:

Your identification has been saved in /home/username/.ssh/id_rsa.Your public key has been saved in /home/username/.ssh/id_rsa.pub.The key fingerprint is:SHA256:/qRoWhRcIBTw0D4KpTUyK6YepyL6RQ2CQrtWsaicCb4 username@871e129f767bThe key's randomart image is:+---[RSA 2048]----+| .o=+.... ||+.*o+o . ||+X.=o o ||@.=.oo . ||=O ...o S ||o.oo . . ||.E+ . . . . ||oo . ... + ||=.. .o. . . |+----[SHA256]-----+

You now have a public and private SSH key pair you can use to access remote servers and to handle authentication for command line programs like Git.

Manage Multiple SSH Keys

Though it's considered good practice to have only one public-private key pair per device, sometimes you need to use multiple keys or you have unorthodox key names. For example, you might be using one SSH key pair for working on your company's internal projects, but you might be using a different key for accessing a client's servers. On top of that, you might be using a different key pair for accessing your own private server.

Managing SSH keys can become cumbersome as soon as you need to use a second key. Traditionally, you would use ssh-add to store your keys to ssh-agent, typing in the password for each key. The problem is that you would need to do this every time you restart your computer, which can quickly become tedious.

A better solution is to automate adding keys, store passwords, and to specify which key to use when accessing certain servers.

SSH config

Enter SSH config, which is a per-user configuration file for SSH communication. Create a new file: ~/.ssh/config and open it for editing:

nano ~/.ssh/config

Managing Custom Named SSH key

The first thing we are going to solve using this config file is to avoid having to add custom-named SSH keys using ssh-add. Assuming your private SSH key is named ~/.ssh/id_rsa, add following to the config file:

Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes

Next, make sure that ~/.ssh/id_rsa is not in ssh-agent by opening another terminal and running the following command:

ssh-add -D

This command will remove all keys from currently active ssh-agent session.

Now if you try closing a GitHub repository, your config file will use the key at ~/.ssh/ida_rsa.

Here are some other useful configuration examples:

Host bitbucket-corporate HostName bitbucket.org User git IdentityFile ~/.ssh/id_rsa_corp IdentitiesOnly yes

Now you can use git clone git@bitbucket-corporate:company/project.git

Host bitbucket-personal HostName bitbucket.org User git IdentityFile ~/.ssh/id_rsa_personal IdentitiesOnly yes

Now you can use git clone git@bitbucket-personal:username/other-pi-project.git

Host myserver HostName ssh.username.com Port 1111 IdentityFile ~/.ssh/id_rsa_personal IdentitiesOnly yes User username IdentitiesOnly yes

Now you can SSH into your server using ssh myserver. You no longer need to enter a port and username every time you SSH into your private server.

Password management

The last piece of the puzzle is managing passwords. It can get very tedious entering a password every time you initialize an SSH connection. To get around this, we can use the password management software that comes with macOS and various Linux distributions.

For this tutorial we will use macOS's Keychain Access program. Start by adding your key to the Keychain Access by passing -K option to the ssh-add command:

ssh-add -K ~/.ssh/id_rsa_whatever

Now you can see your SSH key in Keychain Access:

The Ultimate Guide to SSH - Setting Up SSH Keys (1)

But if you remove the keys from ssh-agent with ssh-add -D or restart your computer, you will be prompted for password again when you try to use SSH. Turns out there's one more hoop to jump through. Open your SSH config file by running nano ~/.ssh/config and add the following:

Host * AddKeysToAgent yes UseKeychain yes

With that, whenever you run ssh it will look for keys in Keychain Access. If it finds one, you will no longer be prompted for a password. Keys will also automatically be added to ssh-agent every time you restart your machine.

Now that you know the basics of creating new SSH keys and managing multiple keys, go out and ssh to your heart's content!

The Ultimate Guide to SSH - Setting Up SSH Keys (2025)

FAQs

How to set up keys for SSH? ›

Generating a new SSH key
  1. Open Terminal .
  2. Paste the text below, replacing the email used in the example with your GitHub email address. ssh-keygen -t ed25519 -C "your_email@example.com" ...
  3. At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases."

How to setup SSH keys between two servers? ›

  1. Step 1: Generate SSH Key Pair on Server A. Log in to Server A using SSH or physical access. ...
  2. Step 2: Copy the Public Key to Server B. Use the ssh-copy-id command to copy the public key to Server B: ssh-copy-id user@serverB_IP. ...
  3. Step 3: Test the Passwordless Connection.
Sep 12, 2023

What are the best keys for SSH? ›

We strongly recommend using only the ed25519 algorithm (an ECDSA variant). It is the most secure SSH key type widely available, and is very well supported in the majority of systems. If you are using an client or server without ed25519 support, you should consider upgrading where possible.

How to connect SSH with SSH keys? ›

The SSH public key authentication has four steps:
  1. Generate a private and public key, known as the key pair. ...
  2. Add the corresponding public key to the server.
  3. The server stores and marks the public key as approved.
  4. The server allows access to anyone who proves the ownership of the corresponding private key.
Aug 10, 2021

How to put SSH keys on server? ›

You can place public SSH keys on an existing server in two ways:
  1. copy the key to the server from the local computer using the ssh-copy-id command;
  2. manually place the key on the server.
Aug 21, 2024

Can two computers have the same SSH key? ›

SSH Keys on Multiple Machines

You can only upload and use one SSH key at a time at the SDCC. If you own multiple machines (e.g., a desktop and a laptop), then you can generate a public/private key pair on one machine, upload the public key to the LDAP server, and copy the private key to your other machines.

Is it OK to have multiple SSH keys? ›

You can use a single key to access any number of hosts. The public key must be included in the authorized_keys file on the server for password-less access. You can also use different keys for accessing different systems, if you prefer. Interacting with Git Fusion requires a separate key.

Do I need to restart SSH after adding a key? ›

Issue: When you generate a new SSH key, you need to restart the SSH server for the new key to take effect. Update: SSH keys are now available for new connections immediately without a server restart as of Oracle ILOM 3.0.

How to get SSH key from terminal? ›

The first method is to use the cat command:
  1. Using the run command.
  2. Execute the following command. cat ~/.ssh/id_rsa.pub. The command will display your SSH key on your Linux machine without requiring your key authentication password.
Sep 12, 2023

How to generate SSH key from server? ›

To generate an SSH key on your Linux server, run the command ssh-keygen . The command can take flags if you would like to customize the type of key that is generated and the signing algorithms that are used to generate the key. This example generates a standard 2048-bit RSA key without a passphrase.

How to find SSH key? ›

Checking for existing SSH keys
  1. Open Terminal .
  2. Enter ls -al ~/.ssh to see if existing SSH keys are present. $ ls -al ~/.ssh # Lists the files in your .ssh directory, if they exist.
  3. Check the directory listing to see if you already have a public SSH key. ...
  4. Either generate a new SSH key or upload an existing key.

Do SSH keys expire? ›

Currently, the SSH keys added doesn't have any expiry policy it would be good if we can set an expiry time for the SSH Keys like we have for Personal Access Tokens.

Does the SSH key include an email address? ›

1 Answer. SSH keys don't have an email address field. They have a comment field, which typically contains a "user@host" of the system which generated the key, but that's neither an email address nor something that SSH (much less Git) pays attention to – it's just a label for the key. No, GitHub doesn't do that.

What are the basics of SSH keys? ›

How do SSH keys work? The SSH key pair is used to authenticate the identity of a user or process that wants to access a remote system using the SSH protocol. The public key is used by both the user and the remote server to encrypt messages. On the remote server side, it is saved in a public key file.

How to setup SSH key Exchange? ›

Configuring SSH key-exchange
  1. Enter global configuration mode. ...
  2. Use the ssh server key-exchange command to set the key exchange algorithm for the server. ...
  3. Use the ssh client key-exchange command to set the key exchange algorithm for the client.

How to set up SSH key for remote login? ›

To generate an SSH key on your Linux server, run the command ssh-keygen . The command can take flags if you would like to customize the type of key that is generated and the signing algorithms that are used to generate the key.

How do I manually add SSH key? ›

How to add new SSH key to a cloud server
  1. Connect to the server using your current key. ssh cloud-user@1.2.3.4 -i /current_private_ssh_key.
  2. Add a public key to the "authorized_keys" file. You can add a public key to the "authorized_keys" file using "nano" text editor (or any text editor of your choice): ...
  3. Verify new key.

How do I specify which SSH key to use? ›

To specify which private key should be used for connections to a particular remote host, use a text editor to create a ~/.ssh/config that includes the Host and IdentityFile keywords. Once you save the file, SSH will use the specified private key for future connections to that host.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 5305

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.