If you want work with many remote Linux machines, managing and remembering IP addresses, user name and keys would be little hard. If you are using ssh client like putty, you can store the session but if you are the person who uses a terminal to do ssh there should be managed away to connect remote machines.
In this guide, you will learn to make use of ssh config file which stores all you ssh information that’s needed for the ssh connection.
SSH Connections for Multiple Machines
Every Linux and Unix based machines have a .ssh folder where you keep you ssh keys. In this folder, you can have a file named “config” and you can put all the remote machine details in this file.
Now create a config file in your .ssh folder using the following command.
A sample configuration is shown below.
Host nodejs HostName nodejs.myserver.com Port 22 User yahooda Host nodejs HostName database.myserver.com Port 22 User yahooda IdentityFile ~/.ssh/mykey.pem
In the above snippet, we have declared “nodejs” (Host) as an identifier for our remote host. All the other parameters are self-explanatory. If your server does not have a domain name, you can give the IP address instead of domain name.
Connecting Configured Machines
Now you have entered all the configurations that are needed to connect to remote machines. Now to connect, all you have to do is just use ssh command with the identifier you used in the config file. An example is shown below.
The above command will look for nodejs identifier in the config file and will pick all the parameters from the config file for the remote ssh connection.
Defining Common parameters
Let’s say you need to manage ten servers and all the servers have the same username and ssh key. In this case, you can define the common parameters under one block with a regular expression. An example for defining the common parameters are shown below.
Host dev Host web.node1 Host web.node2 Host * Port 4556 User jordan IdentityFile ~/.ssh/jordan_privatekey Host web* Port 4668 User james IdentityFile ~/.ssh/james_privatekey
In the above example
Host * means, the parameters described in that block applies to all the servers.
Host web* means, the parameters described in that block applies to all the host definitions that start with the web keywords in the identifiers.