How to Generate & Set Up SSH Keys on CentOS 7

August 19, 2019


Secure Shell (SSH) is an encrypted protocol used by Linux users to connect to their remote servers.

Generally, there are two ways for clients to access their servers – using password based authentication or public key based authentication.

Using SSH keys for authentication is highly recommended, as a safer alternative to passwords.

This tutorial will guide you through the steps on how to generate and set up SSH keys on CentOS 7. We also cover connecting to a remote server using the keys and disabling password authentication.

tutorial on generating SSH keys on CentOS 7

Before you start

1. Check for Existing Keys

Prior to any installation, it is wise to check whether there are any existing keys on the client machines.

Open the terminal and list all public keys stored with the following command:

ls -l ~/.ssh/id_*.pub

The output informs you about any generated keys currently on the system. If there aren’t any, the message tells you it cannot access /.ssh/id_*.pub , as there is no such file or directory.

no such directory or ssh key found

2. Verify SSH is Installed

To check if thw package is installed, run the command:

ssh -V

If you already have SSH, the output tells you which version it is running. Currently, the latest version is OpenSSH 8.0/8.0p1.

checking if ssh is installed screenshot

Note: Refer to our guide If you need to install and enable SSH on your CentOS system.

Steps to Creating SSH keys on CentOS

Step 1: Create SSH Key Pair

1. Start by logging into the source machine (local server) and creating a 2048-bit RSA key pair using the command:

ssh-keygen -t rsa

If you want to tighten up security measures, you can create a 4096-bit key by adding the -b 4096 flag:

ssh-keygen -t rsa -b 4096

2. After entering the command, you should see the following prompt:

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

3. To save the file in the suggested directory, press Enter. Alternatively, you can specify another location.

Note: If you already have a key pair in the proposed location, it is advisable to pick another directory. Otherwise it will overwrite existing SSH keys.

4. Next, the prompt will continue with:

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

Although creating a passphrase isn’t mandatory, it is highly advisable.

5. Finally, the output will end by specifying the following information:

Your identification has been saved in home/your_username/.ssh/id_rsa.
Your public key has been saved in home/your_username/.ssh/
The key fingerprint is:
The key's randomart image is:
+------[RSA 3072]-------+
|       .oo.            |
|        +o+.           |
|      + +.+            |
| o  +          S .     |
|      .    E  .   . =.o|
|    .  +       .   B+@o|
|        +   .     oo*=O|
|   oo            . .+o+|
|                 o=ooo=|
+------ [SHA256] ------+

Now you need to add the public key to the remote CentOS server.

Step 2: Copy Public Key to CentOS Server

You can copy the public SSH key on the remote server using several different methods:

  1. using the ssh-copy-id script
  2. using Secure Copy (scp)
  3. manually copying the key

The fastest and easiest method is by utilizing ssh-copy-id. If the option is available, we recommend using it. Otherwise, try any of the other two noted.

Copy Public Key Using ssh-copy-id

1. Start by typing the following command, specifying the SSH user account, and the IP address of the remote host:

ssh-copy-id username@remote_host

If it is the first time your local computer is accessing this specific remote server you will receive the following output:

The authenticity of host '104.0.316.1 (104.0.316.1)' can't be established.
ECDSA key fingerprint is KYg355:gKotTeU5NQ-5m296q55Ji57F8iO6c0K6GUr5:PO1iRk.
Are you sure you want to continue connecting (yes/no)? yes

2. Confirm the connection – type yes and hit Enter.

3. Once it locates the key created on the local machine, it will ask you to provide the password for the remote account. Type in the password and hit Enter.

4. Once the connection has been established, it adds the public key on the remote server. This is done by copying the ~/.ssh/ file to the remote server’s ~/.ssh directory. You can locate it under the name authorized_keys.

5. Lastly, the output tells you the number of keys added, along with clear instructions on what to do next:

Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'username@104.0.316.1'"
and check to make sure that only the key(s) you wanted were added.

