DEV Community

gautam-droid
gautam-droid

Posted on

6 1 1 1 1

πŸ”‘ Setting Up SSH Key-Based Authentication

Before Proceeding β³πŸ‘‰ If you're interested in learning how to retrieve a username and IP, be sure to check out my first post for a detailed explanation! πŸ”πŸ’‘
How to log in to a remote server using SSH with a username and password

1️⃣ Start the SSH Server on the Host Machine

πŸš€ Run the following command on your host machine to start the SSH server:

sudo systemctl start ssh
Enter fullscreen mode Exit fullscreen mode

πŸ” To check the status, run:

sudo systemctl status ssh
Enter fullscreen mode Exit fullscreen mode

2️⃣ Generate an SSH Key on the Client Machine

βœ… Before proceeding, check if any key is present in the host machine's .ssh folder:

cd ~/.ssh
ls
Enter fullscreen mode Exit fullscreen mode

If there are any keys present, like id_rsa, id_ed25519, or any other name, choose a different file name unless you want to overwrite it.

πŸ’» Now, on your client machine (yes, you heard it right), generate an SSH key pair by running:

sudo ssh-keygen -b 4096
Enter fullscreen mode Exit fullscreen mode

ℹ️ Note: The -b flag specifies the key length in bits (4096 in this case).

generate key

βœ… When prompted for the filename, hit enter to use the default location (~/.ssh/id_rsa) if no existing keys are present.

For a passphrase, if you don’t want one, hit Enter βœ…πŸ”‘

press enter

⚠️ If a file already exists in ~/.ssh/, it's recommended to specify a custom filename (don't forget to provide the full path, or it will be created in your current directory):

~/.ssh/customFileName
Enter fullscreen mode Exit fullscreen mode

🎯 Press enter to continue.


3️⃣ Copy the Public Key to the Host Machine

πŸ’‘ Tip: It doesn't matter where you generate the key. What matters is that the private key should remain on the client machine, and the public key should be on the host machine. If needed, you can also transfer it manually.

πŸ“€ To copy the public key to the host machine from the client machine, run:

ssh-copy-id -i ~/.ssh/customFileName.pub username@ip
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ This command specifies which public key to send to the host machine. In turn, it will be added to ~/.ssh/authorized_keys.

copy public key

πŸ”„ Alternatively, if you want to automatically pick the default public key from ~/.ssh/, use:

ssh-copy-id username@ip
Enter fullscreen mode Exit fullscreen mode

4️⃣ Verify the Key on the Host Machine (Optional)

πŸ› οΈ On your host machine, check if the key was copied successfully:

cat ~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

verify public key

The key in this file should be exactly the same as the public key on your client machine. If there are multiple keys, look for the one that matches your public key.


5️⃣ Log in to the Host Machine from the Client Machine

πŸ”‘ Now, try logging into your host machine from the client machine:

ssh username@ip
Enter fullscreen mode Exit fullscreen mode

Logging IN

πŸ” The first attempt will still ask for a password.

πŸŽ‰ On the second attempt, key-based authentication should work.

successful authentication

⚠️ Troubleshooting: Still Asking for Password?

If SSH still prompts for a password after the first attempt:

1️⃣ On your host machine, edit the SSH configuration file:

sudo nvim /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

2️⃣ πŸ”Ž Search for the following settings and update them if necessary:

  • PasswordAuthentication β†’ Change yes to no

Password Authentication

  • Public Key Authentication β†’ Change no to yes

publicKeyAuthentication

  • AuthorizedKeysFile β†’ Uncomment this line

Authorized Keys File

3️⃣ πŸ’Ύ Save and exit (:wq in nvim).

save and exit
4️⃣ πŸ”„ Restart the SSH server:

sudo systemctl restart ssh
Enter fullscreen mode Exit fullscreen mode

restart ssh server

πŸŽ‰ You're Done!

βœ… Now, you should be able to log in without a password using SSH key-based authentication. πŸš€

Thank you all for taking the time to read this post! πŸ™Œ
I truly appreciate your support and hope you found it helpful. πŸš€

Top comments (0)