how to ssh with multiple servers on same host

OK, this is a pretty common scenario, where someone has many machines with SSH servers behind their router, and only one external IP address, so the internal SSH servers are forwarded from different external ports. Unfortunately, this gives you problems managing SSH host keys, because SSH only associates the hostname (and not the port) with the key by default.

The way to fix this is to create an alias for each server that is behind the firewall. Create a stanza such as the following in your $HOME/.ssh/config:

Host leenux
HostName myhost.dynip.com
Port 2206
HostKeyAlias leenux

Then, when you want to connect to that particular internal machine, use 'leenux' as the host when you invoke ssh. When you use that, it will connect to the host myhost.dynip.com:2206, but it will store the key under the alias instead of the actual hostname. So as long as you don't have conflicting aliases, you should not encounter key conflicts anymore.

Leave a Reply