Enabling SSH with key authentication involves generating an SSH key pair and configuring your SSH server to allow key-based authentication. Here are the basic steps:
- Generate an SSH key pair
On your local machine, generate an SSH key pair using the following command:
ssh-keygen -t rsa
This will create a public key file (typically named id_rsa.pub) and a private key file (typically named id_rsa) in the .ssh directory in your home directory.
- Copy the public key to the server
Copy the contents of the public key file to the server where you want to enable SSH key authentication. You can do this manually by copying and pasting the contents of the file, or you can use the following command:
ssh-copy-id username@server
This will copy the public key to the server and add it to the authorized keys file.
- Configure the SSH server
On the server, open the SSH server configuration file (/etc/ssh/sshd_config) and ensure that the following options are set:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
Save the changes to the configuration file and restart the SSH server.
- Test the SSH connection.
Test the SSH connection to the server by attempting to connect using the private key file:
ssh -i ~/.ssh/id_rsa username@server
If the key-based authentication is successful, you should be able to connect to the server without entering a password.
Note: Be sure to keep the private key file secure and do not share it with anyone. Anyone with access to the private key file can potentially gain access to your server.