If you want to work with many remote Linux machines, managing and remembering IP addresses, user names and keys would be a little hard.
If you are using ssh clients like putty, you can store the session, but if you are the devops engineer who uses a terminal to do ssh, there should be a managed way 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 machine has a .
ssh folder where you keep your 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
We have declared “
nodejs” (Host) as an identifier for our remote host in the above snippet. All the other parameters are self-explanatory. For example, if your server does not have a domain name, you can give the IP address instead of the 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 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 SSH Parameters
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 is 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 apply to all the servers.
Host web* .The parameters described in that block apply to all the host definitions that start with the web keywords in the identifiers.