Sam Hooke

SSH tips

Following are some tips to help remind myself how to perform SSH tasks that I do less frequently.

Passwordless SSH login §

Passwordless SSH login is when you run ssh user@server, and are automatically logged in without being prompted for a password.

There are essentially just two steps for setting up passwordless SSH login:

  1. Generate a key.
  2. Install the key on the relevant server(s).

Generate the key §

Simply run:

ssh-keygen

When prompted for a passphrase, just press enter.

If you do enter a passphrase, you will be prompted for that passphrase when you try to log in to a server with your key.

Install the key (automatically) §

Simply run:

ssh-copy-id user@server

This will install your key to the specified server. You will be prompted for a password (but hopefully this will be the last time!).

Install the key (manually) §

On some systems, the ssh-copy-id script may not exist. For example, on Windows 10. Fortunately, the script itself is fairly simple, so you can manually perform it by doing the following:

# Use scp to copy your public key to the target server
scp ~/.ssh/id_rsa.pub user@server:my_id_rsa.pub

# Log in to the target server
ssh user@server

# Append your public key to the authorized keys
cat my_id_rsa.pub >> ~/.ssh/authorized_keys

SSH tunneling §

Can SSH tunnel through a gateway:

ssh -L local_port:target_ip:target_port user@gateway_ip

Can do SSH tunnels to multiple ports/hosts in one go:

ssh -L local_port_a:target_ip_a:target_port_a -L local_port_b:target_ip_b:target_port_b user@gateway_ip

These are rough notes from whatever I was working on, interested in or thinking about at the time. They vary greatly in quality and length, but prove useful to me, and hopefully to you too!

← Previous: mypy and verbose logging
Next: Sonic Pi on Debian →