When managing home servers and remote devices across changing environments—such as switching between a wired Ethernet link, a dual-band Wi-Fi connection (2.4GHz or 5GHz), and an external Tailscale tailnet—accessing machine via a fixed IP address quickly becomes frustrating.
While OpenSSH configuration files allow define explicit hosts, they evaluate entries sequentially and do not natively support automatic fallback when a preferred local IP is unreachable.
This post details how to implement an automated SSH fallback chain (Wired -> Wireless -> Tailscale) using Netcat (nc) and ProxyCommand, allowing connect seamlessly using a single ssh ubuntu command regardless of physical network state.
The Problem: Multiple Network Interfaces & Priority#
In previous network setups, hardcoding a single HostName in ~/.ssh/config caused connection timeouts whenever the device moved to a different interface (e.g., jumping from wired 192.168.0.50 to wireless 192.168.0.51). Although Tailscale provides secure remote access, remembering separate host aliases or dealing with local routing clashes disrupts workflow convenience.
Presentation is on: Tailscale Introduction Slides ↗
The objective is to establish a unified ubuntu host alias that transparently probes local subnet paths first before falling back to the Tailscale tailnet IP, all while sharing the same SSH private key and configuration profile.
The Solution: ProxyCommand Fallback Configuration#
Drawing inspiration from Bharat Kunwar’s approach on SSH fallback hosts with ProxyCommand, we can use nc to probe ports with a short timeout before opening the actual SSH tunnel.
Add the following configuration block ~/.ssh/config file:
Host ubuntu-wired
HostName 192.168.0.50
User ubuntu
IdentityFile ~/.ssh/ubuntu_ed25519
Host ubuntu-wireless
HostName 192.168.0.51
User ubuntu
IdentityFile ~/.ssh/ubuntu_ed25519
Host ubuntu-tailscale
HostName 123.123.123.123
User ubuntu
IdentityFile ~/.ssh/ubuntu_ed25519
Host ubuntu
User ubuntu
IdentityFile ~/.ssh/ubuntu_ed25519
ProxyCommand bash -c 'if nc -z -w 2 192.168.0.50 22 2>/dev/null; then nc 192.168.0.50 22; elif nc -z -w 2 192.168.0.51 22 2>/dev/null; then nc 192.168.0.51 22; else nc 123.123.123.123 22; fi'How the Proxy Command Works#
- Wired Probe (
192.168.0.50): Netcat tests port22with a 2-second timeout (nc -z -w 2). If active, the session pipes directly through the wired connection. - Wireless Fallback (
192.168.0.51): If the wired probe fails or times out, it immediately checks the wireless interface. - Tailscale Fallback (
123.123.123.123): If both local IPs are unreachable (like away from home), it automatically routes through Tailscale tailnet IP.
Testing and Verification#
1. Debugging the Connection#
To inspect how SSH resolves the proxy chain and establishes the handshake, run the verbose connection command:
ssh -v ubuntu2. Verifying the Active Route#
Once successfully logged into remote machine, you can verify which IP address your current session actually routed through by inspecting the SSH_CONNECTION environment variable:
echo $SSH_CONNECTION| Client IP | Client Port | Server Destination IP | Server Port |
|---|---|---|---|
<your_local_ip> | <port> | <active_target_ip> | 22 |
By checking the server destination IP in the output table, you can instantly confirm whether your SSH session successfully selected your wired interface, wireless interface, or Tailscale fallback.