Pre-requisite

First, generate a SSH key on your local machine if you don’t have one. Follow instructions at GitHub docs on how to do this.


Method 1: using ssh-copy-id

Step 1: Transfer the Public Key

Run this command on your local machine, replacing user with the remote username and remote_host with the IP or hostname:

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote_host

This command appends your public key to the ~/.ssh/authorized_keys file on the remote host. You’ll be prompted for the remote user’s password once.

Step 2: Verify SSH Key authentication works

Test the connection:

ssh user@remote_host

You should be able to log in without being prompted for a password. If it still asks for a password, the key wasn’t added properly. So, follow Method 2, or retry Method 1.


Method 2: Manually copy your SSH public key

If ssh-copy-id is unavailable, follow these steps:

Step 1: Copy your Public Key

On your local machine, display the contents of your public key:

cat ~/.ssh/id_ed25519.pub

Copy the entire output.

Step 2: SSH into the Remote Host with password authentication

ssh user@remote_host

Enter your password when prompted.

Step 3: Create the .ssh directory (if it doesn’t exist)

On the remote host, run:

mkdir -p ~/.ssh

Step 4: Add Your Public Key to authorized_keys

On the remote host, open the authorized_keys file:

nano ~/.ssh/authorized_keys

Paste your public key on a new line, then save the file.

Step 5: Set Correct Permissions

On the remote host, set the proper permissions:

chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh

Step 6: Exit the Remote Host

exit

Step 7: Test Key-Based Authentication

Back on your local machine, test the connection:

ssh user@remote_host

You should be able to log in without being prompted for a password.


Add a config entry for convenient login

Step 1: Create or edit your .ssh/config file

On your local machine, open (or create) the config file:

nano ~/.ssh/config

Step 2: Add a Host Entry

Add an entry like this (replace with your actual values):

Host myserver
    HostName remote_host
    User user
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes
  • Host myserver — this is the custom name you’ll use to log in
  • HostName — the actual IP address or hostname
  • User — the username on the remote machine
  • IdentityFile — path to your private key
  • IdentitiesOnly - only use the identity file(s) you explicitly specify in the config file.

Step 3: Save and Set Correct Permissions

Save the file, then set permissions:

chmod 600 ~/.ssh/config

Step 4: Login using your custom name

Now you can simply run:

ssh myserver

You can add multiple host entries to your config file using the same format for other servers.