CH-J Toolsserver management over SSH
Menu
All guides and articles →

How to manage several SSH servers at once

Keep several servers organised with SSH aliases and side-by-side terminals, and avoid running a command on the wrong one.

What goes wrong when several SSH sessions are open?

You have production in one terminal, a test server in another and a third connection showing logs. After switching windows to answer a message, you return to a familiar prompt and paste a command. The command may be correct while the destination is wrong. Managing several servers is therefore as much about identifying the active session as it is about opening connections.

A practical workflow has three separate checks: which saved destination you selected, which machine actually answered and whether your intended operation succeeded there. An alias helps with the first check, a host key and an identity command help with the second, and an application check handles the third. None of them replaces the others.

The examples use an OpenSSH client on Linux, macOS or Windows and Linux servers. Shell loops run in a local POSIX shell, such as Bash; they are not PowerShell syntax. The documentation addresses are placeholders, so substitute your own approved destinations before connecting.

Why do names, credentials and terminals become confusing?

Similar machines often share the same account name, working directory and shell prompt. A cloned installation may even retain an old hostname. A window title saying production is a useful reminder, but it does not prove which endpoint is connected. Likewise, the name you type after ssh can be a local alias rather than a DNS record.

  • Use a naming scheme such as prod-web-1 and test-web-1, with environment and role visible before any numbering.
  • Distinguish the alias from HostName, which supplies the actual destination address, and User, which selects the remote account.
  • Treat an SSH login key and a server host key as different identities: one authenticates your account, the other identifies the server.
  • Keep a small inventory linking each alias to its owner, role and expected address so you have something independent to check.

Avoid naming a replacement machine after a temporary address alone. Names should describe what an administrator needs to recognise during an incident. When a server changes role, review its alias and inventory entry together rather than leaving an old production label attached to a test system.

How do you diagnose an alias that reaches the wrong destination?

Start on the client. OpenSSH can print its effective settings without opening an SSH connection. Run the following for each suspect alias and inspect hostname, user, port and identityfile in the output. This diagnoses configuration selection; it does not prove that the destination is reachable or that authentication will succeed.

ssh -G prod-web-1

For the configuration shown in the next section, a relevant excerpt could look like this. It is example output, not a full listing:

hostname 203.0.113.10
user user
port 22
identitiesonly yes

Check for earlier matching Host blocks and command-line overrides. Most OpenSSH settings use the first obtained value, so put specific entries before general defaults. IdentityFile is an exception: multiple entries accumulate. A broad identity setting can therefore add keys even when a specific host has its own entry.

If the settings are correct but login fails, use verbose output on one host rather than retrying the whole group. A timeout, connection refusal and permission denial describe different stages. Review diagnostic output before sharing it because it can reveal account names, addresses and local paths.

ssh -v -o ConnectTimeout=5 prod-web-1

How do you set up consistent aliases step by step?

Edit the user configuration on the client, not the server configuration in /etc/ssh/sshd_config. On Linux and macOS the user file is ~/.ssh/config. With the Windows OpenSSH client it is normally C:\Users\user\.ssh\config; substitute your Windows profile directory and save the file without a .txt extension.

  1. Review the existing configuration and keep a backup before editing. Add entries instead of replacing unrelated hosts.
  2. Choose a unique alias for each environment and role, then confirm its address, account and SSH port against your inventory.
  3. Set IdentityFile to an existing private key on the client. The example filenames do not create keys or install public keys on servers.
  4. Put shared defaults at the end, inspect the result with ssh -G, then test each alias individually before using a loop.
Host prod-web-1
    HostName 203.0.113.10
    User user
    Port 22
    IdentityFile ~/.ssh/id_ed25519_prod
    IdentitiesOnly yes

Host test-web-1
    HostName 203.0.113.20
    User user
    Port 22
    IdentityFile ~/.ssh/id_ed25519_test
    IdentitiesOnly yes

Host *
    ServerAliveInterval 30
    ServerAliveCountMax 3

Use paths valid for your client and ports actually configured on the servers. On Unix clients, keep private keys accessible only to their owner; on Windows, use appropriate file ACLs. IdentitiesOnly limits the identities offered to those configured, which helps when an agent holds many unrelated keys.

How do you verify the active server before making changes?

Open one alias in each terminal and verify the first connection using a host-key fingerprint obtained from a trusted console or administrator. An unexpected changed key needs investigation before login. Once connected to a Linux server, run these read-only checks in that session:

hostname
whoami
pwd

Compare the returned hostname with your inventory, confirm the account and inspect the working directory. hostname -f is also useful where fully qualified naming is configured, but it may fail or return an unexpected name on systems with incomplete resolver settings. A hostname is a useful cross-check rather than a substitute for host-key verification.

Make the hostname and environment visible in prompts and window labels. Use text as well as colour so the distinction survives screenshots, different themes and limited colour perception. Before a privileged operation, repeat the identity check in the exact pane that will receive the command, especially after switching accounts or reconnecting.

Before deleting data, changing firewall access or restarting a critical service, check the target again and keep a tested recovery path available. A correct command on the wrong production server can cause data loss or lock you out.

How do you keep remote work running after disconnection?

