How to Configure Mosh Port Forwarding

Published
Length
4 min read

Luis Felipe Dias

All posts

Native mosh port forwarding does not exist because the tool uses UDP for screen state. To forward ports, you must run a separate SSH tunnel alongside your Mosh session. Mosh helps engineers keep shell sessions alive across spotty networks and roaming laptops. It uses SSH first to log in and start a server process on the host. Then it switches to UDP to sync only the screen state. Because UDP drops the standard SSH stream, built-in port tunnels cannot ride over Mosh. How mosh port forwarding w

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

Native mosh port forwarding does not exist because the tool uses UDP for screen state. To forward ports, you must run a separate SSH tunnel alongside your Mosh session.

Mosh helps engineers keep shell sessions alive across spotty networks and roaming laptops. It uses SSH first to log in and start a server process on the host. Then it switches to UDP to sync only the screen state. Because UDP drops the standard SSH stream, built-in port tunnels cannot ride over Mosh.

How mosh port forwarding works in practice

When engineers ask for mosh port forwarding, they want two separate things at once. They want a shell that survives sleep mode and Wi-Fi changes. They also want to reach a remote web app or database on a local port.

Mosh cannot carry TCP streams inside its UDP protocol. The standard pattern is to split the work between two tools. You use Mosh for your typing and terminal screen updates. You run a background SSH process at the same time to carry the port tunnel.

This setup affects engineers who work from mobile networks, trains, or coffee shops. Their interactive shell stays fast through Mosh. Meanwhile, a helper tool or SSH loop keeps the TCP tunnel open in the background. If the laptop sleeps, Mosh recovers the shell right away. The helper tool rebuilds the TCP tunnel when the network returns.

For safe defaults, bind all local forwarded ports to the loopback address only. Never expose a forwarded port to all network interfaces on a user laptop. Anyone on the same public Wi-Fi could reach that open local port. Also, restrict which remote hosts and ports a user role can tunnel through SSH.

How to set up parallel SSH tunnels

Start by setting up your SSH client config for the target host. Define the local port, the target host address, and the remote port there. Keep this tunnel profile separate from your normal interactive login profile. That way, every new terminal window does not try to grab the same local port.

Next, start your background SSH tunnel with no remote command execution. You only need this connection to move TCP packets from your laptop to the host. Configure your client to send keepalive packets at a short interval. Keepalives help the client detect a dead network and close the stale tunnel fast.

SSH Agent Forwarding Security + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Many engineers pair this background tunnel with an automatic reconnect tool. When you switch networks, the old TCP tunnel breaks. The reconnect tool sees the drop and starts a fresh SSH tunnel right away. At the same time, you run Mosh in your main terminal window for command work.

Checking logs and limits of static setups

You should check both the UDP shell session and the TCP tunnel on the host. Check your host firewall rules first. Mosh needs high UDP ports open on the remote server to work. If the firewall blocks UDP, Mosh fails after the initial SSH login step.

Auditing this split setup brings several blind spots for security teams. Standard SSH logs show when a user logs in and starts the Mosh server. After that, the SSH connection closes and the UDP session takes over. System logs will not show what commands the user types inside that UDP stream.

Static SSH keys also leave wide gaps once a session starts. If a user can open a TCP tunnel, the host sees raw socket traffic. Traditional host logs do not record what queries or requests pass through that forwarded port. Data masking is impossible with plain port tunnels, so raw emails and card numbers pass straight through. Plus, an orphaned Mosh server process can stay running on the host after a user leaves.

Teams should review active server processes on a regular schedule. Check for old Mosh processes and open local listening ports on user machines. Limit port forwarding rights in the server config to only the roles that need them.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Why does Mosh ignore my SSH port forwarding settings?

Mosh only uses SSH to log in and launch its remote server process. Once the server starts, Mosh closes the SSH connection and switches to UDP. Any port tunnels tied to that initial SSH session close right away.

Can I forward ports over UDP with Mosh?

No, the Mosh protocol only syncs terminal screen states over UDP. It does not include a way to multiplex TCP streams or forward arbitrary network ports. You must use a separate TCP tool like SSH to forward ports.

Is it safe to run Mosh and SSH tunnels at the same time?

Yes, both tools use strong encryption to protect traffic in transit. Still, you must bind local ports to localhost only and track lingering server processes on the host.

The query runs. The emails stay hidden.