This article will help you to properly organize your ssh server details with key files.
Configuration File Syntax:
We can add multiple ssh hosts details to <strong>~/.ssh/config</strong> file. Edit configuration file in your favorite editor like vi, vim or nano.$ viThe syntax will be like below.~/.ssh/config
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. Add First SSH Host
For example we have our first SSH host is running a PHP development web server with details nick name as php-web1, user root, port 22 and accessible through password. Add the following content in configuration file.Host php-web1 HostName 192.168.1.100 User rootNow try SSH as following command.
$ ssh php-web1
2. Add Second SSH Host
Our second host server (php-web2) is accessible with ssh key-pair with user root on default port 22. Add the following content in configuration file.Host php-web2 HostName 192.168.1.101 IdentityFile ~/.ssh/php-web2.pem User rootNow try SSH as following command.
$ ssh php-web2
3. Add Third SSH Host
Our third ssh host server (php-db1) is running on port 2222, accessible though key-pair with user ubuntu. Add the following content in configuration file.Host php-db1 HostName 192.168.1.110 Port 2222 IdentityFile ~/.ssh/php-db1.pem User ubuntuNow try SSH as following command.
$ ssh php-db1
4. Setup Forwarding with SSH
In this setup we need to forward our local system port 3306 to remote servers (php-db1) hosts on port 3306 . Add the following content in configuration file.Host php-db1-mysql-tunnel HostName 192.168.1.110 Port 2222 IdentityFile ~/.ssh/php-db1.pem LocalForward 3306 127.0.0.1:3306Now try SSH as following command.
$ ssh php-db1-mysql-tunnel
Final Configuration File
Your final configuration file <strong>~/.ssh/config</strong> will look like below.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
Comments
Post a Comment