Ssh Private And Public Key



  1. Ssh Using Public Key
  2. Ssh Command With Key
  3. Test Ssh Private And Public Key
  4. Ssh Public Key Example

The.pub file is your public key, and the other file is the corresponding private key. If you don’t have these files (or you don’t even have a.ssh directory), you can create them by running a program called ssh-keygen, which is provided with the SSH package on Linux/macOS systems. You need your SSH public key and you will need your ssh private key. Keys can be generated with ssh-keygen. The private key must be kept on Server 1 and the public key must be stored on Server 2. This is completly described in the manpage of openssh, so I will quote a.

I am a private Linux user since many Years and I always have used combination of username and password to authenticate when logging via Secure Shell (SSH). It worked pretty well for Years but recently as my Linux footprint grew it started to be very tiresome. I knew that there is this “magical way” to login without password but I was reluctant to try it out. Once I tried it out I loved it and I never ever want to user password to authenticate with SSH. In this post you will learn how you can use combination of public and private keys to login to VMware ESXi.

Thats a very good question. Public Key Infrastructure Public Key Infrastructure - Wikipedia is a very secure way of generating and using a pair of keys - public and private one. Public key can be shared with everyone and private one should be stored securely. In every modern operating system you can create such a key pair and login to SSH enabled systems.

How to create a public / private key pair?

Generally speaking it is very easy and can be done in several ways. It differes per Operating System.

macOS

In macOS you can do it in terminal of your choice.

  1. Open Terminal.
  2. Type ssh-keygen.
  3. You will be asked where you want to store your keys.
  4. Optionally you can secure your keys with a passphrase.
  5. Pair of keys will be generated.

Linux

In Linux procedure is pretty much the same as in macOS :D

Windows

In Windows keys creation of course might be different based on software you use. In my case I am using free PuTTY to login via SSH. In order to generate SSH keys you need to start software called PuTTY Key Generator - puttygen.exe.

  1. Start puttygen.exe.
  2. Choose type of key you want to generate. I suggest as minimum RSA with 2048 bits.
  3. Click Generate.
  4. You will need to generate some randomness with your mouse.
  5. Once done you can configure authentication with keys in your PuTTY session.

Where to store you public keys?

In my case I am using two macOS laptops and one Windows 10 based computer (to play some games). I have different SSH keys on each computer. I added all SSH keys to my GitHub account and whenever I need to retrieve them I simply check them using following command.

wget https://github.com/wojciehm.keys

Procedure is quite simple.

  1. Login to VMware ESXi Server using SSH.
  2. Edit following file /etc/ssh/keys-root/authorized_keys and add your keys.
  3. Execute following command to restart SSH service. /etc/init.d/SSH restart.
  4. Try logging in and mark that you don’t need password anymore.

This tutorial explains the Passwordless SSH using Public Key and Private Key in Linux.

Ssh Private And Public Key

SSH stands for Secure SHELL, is a protocol used to connect remote hosts to login or performing some tasks using scripts.

When we want to automate some tasks on remote hosts using scripts from a centralized server like Jenkins/Ansible or any Linux Server, we may require a password less connection between the remote hosts and the centralized Server.

In this tutorial, we will learn to create Passwordless SSH login using public key and private key. Follow the step by step guide to make your ssh connection passwordless.

This tutorial will work for Linux Destro such as Centos, Ubuntu, Redhat, Amazon Linux(AWS EC2) and Other as well.

Recommended Read:How to Install Jenkins on Ubuntu

Also Read : Git Tutorial for beginners (Part I)

Scenario

We have one Local Machine and one Remote Server.We will setup a passwordless connection to login Remote Server from the local Machine.

Perform following steps on the remote Server

Step 1– Create an User and login or login as an existing user.

$ useradd devops

$ su – devops

Step 2 – Generate a key pair ( Public key and Private Key) using ssh-keygen command.

Before running this command make sure you are on home directory of the user.If not you can go to the home directory by cd ~ command.

$ cd ~

$ ssh-keygen -t rsa

It will ask for some details. Do not put anything here and press ENTER only.

By ls -al command you can see a hidden directory .ssh and two files namely id_rsa and id_rsa.pub inside .ssh directory are created.Here id_rsa is the Private key and id_rsa.pub is the Public Key.

Private key(id_rsa) is kept at source computer(local machine) from where you have to ssh. Public Key(id_rsa) is kept at Destination Server(Remote Server) , the Server you want to access.

Step 3- Create a file name authorized_keys in side .ssh directory and copy the content of id_rsa.pub file to authorized_keys file.

Go to .ssh directory

$ cd ~/.ssh/

Create an empty file name authorized_keys

$ touch authorized_keys

Copy the content of id_rsa.pub to authorized_keys

$ cat id_rsa.pub > authorized_keys

Check the authorized_keys file if contents are copied.

Public

$cat authorized_keys

Step 4 – Change the permission of authorized_keys

$ chmod 600 authorized_keys

Step 5– Copy the content of id_rsa file

Use cat command to display the content of id_rsa and copy its content.

$ cat id_rsa

On the local Machine

Step 1– Create a file and paste the content of id_rsa copied from remote server inside this file. You can use nano command to perform this action.

Create a file name devops.key using nano command , paste the content and pres Ctrl+X to save and close the file.

$ nano devopys.key

Step 2 – SSH remote Server from local machine without using password.

Ssh Using Public Key

$ sudo ssh -i path-to-private-key [email protected]

Ssh Command With Key

$ sudo ssh -i devops.key [email protected]

I hope you enjoyed this tutorial and learned Passwordless SSH login using public key and private key. If you think this is really helpful, please do share this to other as well. Please also share your valuable feedback, comment or any query in the comment box.I will really happy to resolve your all queries.

Thank You

Test Ssh Private And Public Key

If you think we helped you or just want to support us, please consider these:-

Ssh Public Key Example

Connect to us: Facebook | Twitter





Comments are closed.