ssh to a remote server

This guide will walk you through the process of setting up passwordless login to a remote server using SSH key pairs. This method is more secure and convenient than using a password.

Step 1: Generate an SSH Key Pair

First, you need to create a new public/private key pair on your local machine.

  1. Open a terminal and run the ssh-keygen command:
  2. When prompted, save the key to a specific file. For this example, use: /home/user/.ssh/id_rsa_my_remote_host
  3. You can set a passphrase for an extra layer of security or leave it blank for a fully automated login.

This creates two files:

  • Private Key: id_rsa_my_remote_host (Keep this secure and private on your local machine)
  • Public Key: id_rsa_my_remote_host.pub (This will be copied to the server)
Step 2: Copy the Public Key to the Remote Server

Next, you need to install your public key on the remote server.

  1. Copy the entire contents of your public key file: id_rsa_my_remote_host.pub.
  2. Log into the remote server using your password (this will be the last time you need it).
  3. On the remote server, open or create the file ~/.ssh/authorized_keys.
  4. Paste your public key on a new line in this file. If the file already contains other keys, simply add yours at the end.
  5. Save the file and exit.
Step 3: Connect to the Remote Server

You can now connect without a password using the private key.

ssh my_remote_user@my_remote_host_name
Step 4 (Optional): Simplify with an SSH Config File

To make connecting even easier, you can create a configuration file that remembers your connection details. This eliminates the need to remember usernames, hostnames, or specify the key file every time.

Edit or create the file ~/.ssh/config on your local machine and add the following block:

Host my_remote_host_name
        User my_remote_user
        Hostname my_host_ip_or_name
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_rsa_my_remote_host
  • Host: A friendly alias you will use to connect (e.g., ssh my_server).
  • User: Your username on the remote server.
  • Hostname: The actual IP address or domain of the server.
  • IdentityFile: The path to the private key you generated.

Author

Rami Boutassghount

Rami Boutassghount

Hetzner Cloud

Sign up using my referral link and get โ‚ฌ20 in Hetzner Cloud credits!

Once you spend โ‚ฌ10, Iโ€™ll also receive a โ‚ฌ10 credit โ€” a win-win!

๐ŸŒ Change language