If anyone finds these useful, here are some snippets from my ssh configuraion.
On the home automation PC:
/home/justin/.ssh/config:
Host hub hub.example.com
Hostname hub.example.com
User justin
IdentityFile /home/justin/.ssh/hub
Port 8222
RemoteForward 1880 127.0.0.1:1880
RemoteForward 8086 127.0.0.1:8086
This creates an ssh profile ‘hub’, so when you say ‘ssh hub’ it applies all these config options.
IdentityFile is an ssh private key (see ‘man ssh-keygen’) which would be created without a passphrase for automated connections.
‘Port 8222’ makes it connect to a non-standard port on the target hostname (port 22 is heavily scanned and can result in significant network traffic from bots/worms, so it is a good idea to use a non-standard port on internet accessible ssh services - it will still be discovered, but only by port scanners, which are rarer than service scanners). [NOTE2: It is also a really good idea to set up fail2ban on internet accessible servers - just to reduce traffic.]
‘RemoteForward 1880 127.0.0.1:1880’ sets up remote forwarding. When the ssh session is started, it creates a listening port (1880) on the host that you connect to. If anything then connects to this port on that host, then the connection is forwarded over the established ssh connection, and sent to the local address (127.0.0.1 port 1880).
So, with this config, when you connect to port 1880 on hub.example.com, you are actually connecting to port 1880 on the home automation PC.
[NOTE: You can also do something like ‘RemoteForward 8223 127.0.0.1:22’ to make the ssh port directly accessible on port 8223 of hub.example.com .]
/etc/systemd/system/ssh-hub.service:
[Unit]
Description=SSH Tunnel to google hub
After=network.target
[Service]
ExecStart=/usr/bin/ssh -NT -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes hub
User=justin
Group=justin
# Restart every >2 seconds to avoid StartLimitInterval failure
RestartSec=5
Restart=always
[Install]
WantedBy=multi-user.target
This creates a systemd unit ‘ssh-hub’. You can enable this with ‘systemctl enable ssh-hub’ then ‘systemctl start ssh-hub’.
With this installed and enabled, every time the PC starts it will try to fire up ‘/usr/bin/ssh -NT -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes hub’ (ssh to hub). The extra flags will make it die if the connection is lost or the forwards are broken.
Systemd will restart/keep trying this command if it dies/fails, so you end up with a fairly reliable permanent outbound ssh to hub.example.com (with all the port forwards established on that host).
On remote PCs
/home/justin/.ssh/config:
Host hub hub.example.com
Hostname hub.example.com
port 8222
LocalForward 1880 127.0.0.1:1880
LocalForward 8086 127.0.0.1:8086
On every PC I want to connect from (and on my phone, using ConnectBot) I have this ssh configuration.
So when I say ‘ssh hub’, it connects to hub.example.com on port 8222. But now we do ‘LocalForward’ instead.
‘LocalForward 1880 127.0.0.1:1880’ - this creates port 1880 on the local machine, and forwards any connection to this port to the local (127.0.0.1) port 1880 on hub.example.com - which in turn is forwarded to port 1880 on the home automation box.
Now open http://127.0.0.1:1880 to connect to node-red on the home automation PC.
[NOTE: you may need to change port numbers across various hops to avoid conflicts]
I also generally set ‘X11Forwarding yes’ in /etc/ssh/sshd_config on all hosts. Then I can type ‘ssh -X hub’ and any X applications I start will open their windows on my local PC.