LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > Elizine
User Name
Password

Notices


Rate this Entry

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 –
Code:
touch ~/.ssh/config
Here’s a sample configuration –
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
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 –
Code:
ssh nodejs
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 –
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
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.
Posted in Uncategorized
Views 1491 Comments 2
« Prev     Main     Next »
Total Comments 2

Comments

  1. Old Comment
    In this context, would mentioning utilities such as mpssh not be appropriate?
    Posted 06-03-2016 at 05:17 AM by wpeckham wpeckham is offline
  2. Old Comment
    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 Turbocapitalist is offline
 

  



All times are GMT -5. The time now is 04:45 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration