How to connect via SSH using keys

ByJUJU-dev

How to connect via SSH using keys

Connecting to a server via SSH using keys is a secure and convenient way to access your server without having to remember a password. Here’s a step-by-step guide on how to connect to a server via SSH using keys:

Prerequisites:

  • You have a Unix-based operating system (such as Linux or macOS) or a tool that allows you to use SSH, such as PuTTY on Windows.
  • You have generated a pair of SSH keys using a tool like OpenSSL or Git Bash.
  • You have the public key saved on your local machine and the private key saved on your server.

Step 1: Generate a pair of SSH keys

If you haven’t already, generate a pair of SSH keys using a tool like OpenSSL or Git Bash. The process is as follows:

  • Open Terminal or Git Bash.
  • Run the command ssh-keygen -t rsa -b 4096 (or ssh-keygen -t ed25519 for newer versions of OpenSSH).
  • Follow the prompts to create a new key pair.
  • Save the private key in a secure location, such as ~/.ssh/id_rsa (or ~/.ssh/id_ed25519 for newer versions of OpenSSH).
  • Save the public key in a file, such as ~/.ssh/id_rsa.pub (or ~/.ssh/id_ed25519.pub for newer versions of OpenSSH).

Step 2: Copy the public key to your server

Copy the contents of the public key file (id_rsa.pub or id_ed25519.pub) and add it to the authorized_keys file on your server. You can do this using SFTP, SCP, or by pasting it directly into the file using SSH.

Step 3: Configure SSH client

To connect to your server using SSH, you’ll need to configure your SSH client. The process is as follows:

  • On Linux/macOS, open Terminal and run the command ssh-add ~/.ssh/id_rsa (or ssh-add ~/.ssh/id_ed25519 for newer versions of OpenSSH).
  • On Windows, open PuTTY and load the private key file by clicking “File” > “Load private key” and selecting your private key file.

Step 4: Connect to your server

Use your configured SSH client to connect to your server. The command would be:

ssh user@your-server-ip-or-hostname

Replace user with your username and your-server-ip-or-hostname with your server’s IP address or hostname.

Tips and Variations:

  • Make sure to set the correct permissions on the authorized_keys file and ensure it’s only readable by the owner.
  • If you’re using a non-standard port for SSH, specify it in the connection command: ssh -p 2222 user@your-server-ip-or-hostname.
  • If you’re connecting from a corporate network or behind a firewall, you may need to configure your firewall rules or ask your network administrator for assistance.

By following these steps, you should be able to connect to your server via SSH using keys. This method provides an additional layer of security compared to traditional password-based authentication.

About the author

JUJU-dev administrator

Leave a Reply

11 − 11 =