Simplify your life with an SSH config file and ClusterSSH

Secure Shell (SSH) client-server encrypted connection icon

If you’re anything like me, you probably log in and out of a half dozen remote servers on a daily basis. And if you’re even more like me, you have trouble remembering all of the various usernames, remote addresses and command line options for things like specifying a non-standard connection port or forwarding local ports to the remote machine. And if you sometimes have to do the same thing across many servers, this post is for you.

One option would be to create a bash alias for each remote server. However, there is another, much better, and more straightforward solution to this problem. OpenSSH allows you to set up a per-user configuration file where you can store different SSH options for each remote machine you connect to.

The OpenSSH client-side configuration file is named config and is stored in the .ssh directory under the user’s home directory.

The ~/.ssh directory is automatically created when the user runs the ssh command for the first time. If the directory doesn’t exist on your system, create it using the command below:

mkdir -p ~/.ssh && chmod 700 ~/.ssh

By default, the SSH configuration file may not exist, so you may need to create it using the touch command:

touch ~/.ssh/config

This file must be readable and writable only by the user and not accessible by others:

chmod 600 ~/.ssh/config

Typically, when connecting to a remote server via SSH, you would specify the remote user name, hostname, and port. For example, to log in as a user named john to a host called dev.example.com on port 2322 from the command line, you would type:

ssh [email protected] -p 2322

The SSH Config File takes the following structure:

Host hostname1

SSH_OPTION value

SSH_OPTION value

Host dev

HostName dev.example.com

User john

Port 2322

Host *

SSH_OPTION value

The contents of the SSH client config file is organized into stanzas (sections). Each stanza starts with the Host directive and contains specific SSH options used when establishing a connection with the remote SSH server.

Indentation is not required but is recommended since it makes the file easier to read.

The Host directive can contain one pattern or a whitespace-separated list of patterns. Each pattern can contain zero or more non-whitespace characters or one of the following pattern specifiers:

* – Matches zero or more characters. For example, Host * matches all hosts, while 192.168.0.* matches hosts in the 192.168.0.0/24 subnet.

? – Matches exactly one character. The pattern, Host 10.10.0.? matches all hosts in the 10.10.0.[0-9] range.

! – When used at the start of a pattern, it negates the match. For example, Host 10.10.0.* !10.10.0.5 matches any host in the 10.10.0.0/24 subnet except 10.10.0.5.

The SSH client reads the configuration file stanza by stanza, and if more than one pattern matches, the options from the first matching stanza take precedence. Therefore, more host-specific declarations should be placed at the beginning of the file, and more general overrides at the end of the file.

You can find a full list of available ssh options by typing man ssh_config in your terminal or visiting the ssh_config man page.

The SSH config file is also read by other programs such as scp, sftp, and rsync.

ClusterSSH

If you need to type the same command into several machines at once, you can log in to each one with SSH and do it serially, or you can save yourself a lot of time and effort and use a tool like ClusterSSH.

To install it, just run:

sudo apt install clusterssh

Once installed, you can pass host aliases directly from your ~/.ssh/config file or group hosts inside ~/.clusterssh/clusters:

# Launch ClusterSSH across three hosts at once
cssh web1 web2 web3

# Or define reusable groups in ~/.clusterssh/clusters
prod-web web1 web2 web3
cssh prod-web

Every keystroke in the ClusterSSH control window broadcasts simultaneously to all open terminals, which works well for quick interactive checks. For modern, repeatable fleet operations, pair ~/.ssh/config with tmux (:setw synchronize-panes on), pssh, or idempotent Ansible playbooks.

Source: linuxize & linux.com

Comments are Disabled