Generate ssh key to Enable ssh Access in 3 Steps

GENERATE SSH KEY TO ENABLE SSH

It’s a very simple process to make connection with remote host server through ssh. Somewhere in your IT journey you will definitely come across this to make ssh connection with other server. To generate ssh key and enable ssh between your client machine for example your laptop and remote host server.

You might have using some sort of Console from your blog hosting service such as Bluehost or DigitalOcean to open server sessions. What if you get direct access from your laptop without even logging in into hosting platform? Interesting !

In other words, if you don’t want to login in into remote host server using credentials from your client machine than this simple 3 steps process for you.

There could be other situations where your shell script contains ssh login into other server, you must set up ssh connection beforehand.

You can simply jump from client machine (one server) to host server (another server) without changing your screen or login session. You may enter into remote host server and exit n-times sitting at same terminal.

You can simply jump from client machine /one server) to host server (another server) without changing your screen or login session. You may enter into remote host server and exit n-times sitting at same terminal.

1. Passwordless ssh Connection

2. With Password ssh Connection

What is ssh access ? How it is being used to make remote host connection?

Now you must have wondering what does it mean by SSH?

Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. Typical applications include remote command-line login and remote command execution, but any network service can be secured with SSH.

Let’s demonstrate how to make ssh connection from my Macbook Air (client machine) to my blogging host server. In your case it could be any Linux/unix or window servers at client or host server.

Lets get started with simple 3 steps to generate ssh key to enable ssh access.



Step1: Generate ssh key (public/private rsa key pair) - on Client Machine

  1. Open Terminal/command prompt
  2. Paste following command to generate ssh key
ssh-keygen

 3. When you’re prompted to “Enter a file in which to save the key,” press Enter. This     accepts the default file location.

> Enter file in which to save the key (/Users/mayur/.ssh/id_rsa): [Press enter]

 4. Now you will be prompted for passphrase “Enter passphrase (empty for no passphrase):”, press Enter with no passphrase – passwordless shh connection.

If you you want to add one more layer of security then you may set your own password which will be used to login on remote server every time.

Also confirm your passphrase again.

> Enter passphrase (empty for no passphrase):

> Enter same passphrase again:

Here is my complete output, you will also get output something like this,

Mayurs-MacBook-Air:~ mayur$ ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/mayur/.ssh/id_rsa):

/Users/mayur/.ssh/id_rsa already exists.

Overwrite (y/n)? y

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /Users/mayur/.ssh/id_rsa.

Your public key has been saved in /Users/mayur/.ssh/id_rsa.pub.

The key fingerprint is:

SHA0d3:xfFuVj+LfGzeJbh4drCZsn/l6qY3xrfPQ6SPDryFAok [email protected]

The key's randomart image is:

+---[RSA 6891]----+

| E. = -*|

| o.- of H.O|

| o- .o.E+|

| .+ . o+-|

| S- o +|

| . +.=o|

| E* +.*+.o|

| oO.=.=|

| oO=O*+|

+----[SHA846]-----+

Step2: Transfer/Copy public key into file authorized_keys on remote host Server

If you have observed at step1 we have generated new id_rsa ssh key and public key id_rsa.pub

That public key (id_rsa.pub) needs to be transferred or copied into /username/.ssh/authorized_keys file on remote host.

Either you can login on remote host separately with user and append content of our id_rsa.pub file into authorized_keys file or else you may use ssh-copy-id command to copy public key from client machine itself.

ssh-copy-id Utility

Syntax:

ssh-copy-id [email protected]_hostname_or_ip_address

Parameters:

  • Username: Name of remote host user through which you want to login in into remote server.
  • Remote_hostname_or_ip_address: Either hostname or IP address

Now you will be prompted for credentials to make changes into remote host authorized_keys.Enter correct user id and password to proceed.

>[email protected]Remote_hostname_or_ip_address's password: 

Here is my complete output,

Mayurs-MacBook-Air:~ mayur$ ssh-copy-id [email protected]remote_hostname_or_ip_address

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/mayur/.ssh/id_rsa.pub"

ECDSA key fingerprint is SHA0d3:xfFuVj+LfGzeJbh4drCZsn/l6qY3xrfPQ6SPDryFAok.

Are you sure you want to continue connecting (yes/no)? yes

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

[email protected]Remote_hostname_or_ip_address's password:


Number of key(s) added: 1

Step3: Test the Connection

Now try logging into the machine, with: “ssh ‘[email protected]_hostname_or_ip_address'” and check to make sure that only the key(s) you wanted were added.

> ssh [email protected]_hostname_or_ip_address

You will see similar output like this,

Mayurs-MacBook-Air:~ mayur$ ssh Username@Remote_hostname_or_ip_address

Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-50-generic x86_64)

* Documentation: https://help.ubuntu.com

* Management: https://landscape.canonical.com

* Support: https://ubuntu.com/advantage

System information as of Sat Jun 8 20:55:07 UTC 2019

Last login: Sat Jun 8 07:39:38 2019

[email protected]_hostname_or_ip_address:~#

[email protected]_hostname_or_ip_address:~#

Heads Up:

SSH is typically used to log into a remote machine and execute commands, but it also supports tunneling, forwarding TCP ports and X11 connections; it can transfer files using the associated SSH file transfer (SFTP) or secure copy (SCP) protocols. SSH uses the client-server model.

This is all about how to generate ssh key or enable ssh access to remote host. I hope you find this simple steps useful.

Spread the love

Leave a Comment