square-terminalSSH

Setup SSH authentication on a Linux server from Linux/Windows

Generate SSH Keys

ssh-keygen

Copy Public Key to Remote Linux Host

From Linux:

circle-info

To connect via SSH key, you must put your Public Key in the remote server's ~/.ssh/authorized_keys file (requiring initial access like physical or password ssh). Your Private Key is your secret proof of identity and must never leave your local machine.

ssh-copy-id <user>@<hostname> # -p <port-number> (optional)

OR

cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'umask 0077; mkdir -p .ssh; cat >> .ssh/authorized_keys && echo "Key copied"'

From Windows:

type $env:USERPROFILE\.ssh\id_rsa.pub | ssh <hostname> "cat >> .ssh/authorized_keys"

(Optional) SSH Config

Create .ssh/config file

Host MyServer
  HostName <hostname>
  User <user>
  IdentityFile ~/.ssh/id_rsa

Host blabla
  HostName <hostname>
  User <user>
  IdentityFile ~/.ssh/id_rsa_blabla

You can then connect easily to ssh like this:

If you use VSCode, it will also detect your SSH Hosts from config file.

circle-info

You might encounter an issue with VSCode where it'll ask you everytime the Remote Platform, here's how to fix it

  1. Press F1 and open user Settings (JSON)

  2. Add your remote hosts like this:

Last updated