Simple Things I Forget with SSH

As I can never consistently remember all of the following, I'm putting it in writing for my future self. If anyone else finds it useful then that's great... but it is selfishly meant for me. There are countless other resources that go into far greater and specific detail.

We are assuming the remote server is 192.168.1.126, the remote user pi, and the key names pi_hole_id_rsa. Swap out relevant bits as needed.

Generate SSH Key

ssh-keygen -t rsa you can choose a name and catchphrase for the key during this process. Default is id_rsa. Generates two files... store them in ~/.ssh.

Avoid Defaulting to id_rsa

To force your ssh client to use a specific private key by default, rather than`id_rsa`, you would do something like ssh -i ~/.ssh/pi_hole_id_rsa pi@192.168.1.126.

To avoid having to type this out by hand every time, you can modify the ~/.ssh/config file.

Update with the following as appropriate:

Host 192.168.1.126
  IdentityFile ~/.ssh/pi_hole_id_rsa

Now you should be able to do ssh pi@192.168.1.126 (or whatever else for the username) without having to enter your password.

Copy SSH Key to Server

Simple enough:

ssh-copy-id -i ~/.ssh/pi_hole_id_rsa.pub pi@192.168.1.126

Create a Host Alias

If you want to get even cleaner, edit ~/.ssh/config some more to include User, HostName, and even a Port:

Host pihole
  User pi
  HostName 192.168.1.126
  Port 22
  IdentityFile ~/.ssh/pi_hole_id_rsa

Now you can use ssh pihole to access the server.