LinuxCommandLibrary

mosh

Connect to remote servers with better responsiveness

TLDR

Connect to a remote server

$ mosh [username]@[remote_host]
copy

Connect to a remote server with a specific identity (private key)
$ mosh --ssh="ssh -i [path/to/key_file]" [username]@[remote_host]
copy

Connect to a remote server using a specific port
$ mosh --ssh="ssh -p [2222]" [username]@[remote_host]
copy

Run a command on a remote server
$ mosh [remote_host] -- [command -with -flags]
copy

Select Mosh UDP port (useful when remote_host is behind a NAT)
$ mosh -p [124] [username]@[remote_host]
copy

Usage when mosh-server binary is outside standard path
$ mosh --server=[path/to/mosh-server] [remote_host]
copy

SYNOPSIS

mosh [OPTIONS] [user@]host [command...]

mosh-client [OPTIONS]

mosh-server [OPTIONS]

PARAMETERS

-p [:]
    Specifies the UDP port number (or range) for the Mosh server to listen on.
Example: -p 60001 or -p 60000:60010.

--ssh=
    Specifies the SSH command to use for the initial connection.
Example: --ssh='ssh -p 2222'.

--ssh-port=
    Specifies the SSH port number to use for the initial connection, shorthand for part of --ssh.
Example: --ssh-port=2222.

--predict=
    Sets the local echo prediction strategy. Valid values include: always, auto (default), never. This controls how Mosh predicts server responses to keystrokes.

--no-confirm
    Do not ask for confirmation before killing a running Mosh server process on the remote host if one is found.

--no-init
    Do not send SIGHUP or other initialization signals to the remote command. Useful for scripts.

--bind-server={ssh|all|auto|}
    Controls which IP address the Mosh server binds to. ssh (default) uses the address SSH reports, all listens on all interfaces, auto attempts to find a good IP, or specify a direct IP address.

--server=
    Specifies the path to the mosh-server binary on the remote host.

--client=
    Specifies the path to the mosh-client binary on the local host.

--local
    Instructs Mosh to run mosh-server locally (on 127.0.0.1) instead of over SSH to a remote host. Useful for testing or when using a remote display.

--experimental-remote-ip=
    Attempts to connect to the Mosh server using a specific IP address, bypassing SSH's reported address. Use with caution.

DESCRIPTION

Mosh (Mobile Shell) is a remote terminal application that offers a significant improvement over SSH for interactive sessions, particularly in challenging network environments. Designed for mobile and intermittent connections, Mosh maintains the session even when the client's IP address changes or network connectivity is temporarily lost. It achieves this by using UDP for its primary data transfer, intelligently handling packet loss and out-of-order delivery.

A key feature of Mosh is its responsiveness, providing instant local echo of keystrokes. Instead of waiting for round-trip network delays, Mosh predicts what the server will do and displays the characters immediately, correcting them if the prediction is wrong. This makes typing feel much more fluid on high-latency connections.

Mosh leverages SSH for initial connection authentication and port forwarding, but then switches to its own protocol over UDP for the interactive session. This design allows Mosh to roam seamlessly between different IP addresses (e.g., switching from Wi-Fi to cellular data) without interrupting the session, making it ideal for users on the go or with unstable network access. It is an excellent choice for anyone who frequently works over unreliable internet connections or experiences high latency.

CAVEATS

Mosh requires UDP ports to be open on the remote server (default 60001-60060).
It does not natively support X11 forwarding directly over its UDP connection (it relies on SSH for this).
It does not replace `scp` or `sftp` for file transfers, which still require SSH.
Some advanced terminal features or sequences might not be fully compatible due to Mosh's state synchronization model.
Requires mosh-server to be installed on the remote host.

HOW MOSH WORKS

Mosh establishes an initial connection using SSH for authentication and to spawn mosh-server on the remote host. After this, the interactive session switches to Mosh's own protocol over UDP. Mosh maintains a synchronized state between the client and server, sending only the differences in the screen state. This state synchronization, combined with UDP, allows it to handle packet loss and network changes gracefully without session interruption.

LOCAL ECHO AND PREDICTION

One of Mosh's most praised features is its predictive local echo. When you type, Mosh immediately displays the characters on your local terminal, without waiting for the server's acknowledgment. It uses a predictive algorithm to anticipate the server's response. If the prediction is incorrect (e.g., an error message or a command not found), Mosh quickly corrects the display, making typing feel much more responsive, especially over high-latency connections. This significantly reduces the perceived lag.

PORT REQUIREMENTS

Mosh typically requires UDP ports 60001-60060 to be open on the remote server's firewall for the mosh-server to listen on. If these ports are blocked, Mosh will try to find an open port within this range. The specific port can also be explicitly specified using the -p option. SSH's default port 22 is still used for the initial setup.

HISTORY

Mosh was developed by Jonathon Levin and others at MIT as part of the Culler research group. Its development began around 2011, driven by the increasing need for a robust and responsive shell experience on mobile devices and over high-latency or intermittently connected networks. Unlike traditional SSH which maintains a TCP connection, Mosh was designed from the ground up to use UDP, enabling its key features: seamless roaming across IP address changes, resilience to network outages, and predictive local echo. It aimed to address the fundamental limitations of TCP-based connections in the context of interactive remote shells, quickly gaining popularity among developers and system administrators working in challenging network conditions.

SEE ALSO

ssh(1), tmux(1), screen(1), sshd(8)

Copied to clipboard