TechBuzzPro

SSH Over Bad Wi-Fi: The CLI Tool That Fixes It

· 4 min read · Wi-Fi & Networking

In this article
  1. Why SSH Feels Awful on Bad Wi-Fi
  2. What Mosh Does Differently
  3. How to Install and Use Mosh
  4. When Mosh Shines and When It Doesn't
  5. Common Problems and Troubleshooting
  6. Alternatives to Mosh
  7. Frequently Asked Questions

Yes, a command-line tool called Mosh (Mobile Shell) fixes the misery of SSH over bad Wi-Fi. It keeps your session alive across network changes and latency spikes, giving you a responsive terminal even on a flaky connection.

Why SSH Feels Awful on Bad Wi-Fi

SSH was designed for reliable, low-latency networks. It sends each keystroke as a separate packet and waits for the server to echo it back before displaying it. On a high-latency or lossy connection, this creates a noticeable delay between typing and seeing characters appear. Worse, if your Wi-Fi drops for even a few seconds, SSH often freezes and eventually disconnects, forcing you to reconnect and lose your terminal state.

The core problem is that SSH is synchronous: it blocks on every round trip. Mosh solves this by using a different protocol that predicts what the server will send and renders it locally, so you see your input instantly, even with 500ms of latency.

What Mosh Does Differently

Mosh is a remote terminal application that replaces SSH for interactive sessions. It consists of two parts: a client on your local machine and a server on the remote host. The client and server communicate over UDP, not TCP, which means it doesn't suffer from TCP's head-of-line blocking. Instead of sending raw keystrokes, Mosh sends the state of your terminal screen, and the client predicts the next state locally.

Key features:

  • Instant local echo: Keystrokes appear immediately, even with high latency.
  • Connection roaming: Your session survives IP address changes, so you can switch from Wi-Fi to cellular without dropping.
  • No disconnects on sleep: If your laptop sleeps or the network drops, Mosh just waits and resumes when you're back.
  • Scrollback support: You can scroll through the terminal history, unlike a plain SSH session.

Mosh does not replace SSH for file transfers or running scripts; it's strictly for interactive use.

How to Install and Use Mosh

Installation is straightforward on most systems. On macOS, use Homebrew: brew install mosh. On Ubuntu/Debian, use sudo apt install mosh. On Windows, you can use WSL or the MSYS2 environment. The remote server also needs Mosh installed, and you must have SSH access to it.

To connect, simply run:

mosh user@hostname

Mosh will first establish an SSH connection to launch its server, then switch to the UDP protocol. You can pass SSH options like port or key file using the --ssh flag:

mosh --ssh="ssh -p 2222" user@hostname

For the best experience, ensure UDP ports 60000-61000 are open on your firewall, as Mosh uses a range of high ports.

When Mosh Shines and When It Doesn't

Mosh is ideal for:

  • Working over hotel or coffee shop Wi-Fi.
  • Roaming between networks (e.g., from Wi-Fi to LTE).
  • Using a laptop that sleeps frequently.
  • Editing code in a remote terminal with high latency.

However, Mosh has limitations:

  • It does not support port forwarding (tunneling) like SSH.
  • It cannot run non-interactive commands or scripts; you need SSH for that.
  • It requires UDP ports to be open, which some restrictive firewalls block.
  • It does not support X11 forwarding for graphical apps.

For those use cases, stick with SSH, but for interactive terminal work, Mosh is a game-changer.

Common Problems and Troubleshooting

If Mosh fails to connect, check these:

  1. UDP ports blocked: Ensure UDP 60000-61000 are open on the server's firewall.
  2. SSH connection fails: Mosh uses SSH to bootstrap, so verify SSH works first.
  3. Mosh not installed on server: You'll get an error like "mosh-server not found." Install it.
  4. NAT or VPN issues: Some VPNs interfere with UDP; try disconnecting or using a different network.

If you see "mosh: Nothing received from server," it usually means the UDP handshake failed. Check your firewall rules and that the server's mosh-server is running.

Alternatives to Mosh

While Mosh is the most popular tool for this, there are alternatives:

  • SSH with TCP keep-alives: You can configure SSH to send keep-alive packets, but it doesn't fix latency.
  • Screen or tmux: These let you detach and reattach sessions, but they don't improve responsiveness.
  • Eternal Terminal (ET): A newer tool that also uses UDP and supports scrollback, but it's less mature.
  • SSH over a VPN: A VPN can stabilize the connection, but adds overhead and doesn't solve latency.

For most people, Mosh is the best balance of features and stability.

Frequently Asked Questions

Does Mosh work on Windows?

Yes, you can use Mosh on Windows via WSL (Windows Subsystem for Linux) or through MSYS2. The experience is slightly less seamless than on macOS/Linux, but it works well.

Is Mosh secure?

Mosh encrypts its traffic with AES-128 in OCB mode, and it uses SSH for initial authentication. It does not store any data on the client or server after the session ends. It's considered secure for most use cases, though some organizations may prefer SSH for compliance reasons.

Can I use Mosh for file transfers?

No, Mosh is only for interactive terminal sessions. For file transfers, use SCP, SFTP, or rsync over SSH.

Does Mosh work over a VPN?

Yes, Mosh works over a VPN, but if the VPN blocks UDP or adds significant latency, you may not see the full benefit. In some cases, the VPN's own TCP congestion control can interfere, but generally it works.

Why doesn't Mosh support port forwarding?

Mosh's protocol is designed for terminal I/O, not arbitrary packet forwarding. The developers chose to keep it simple and focused. If you need port forwarding, use SSH in parallel with Mosh.