Skip to content

Instantly share code, notes, and snippets.

@alifeee
Created November 26, 2023 23:04
Show Gist options
  • Save alifeee/2f4be232f6b9c4b384f4c020a5bc10a1 to your computer and use it in GitHub Desktop.
Save alifeee/2f4be232f6b9c4b384f4c020a5bc10a1 to your computer and use it in GitHub Desktop.
Setup RSA keys for automatic SSH login

How to setup RSA keys for automatic SSH login

Before this, I logged in to my server via ssh with

> ssh root@server.alifeee.co.uk
root@server.alifeee.co.uk's password:
...
root@chunkifeee:~#

and then I had to type in the password.

This document describes how to setup RSA keys so I can just type ssh server to login

> ssh server
root@chunkifeee:~#

Generate RSA keys (on server)

Generate public/private RSA keys

The name of the key here is "alifeeePC". I do not set a password.

~# ssh-keygen -f ~/.ssh/alifeeePC -C "alifeee's PC"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/alifeeePC
Your public key has been saved in /root/.ssh/alifeeePC.pub
The key fingerprint is:
...
The key's randomart image is:
...

Add key to ~/.ssh/authorised_keys

cat .ssh/alifeeePC.pub >> .ssh/authorized_keys

Setup SSH connection (on client)

Copy RSA key to machine

scp -r root@server.alifeee.co.uk:~/.ssh/alifeeePC C:\Users\alifeee\.ssh

Connect using key (vanilla)

ssh -i C:\Users\alifeee\.ssh\alifeeePC root@server.alifeee.co.uk

Add host and key to SSH config file

Doing this means I can type ssh server and it basically runs the above command

Add the following to ~/.ssh/config

Host server
  HostName server.alifeee.co.uk
  User root
  IdentityFile ~/.ssh/alifeeePC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment