SSH key-based authentication

Open SSH By default stores key pairs in the user’s home ~/.ssh directory.

Start with checking the containents of users home directory to confirm exsisiting key pair.

ls ~/.ssh

If there is a key pair it will look like this

id_rsa
id_rsa.pub

After typing

ssh-keygen 

During this process If there are a key pair in your ~/.ssh/ directory,

You will be aked to keep or Generate new key pair.

To use your exsisting keys type no

Warning Selecting yes is an irreversible destructive process.

/home/username/.ssh/id_rsa already exists.
Overwrite (y/n)?

To generate a new 2048-bit RSA key pair.

  1. Agree to Use the default location ie ~/.ssh/
  2. Setup new passphrase/password

Use ssh agent to aviod being prompted for a password

ssh kegen 
Created directory '/home/username/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again: 
  • Upon succeces the terminal will output similar to below.
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
The key fingerprint is:
a9:49:EX:AM:PL:E3:3e:a9:de:4e:77:11:58:b6:90:26 username@203.0.113.0
The key's randomart image is:
+--[ RSA 2048]----+
|     ..o         |
|   E o= .        |
|    o. o         |
|        ..       |
|      ..S        |
|     o o.        |
|   =o.+.         |
|. =++..          |
|o=++.            |
+-----------------+

Display the keys pair and

ls ~/.ssh/
id_rsa
id_rsa.pub

Display the the public key

cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAUxXyRfAyhF/rEXwmWkup+pDrdr0/OMb3B8jOhjCXavVEmo7ff/cyDQ0FLqJtiReP12UC50M3M4ALHi98ulG9hJe7IE/ user@example

SSH client custom configuration file,

Simple example

Host dev-server             #Hostname(alias)
    Hostname 10.10.0.1      #Ip address or daomain name (example.com)
    User foo                #User name for login
    Port 2299               #Custom port

With this in place we can login using

ssh dev-server

Note: We dont have to type the full ssh url prefix(ssh foo@10.10.01:2299).

Connecting via, ssh:// Work around for host key verification. Gogs or Gitea URL.

If you get error like bleow

Host key verification failed.
fatal: Could not read from remote repository.

To Disable key checking for specific host.

Host dev-server
    Hostname 10.10.0.1
    User foo
    Port 2299
    StrictHostKeyChecking no

Disable host key checking for all hosts Not recommanded on production

Host *
    StrictHostKeyChecking no