Trending News

Blog

SSH Hosts and Private Keys: The Best Way to Manage
How To

SSH Hosts and Private Keys: The Best Way to Manage 

SSh Hosts and Private Keys

You work with many remote networks on a daily basis as a server administrator. Throughout your job, you may need to SSH into systems several times. You can access several remote Linux servers with a password, and you can even access them with a private key. As a result, managing all of them might be more typical.

Table of Contents

This article will show you the best way to manage SSH Hosts and Private Keys.

SSH configuration file yehiweb

Syntax of the Configuration File:

Multiple ssh hosts may be defined in the /.ssh/config file. Use your favorite editor, such as vi, vim, or nano, to edit the configuration file.

				
					$ vi ~/.ssh/config  
				
			
The following is the syntax.
				
					Host <NICK_NAME>
     HostName <IP ADDRESS OF REMOTE>
     IdentityFile <PATH TO PRIVATE FILE>
     User <LOGIN AS USERNAME>
     Port <SSH PORT TO USE>
     LocalForward <LOCAL PORT>  <REMOTE_LOCATION:PORT>
				
			

1. Configure the First SSH Host

For instance, our first SSH host is running a PHP development web server with the following details: nickname php-web1, user root, port 22, and password access. In the configuration file, add the following content.

				
					Host php-web1
     HostName 192.168.1.100
     User root
				
			
Now try SSH with the command below.
				
					$ ssh php-web1
				
			

2. Configure a second SSH host

On default port 22, our second host server (php-web2) is accessible via ssh key-pair with user root. In the configuration file, add the following content.

				
					Host php-web2
     HostName  192.168.1.101
     IdentityFile ~/.ssh/php-web2.pem
     User root

				
			

Now try SSH with the command below.

				
					$ ssh php-web2
				
			

3. Add a third SSH host server

Our third ssh host server (php-db1) is accessible via key-pair with user ubuntu on port 2222. In the configuration file, add the following content.

				
					Host php-db1
     HostName 192.168.1.110
     Port 2222
     IdentityFile ~/.ssh/php-db1.pem
     User ubuntu
				
			

Now try SSH with the command below.

				
					$ ssh php-db1
				
			

4. Use SSH to set up forwarding

In this setup, we must forward port 3306from our local system to remote servers (php-db1). In the configuration file, add the following content.

				
					Host php-db1-mysql-tunnel
     HostName 192.168.1.110
     Port 2222
     IdentityFile ~/.ssh/php-db1.pem
     LocalForward 3306 127.0.0.1:3306
				
			
Now try SSH with the command below
				
					$ ssh php-db1-mysql-tunnel
				
			

File with Final Configuration

The following is an example of your final configuration file, /.ssh/config.
				
					Host php-web1
     HostName 192.168.1.100
     User root

Host php-web2
     HostName  192.168.1.101
     IdentityFile ~/.ssh/php-web2.pem
     User root

Host php-db1
     HostName 192.168.1.110
     Port 2222
     IdentityFile ~/.ssh/php-db1.pem
     User ubuntu

Host php-db1-mysql-tunnel
     HostName 192.168.1.110
     Port 2222
     IdentityFile ~/.ssh/php-db1.pem
     LocalForward 3306 127.0.0.1:3306
				
			
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit
Share on vk
Share on tumblr
Share on skype
Share on stumbleupon
Share on whatsapp
Share on email

Related posts

Leave a Reply

Required fields are marked *