Copy Public Key Using Secure Copy

1. First, set up an SSH connection with the remote user:

ssh username@remote_host

2. Next, create the ~/.ssh directory as well as the authorized_keys file:

mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys

3. Use the chmod command to change the file permission:

chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

chmod 700 makes the file executable, while chmod 600 allows the user to read and write the file.

4. Now, open a new terminal session, on the local computer.

5. Copy the content from (the SSH public key) to the previously created authorized_keys file on the remote CentOS server by typing the command:

scp ~/.ssh/ username@remote_host:~/.ssh/authorized_keys

With this, the public key has been safely stored on the remote account.

Copy Public Key Manually 

1. To manually add the public SSH key to the remote machine, you first need to open the content from the ~/.ssh/ file:

cat ~/.ssh/

2. As in the image below, the key starts with ssh-rsa and ends with the username of the local computer and hostname of the remote machine:

example of a ssh public key on local machine username
3. Copy the content of the file, as you will need later.

4. Then, in the terminal window, connect to the remote server on which you wish to copy the public key. Use the following command to establish the connection:

ssh username@remote_host

5. Create a ~/.ssh directory and authorized_keys file on the CentOS server with the following command:

mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys

6. Change their file permission by typing:

chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

7. Next, open the authorized_keys file with an editor of your preference. For example, to open it with Nano, type:

nano authorized_keys

8. Add the public key, previously copied in step 2 of this section, in a new line in (under the existing content).

9. Save the changes and close the file.

10. Finally, log into the server to verify that everything is set up correctly.

Step 3: Connect to Remote Server Using SSH Keys

Once you have completed the previous steps (creating an RSA Key Pair and copying the Public Key to the CentOS server), you will be able to connect to the remote host without typing the password for the remote account.

All you need to do is type in the following command:

ssh username@remote_host

If you didn’t specify a passphrase while creating the SSH key pair, you will automatically log in the remote server.

Otherwise, type in the passphrase you supplied in the initial steps and press Enter.

Once the shell confirms the key match, it will open a new session for direct communication with the server.

Step 4: Disable Password Authentication

Although you managed to access the CentOS server without having to provide a password, it still has a password-based authentication system running on the machine. This makes it a potential target for brute force attacks.

You should disable password authentication entirely by following the outlined steps.

Note: Consider performing the following steps through a non-root account with sudo privileges, as an additional safety layer.

1. Using the SSH keys, log into the remote CentOS server which has administrative privileges:

ssh username@remote_host

2. Next, open the SSH daemon configuration file using a text editor of your choice:

sudo nano /etc/ssh/sshd_config

3. Look for the following line in the file:

PasswordAuthentication yes

4. Edit the configuration by changing the yes value to no. Thus, the directive should be as following:

PasswordAuthentication no

5. Save the file and exit the text editor.
6. To enable the changes, restart the sshd service using the command:

sudo systemctl restart sshd.service

7. Verify the SSH connection to the server is still functioning correctly. Open a new terminal window and type in the command:

ssh username@remote_host


In this article, you learned how to generate SSH key pairs and set up an SSH key-based authentication. We also covered copying keys to your remote CentOS server, and disabling SSH password authentication.

Was this article helpful?
Sofija Simic
Sofija Simic is an aspiring Technical Writer at phoenixNAP. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How To Install / Enable OpenSSH On CentOS 7
August 28, 2019

This article provides all the information you need in order to set up SSH encryption on your remote device...
Read more
19 Common SSH Commands In Linux With Examples
August 25, 2019

Secure Shell is an important protocol for anyone managing and controlling remote machines...
Read more
How To Generate SSH Keys On Ubuntu 18.04
July 29, 2019

Establishing a connection with a remote server without taking the proper security measures can lead to severe consequences...
Read more
How To Enable SSH On Ubuntu 18.04
April 23, 2019

When establishing a remote connection between a client and a server, a primary concern is ensuring a secure connection....
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.