Run tmux on the remote Linux server for work that needs to survive a terminal closing or a network interruption. A tmux running only on your laptop does not protect the remote shell from a lost SSH connection. Ensure tmux is installed on the server first, then create a named session after logging in:

ssh prod-web-1
tmux new -s maintenance

Start the job inside that session. To detach while leaving it running, press Ctrl+b, release the keys, then press d. After reconnecting to the same server with the same account, list the sessions and attach to the existing one:

tmux ls
tmux attach -t maintenance

Do not immediately launch the job again after losing the connection. Reattach and inspect what is still running first. Persistence also has limits: tmux does not survive a server reboot, and terminating its server process or session ends that protection. Account cleanup policies can affect background processes too.

The ServerAlive settings above help the SSH client detect an unresponsive connection. They cannot preserve a job or guarantee that a firewall will keep the connection open. For unattended recurring work, use the server’s job scheduling or service management facilities with logs and explicit success checks.

How do you check several servers without mixing their results?

Begin with read-only commands and an explicit, reviewed list of aliases. The following loop runs locally in Bash or another POSIX shell and queries Linux servers one at a time. Verify host keys and key-based login individually beforehand. BatchMode disables interactive authentication prompts, while StrictHostKeyChecking=yes refuses unverified host keys.

for host in prod-web-1 test-web-1; do
    printf "\n=== %s ===\n" "$host"
    if ssh -n -o BatchMode=yes -o StrictHostKeyChecking=yes \
        -o ConnectTimeout=5 "$host" 'hostname && uptime'; then
        printf "%s: check completed\n" "$host"
    else
        printf "%s: SSH or remote command failed\n" "$host" >&2
    fi
done

Each result has a visible alias header. The remote hostname helps detect inventory mistakes, and the failure message deliberately includes both connection and command failures. A nonzero SSH exit status is not proof that the machine is switched off. It can also mean authentication failed or the remote command returned an error.

ConnectTimeout limits connection establishment, not the total runtime of a remote command. A command that hangs after login can still delay the loop. An SSH-port check answers an even narrower question: whether a TCP connection can be made from your network. Neither result establishes that the web application or database is healthy.

After a real change, verify its intended effect on the selected server and then check the dependent service from a client. Keep the hostname with the result in your change record. For changes across a group, validate one machine before moving to the next.

Which habits prevent mistakes during routine administration?

A tidy workspace matters most when several machines look interchangeable. Keep the number of visible sessions small enough that you can read their labels and output. For a sensitive change, focus on a single target and leave monitoring output in a clearly separate view. More open panes do not automatically mean more control.

  • Do not broadcast privileged commands to every pane just because the servers have similar roles. Configuration drift can make the same instruction behave differently.
  • Do not disable host-key checking to make a group check complete. Investigate the particular host that failed verification.
  • Avoid pasting a whole maintenance procedure at once. Read the command, verify the target, execute one meaningful step and inspect the result.
  • Do not treat a successful SSH login as evidence that a backup, migration or application deployment completed successfully.
  • Review aliases after server replacements and remove obsolete entries once their use has ended. Keep private keys and passwords out of shared inventories.

Separate read-only fleet inspection from changes that need a maintenance window and a recovery plan. As the environment grows, use reviewed automation with an explicit inventory and per-host results. Preserve the same habits of target verification and post-change checks even when the commands are no longer typed manually.

How does CH-J Tools help with several servers?

CH-J Tools stores server profiles in a local encrypted vault and allows several SSH sessions to be open at once. In Alpha 0.1.1 two terminals can sit side by side or stacked, connected to one server or two. Hiding a terminal does not end its session.

Modules automatically use a single open connection and ask when several are open, so a module does not switch servers on its own. Locking the vault disconnects SSH sessions. Finish running work first or keep it in tmux on the server.

In Alpha 0.1.1 the availability monitor checks saved servers in the background by making a TCP connection to the SSH port, by default every 12 seconds, without logging in. Offline means that the SSH port cannot be reached from your network; the server itself may still be running.

Frequently asked questions

Can I use SSH aliases from Windows PowerShell?

Yes, when invoking the Windows OpenSSH client with the configuration in your user profile. The ssh alias command works there, but the POSIX shell loop in this guide requires a suitable shell or a separate PowerShell rewrite.

Does editing an SSH alias change an already open connection?

No. An existing session remains connected to its original endpoint. Reconnect deliberately after changing the configuration and verify the server identity again; an old terminal label may otherwise be misleading.

Why does BatchMode fail when normal SSH works?

Normal login may ask for a password, key passphrase or another interactive response. BatchMode prevents those prompts. Prepare approved noninteractive authentication, such as a key already available through your local agent, before running unattended checks.

Can two terminals attach to the same tmux session?

Yes. With access to the same tmux server, both can attach to that session and see the same activity. They are sharing a session rather than running independent copies of the job, so coordinate keyboard input.

Does a reachable SSH port mean the server is healthy?

No. It only establishes network reachability to that port from the checking location. Authentication, disk space, application responses and dependent services need their own checks.

Detailed documentation

OpenSSH: ssh_config →

OpenSSH: ssh →

OpenBSD manual: tmux →

Biometric unlock and security →

All tutorials by category →