Enhance Your SSH Security: Setting up Local and Remote Servers to Use SSH Key Connection

SSH (Secure Shell) is a secure protocol for connecting to remote servers and performing various tasks. While SSH can use a password for authentication, it’s much more secure to use an SSH key pair instead. This method involves generating a public and private key pair, with the public key being placed on the remote server and the private key being kept on your local machine. Here’s how you can set up a local and remote server to use SSH key connection.

Generating an SSH Key Pair

The first step is to generate an SSH key pair on your local machine. To do this, open a terminal and run the following command:

ssh-keygen -t rsa

This will generate an RSA key pair, which consists of a public key (id_rsa.pub) and a private key (id_rsa). By default, these files will be saved in the ~/.ssh/ directory.

Copying the Public Key to the Remote Server

Next, you’ll need to copy the public key to the remote server. To do this, you can use the ssh-copy-id command. This command will copy the public key to the remote server and add it to the authorized_keys file, which is used to authenticate SSH connections.

ssh-copy-id username@remote_server_ip

Replace username with your username on the remote server and remote_server_ip with the IP address or hostname of the remote server.

If you’re prompted to enter a password, enter the password for your remote server account. Once the command completes, you’ll be able to connect to the remote server without a password.

SSH Key Pair with a Custom Name

Generating an SSH Key Pair with a Custom Name:

By default, the SSH key pair generated with the ssh-keygen command is named id_rsa for the private key and id_rsa.pub for the public key. However, you can also generate an SSH key pair with a custom name using the -f option. For example, to generate an SSH key pair named my_key:

ssh-keygen -t rsa -f ~/.ssh/my_key

This will generate a private key named my_key and a public key named my_key.pub in the ~/.ssh/ directory.

Copying the Public Key with a Custom Name to the Remote Server

When copying the public key to the remote server, you’ll need to specify the custom name of the public key using the -i option. For example:

ssh-copy-id -i ~/.ssh/my_key.pub username@remote_server_ip

This will copy the my_key.pub public key to the remote server and add it to the authorized_keys file.

Configuring SSH on the Local Machine with a Custom Named Key

To configure SSH on the local machine to use the private key with a custom name, you’ll need to add the following lines to the ~/.ssh/config file:

Host remote_server_ip
    IdentityFile ~/.ssh/my_key

Replace remote_server_ip with the IP address or hostname of the remote server, and my_key with the custom name of your private key.

Testing the SSH Connection with a Custom Named Key

Finally, you can test the SSH connection to the remote server with the custom named key by running the following command:

ssh -i ~/.ssh/my_key username@remote_server_ip

This will use the my_key private key for authentication.

Configuring SSH on the Local Machine

Now that the public key is on the remote server, you’ll need to configure SSH on your local machine to use the private key for authentication. To do this, open the ~/.ssh/config file in a text editor and add the following lines:

Host remote_server_ip
    IdentityFile ~/.ssh/id_rsa

Replace remote_server_ip with the IP address or hostname of the remote server.

Save the changes and exit the text editor.

Testing the SSH Connection

Finally, you can test the SSH connection to the remote server by running the following command:

ssh username@remote_server_ip

If everything was set up correctly, you should be able to connect to the remote server without a password prompt.

Conclusion

Setting up a local and remote server to use SSH key connection is a simple process that can greatly enhance the security of your SSH connections. By using an SSH key pair instead of a password, you can help prevent unauthorized access to your servers and data.

In summary, the steps involved in setting up SSH key connection include generating an SSH key pair on your local machine, copying the public key to the remote server, configuring SSH on the local machine to use the private key for authentication, and testing the SSH connection to the remote server. By following these steps, you can set up a secure and reliable SSH connection between your local and remote servers.