In this tutorial, we will show you how to set up SSH Keys on CentOS 8. For those of you who didn’t know, Secure Shell (SSH) is a cryptographic network protocol designed for a secure connection between a client and a server. 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 article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation and setup of SSH keys on the CentOS 8 server.
Prerequisites
- A server running one of the following operating systems: CentOS 8.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, you can harm your system if you’re not careful when acting as the root.
Setup SSH Keys on CentOS 8
Step 1. First, let’s start by ensuring your system is up-to-date.
sudo dnf update
Step 2. Creating SSH keys on CentOS 8.
Start by logging into the source machine (local server) and creating a 4096-bit RSA key pair using the command:
ssh-keygen -t rsa -b 4096 -C "your_email@idroot.us"
After entering the command, you should see the following prompt:
Enter file in which to save the key (/home/your-username/.ssh/id_rsa):
To save the file in the suggested directory, press Enter. Alternatively, you can specify another location.
Next, the prompt will continue with:
Enter passphrase (empty for no passphrase):
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/id_rsa.pub. The key fingerprint is: KYg355:gKmeiU5NQ-ramona5Ji57F8iO6c0K6GUr5:BMWRk username@hostname The key's randomart image is: +------[RSA 3072]-------+ | .oo. | | +o+. | | + +.+ | | o + S . | | . E . . =.o| | . + . B+@o| | + . oo*=O| | oo . .+o+| | o=ooo=| +------ [SHA256] ------+
Step 3. Copy Public Key to CentOS Server.
Start by typing the following command, specifying the SSH user account, and the IP address of the remote host:
ssh-copy-id username@server_ip_address
Once the user is authenticated, the content of the public key file (~/.ssh/id_rsa.pub
) will be appended to the remote user ~/.ssh/authorized_keys
file and the connection will be closed.
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@server_ip_address'" and check to make sure that only the key(s) you wanted were added.
Step 4. Connect to Remote Server Using SSH Keys.
After completing the steps above, you should be able to log in to the remote server without being prompted for a password:
ssh remote_username@server_ip_address
Step 5. 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. Follow the steps below to disable SSH password authentication:
ssh sudo_user@server_ip_address
Open the SSH configuration file /etc/ssh/sshd_config:
sudo nano /etc/ssh/sshd_config
PasswordAuthentication no ChallengeResponseAuthentication no UsePAM no
Once you are done save the file and restart the SSH service:
sudo systemctl restart ssh
Congratulations! You have successfully installed the SSH server. Thanks for using this tutorial for installing and configuring SSH keys on CentOS 8 systems. For additional help or useful information, we recommend you check the official SSH website.