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.
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.
- 1 Benefits of using ssh Connection
- 2 What is ssh access ? How it is being used to make remote host connection?
- 3 Step1: Generate ssh key (public/private rsa key pair) - on Client Machine
- 4 Step2: Transfer/Copy public key into file authorized_keys on remote host Server
- 5 Step3: Test the Connection
Benefits of using ssh Connection
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.
Step1: Generate ssh key (public/private rsa key pair) - on Client Machine
- Open Terminal/command prompt
- Paste following command to generate ssh key
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:
| E. = -*|
| o.- of H.O|
| o- .o.E+|
| .+ . o+-|
| S- o +|
| . +.=o|
| E* +.*+.o|
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 [email protected]_hostname_or_ip_address
- 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
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
This is all about how to generate ssh key or enable ssh access to remote host. I hope you find this simple steps useful.