How To Set Up SSH Keys on RHEL 8

Setting up SSH keys on a Red Hat Enterprise Linux 8 (RHEL 8) system allows you to log in to a remote server without having to enter a password. Here is an overview of the steps to set up SSH keys on RHEL 8:

  1. Start by creating a new key pair on your local machine. This can be done using the ssh-keygen command:
ssh-keygen -t rsa -b 4096

This will create a new RSA key pair in the ~/.ssh directory, with the private key stored in the file id_rsa and the public key stored in the file id_rsa.pub.

  1. Copy the public key to the remote server by using the ssh-copy-id command:
ssh-copy-id user@remote-server-ip

This will copy the public key to the remote server, and add it to the authorized_keys file in the ~/.ssh directory on the remote server.

  1. Test the connection to the remote server without a password:
ssh user@remote-server-ip
  1. If you are using SELinux on your RHEL 8 system, you may need to configure it to allow SSH key-based authentication. You can do this by running the following command:
sudo setsebool -P ssh_use_syslog on
  1. To disable SSH password authentication and enable SSH key-based authentication, open the SSH configuration file:
sudo nano /etc/ssh/sshd_config

and change the following lines:

PasswordAuthentication no
PubkeyAuthentication yes
  1. Restart the SSH service:
sudo systemctl restart sshd
  1. You can also configure ssh-agent so that you don’t have to enter the passphrase every time you log in to the remote server. You can start the ssh-agent service and add your key to the ssh-agent keychain with the following commands:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
  1. To remove ssh key from ssh-agent keychain you can use
ssh-add -d ~/.ssh/id_rsa

By following these steps, you can set up SSH keys on a RHEL 8 system, and use them to log in to a remote server without having to enter a password. Additionally, you can configure SELinux and SSH configuration file to allow SSH key-based authentication and disable SSH password authentication. You can also start the ssh-agent service and add your key to the ssh-agent keychain so that you don’t have to enter the passphrase every time you log in to the remote server.

Leave a Comment