Simple Commands to Manage SSH Connections for Multiple Machines
Posted 06-02-2016 at 05:34 AM by Elizine
Are you working with many remote Linux machines? It must be hard for you to memorize the IP addresses, keys and user names. It becomes easy with SSH client like putty but if you are using the terminal to do SSH there should be a proper way to connect remote machines.
Here’s a guide for you on making use of SSH config file that stores all the essential information required for the SSH connection.
Script for making SSH Connections for Multiple Machines –
As you are using Linux web server, you might be aware about the .ssh folder where the SSH keys are saved. This folder contains a file named as “config” and you can store all the remote machine details in this file.
Below is the command for creating a config file in your .ssh folder –
Here’s a sample configuration –
The above snippet represents “nodejs” (Host) that acts as an identifier for our remote host. If the server isn’t having a domain name, an IP address can be allocated instead of it.
Configured Machines Connection
The configurations required to connect to remote machines have been already entered now. In order to connect, you simply need to use SSH command with the identifier used in the config file as below –
This command will search for the nodejs identifier in the config file and select the parameters from the config file for the remote SSH connection.
Describing the Common Parameters
Suppose you need to manage ten servers and all have the same username and SSH key. In this case, you can outline the common parameters under one block with a regular expression. Below is an example –
In the above command, Host* means the parameters mentioned in that block apply to all the servers.
Host web* means, the parameters mentioned in the block is applicable to all the host definitions starting with the web keywords in the identifiers.
Here’s a guide for you on making use of SSH config file that stores all the essential information required for the SSH connection.
Script for making SSH Connections for Multiple Machines –
As you are using Linux web server, you might be aware about the .ssh folder where the SSH keys are saved. This folder contains a file named as “config” and you can store all the remote machine details in this file.
Below is the command for creating a config file in your .ssh folder –
Code:
touch ~/.ssh/config
Code:
Host nodejs HostName nodejs.myserver.com Port 22 User yahooda Host nodejs HostName database.myserver.com Port 22 User yahooda IdentityFile ~/.ssh/mykey.pem
Configured Machines Connection
The configurations required to connect to remote machines have been already entered now. In order to connect, you simply need to use SSH command with the identifier used in the config file as below –
Code:
ssh nodejs
Describing the Common Parameters
Suppose you need to manage ten servers and all have the same username and SSH key. In this case, you can outline the common parameters under one block with a regular expression. Below is an example –
Code:
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
Host web* means, the parameters mentioned in the block is applicable to all the host definitions starting with the web keywords in the identifiers.
Total Comments 2
Comments
-
In this context, would mentioning utilities such as mpssh not be appropriate?
Posted 06-03-2016 at 05:17 AM by wpeckham -
Nice. One fine point is that in ssh_config(5) and with ssh(1) in general the first obtained value will be used in configuration. That means in the configuration file the general host patterns should follow the more specific host patterns. Otherwise you may get a different result than you aim for.
Posted 10-15-2016 at 06:31 AM by Turbocapitalist