UNIX & GNU/Linux - SSH - Generating the public and private keys with a CLI

Using SSH is helpful in many situations.

But before using it, it's essential to have the famous public and private keys.

This SSH tutorial is for Windows and Linux users but feel free to inspire yourself for another operating system.

Let's see that.

First of all

In this SSH tutorial we are going to use a CLI (Command Line Interface).

I've chosen Git Bash but of course you can use the CLI of your choice (Cygwin for example) on Windows or simply your usual terminal on Linux.

To download Git + Git Bash: https://git-scm.com/downloads.

SSH keys generating 

We need private and public SSH keys.

At this point you have two situations:

  1. Having already a .ssh directory with id_rsa and id_rsa.pub files within
  2. Not having a .ssh directory yet or having a .ssh directory but empty

You already have a .ssh directory with id_rsa and id_rsa.pub files within

If you already have a .ssh directory on your computer then you can use it or generate another pair of ssh keys.

To check it, generally it's installed in your home directory, such as:

  • C:/Users/Mi-K/.ssh/
  • /home/mi-k/.ssh

The directory here is: ".ssh".

Inside there are normally 2 files such as:

  • id_rsa
  • id_rsa.pub

You can rename as your needs, it can be useful if you want to have different SSH keys for different goals.

For example if you want to use use it exclusively for a specific program and having another need for another one.

So you can rename it like that:

  • program1_id_rsa
  • program1_id_rsa.pub

But if you change these names, you'll have to precise it with the -i option like this:

ssh user@program1 -i /path/to/the/.shh/program1_id_rsa.pub

It's up to you.

You don't have a .ssh directory yet or your .ssh directory is empty

So, if you don't already have this kind of directory, we are going to create one.

Open up your Git bash, then type:

ssh-keygen

It will show you something like this:

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Mi-K/.ssh/id_rsa):
Click ENTER to accept.
Created directory '/c/Users/Mi-K/.ssh'.
or on Linux:
Created directory '/home/mi-k/.ssh'.
At this point, you can enter a passphrase (it's a password) but if you do it, you'll have to type it each time you'll have to communicate with your server.
It's of course an additional security measure.
Enter passphrase (empty for no passphrase):
But for our tutorial, we won't use any passphrase, so just click ENTER.
Enter same passphrase again:
Then click ENTER again.
Your identification has been saved in /c/Users/Mi-K/.ssh/id_rsa.
Your public key has been saved in /c/Users/Mi-K/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:z761CuJOXxE/6H97MFnTt1NnPDGf6pSwONf1hekNxiM Mi-K@PC-Mi-K
The key's randomart image is:
+---[RSA 2048]----+
|             . BB|
|          . E O.#|
|          .+.= B@|
|          .++.++o|
|        S .oo+...|
|         o .o. ..|
|      o . = .. .o|
|     . o + . .  +|
|     .o . +..  ..|
+----[SHA256]-----+
Done.
 
You can now check your .ssh directory to see your 2 new files.

Conclusion

It wasn't finally a big deal!

SSH is often use to have safely communication between server and client sides.

You are ready to use SSH with your CLI and the programs you want to communicate with.

Good job you did it. cool

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.