Working with SSH keys for remote sessions in Linux - Kote

Working with SSH keys for remote sessions in Linux

SSH keys can be used as an excellent choice for authenticating remote hosts. Using keys instead of simple usernames and passwords have many advantages, however, what is more, essential for me to – use an ssh key helps establish a connection faster and more secure. Let’s review how to create, share and use keys.

In my setup, I am using wsl with ubuntu installed. Here is the list of commands that will help to use ssh keys..

How to generate an SSH key

By default, ssh creates a hidden directory for config files, and it is easier to run all the commands from that directory.

$ cd ~/.ssh/
$ ssh-keygen -t rsa -f aws.key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in aws.key.
Your public key has been saved in aws.key.pub.
The key fingerprint is:
SHA256:xjeOsfbbfiZfwLpm9Y5fF5p17COVINUjSSdYpqwHlnk userName@Host.com
The key's randomart image is:
+---[RSA 2048]----+
|            +*o. |
|          +.+ooo |
|         = E .. .|
|       .. + .....|
|        S.o.  o=o|
|       . *.. .*oo|
|        + . .= o=|
|       . . .+.+o=|
|          o=+=ooo|
+----[SHA256]-----+

Let’s check that public (aws.key.pub) and private (aws.key) are created:

$ ls
aws.key  aws.key.pub  known_hosts

Adding public ssh key to the remote host

To start using ssh keys for authentication, it is required to add public key to the remote host:

$ ssh-copy-id -i aws.key.pub userName@Remote_Server_Address.com

For the first time, it will be requested to enter a password for the specified ‘username’. After that connection will be done automatically.

How to setup ssh config file to use ssh keys

In case you have a few keys and (or) a few different servers, it is convenient to specify ssh keys in the config file:

$ cat ~/.ssh/config
#AWS HOST
Host Remote_Server_Address.com
HostName Remote_Server_Address.com
User userName
IdentityFile /home/userName/.ssh/aws.key

With parameters specified you don’t need to specify the ssh key with the ‘i’ parameter, and to initiate an ssh connection you can use a simple command like :

"ssh userName@Remote_Server_Address.com"