SSH into Multiple Servers using MultiSSH
When you need to run the same maintenance commands on multiple servers, one option is to use a tool called MultiSSH (sometimes referred to as mssh). MultiSSH is a basic tool that allows you to connect to multiple SSH servers and run the same commands simultaneously on all of the connected servers. MultiSSH also allows you to run commands on a single server, a group of connected servers, or all of the connected servers. If you’re not using Linux, there are alternatives to MultiSSH, and alternatives also exist for Linux.
Installing and Using MultiSSH
Right now MultiSSH’s package (mssh) is available in the Debian and Ubuntu package repository and can easily be installed. Using apt install mssh
you can download and install MultiSSH. It’s a very small program with few common dependencies that you most likely already have installed.
Once MultiSSH is installed, you have to initiate the program from the terminal instead of through a menu entry. When running the mssh
command, you have to provide at minimum one host (or an alias of a server) for the program to start, but other servers can be added once the program loads. Each host entry can include the SSH user along with the SSH port number. If you leave out the port and user, the port defaults to 22 and the user defaults to your current username. It is possible to connect to the same server multiple times as different users (or even the same user).
$ mssh user@192.168.0.108:2222 user@192.168.0.109:2222
The graphical interface of the program is fairly straight forward to use. You can either focus on a single server by clicking on the terminal for the specific server and run commands for that server alone. To run commands on all of the servers, use the input field on the top of the MultiSSH window. All inputs will be sent to all of the servers (enter, tab, arrow keys, etc). You can enable and disable servers that you are connected to by using the menu “Servers” and deselecting and selecting servers where commands should be sent. There is also a “Commands” menu that will be populated once you setup a configuration file (see below).
To connect to additional servers, you want to use the File menu and use the option “Add session” and specify the server in the format of <user>@<hostname>:<port>
. The port can be omitted if it’s 22, and the user can be left out if the username is the same as the user you’re currently logged in as.
MultiSSH Configuration, Aliases, and Server Groups
MultiSSH reads the configuration file $HOME/.mssh_clusters
which can specify aliases for servers and commands. This configuration file is very useful if you have a large list of servers and want to group servers together. An alias can also specify other aliases to create an alias for all servers or other variants. Below is a small example of a .mssh_clusters
file.
web-servers: web@192.168.0.108:2222 web@192.168.0.109:2222
database-servers: database@192.168.0.110:2222
all: [web-servers] [database-servers]
# Command alias are defined using curly brackets {}
{Update and Upgrade} apt update && apt upgrade\n
As you can see, server aliases are defined in the format of <alias-name>: <servers>
. You can reference another alias by wrapping the alias in square brackets in the server list. It’s also possible to define a command alias by using curly brackets instead of a colon. Follow the alias with a line-break (i.e. \n
) if you want the command to execute instantly without the need to press enter after selecting the command from MultiSSH.
To use a server alias when running mssh
, you have to prefix the alias name with the flag -a
. Your command will look similar to mssh -a web-servers
.
MultiSSH Pros and Cons
There are a few advantages of using MultiSSH instead of manually connecting to all of your servers manually. Firstly, you will save a lot of time if you have your SSH keys configured properly and have created a useful MultiSSH configuration file and aliases. Secondly, your servers should all remain consistent when fixing problems. Instead of patching one server at a time and slightly differently, all servers will be patched in the identical manner.
Sadly, there are a few downfalls to using MultiSSH and other multiple ssh clients. The first problem is you can introduce new issues on servers if you are running commands carelessly. For example, if you are using tab completion for commands, you could run the incorrect command on a few servers if you are not paying attention or run commands on the incorrect files or devices. Another problem is it’s difficult to edit files on multiple servers at the same time. All keys are sent to all of the selected servers at the same time, but if the file’s contents vary between servers, you won’t be able to edit the file correctly on all of the servers at the same time.