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/bin/]mosh-server [remote_host]
copy

SYNOPSIS

mosh [options] [--] [user@]host [command...]

PARAMETERS

--help
    Display a help message and exit.

--version
    Display version information and exit.

--server=
    Path to the mosh-server executable on the remote host.

--ssh=
    Path to the ssh executable.

--predict=
    Prediction mode. 'always' predicts all input, 'adaptive' predicts only when latency is low, 'never' disables prediction.

--no-ssh-pty
    Do not allocate a pseudo-terminal via SSH. Useful for executing commands directly without a shell.

[user@]host
    The remote host to connect to. You can specify a username if it's different from your local username.

[command...]
    Optional command to execute on the remote host. If not specified, a login shell will be started.

DESCRIPTION

Mosh (Mobile Shell) is a remote terminal application designed as a replacement for SSH. It provides a more robust and responsive terminal experience, especially over intermittent or high-latency network connections.

Unlike SSH, Mosh predicts the user's actions and displays the results immediately. This significantly improves perceived responsiveness, as the terminal doesn't have to wait for server confirmation for every keystroke. Mosh also supports roaming IP addresses, allowing connections to persist even when the user's IP address changes. It achieves this by operating over UDP, rather than TCP like SSH, and maintaining session state on the client. While it's primary use is for remote command line sessions, the reduced latency makes it superior in comparison to ssh for some GUI based interactions like using terminal based text editors(vim/nano).

Mosh requires SSH for initial authentication and setup. Once authenticated, Mosh establishes a UDP connection for data transfer.

CAVEATS

Mosh requires UDP ports 60000-61000 to be open on both the client and server sides. Firewall configuration is crucial. It also relies on SSH for initial authentication, so SSH must be configured and working.

SECURITY CONSIDERATIONS

While Mosh uses SSH for initial authentication, the UDP connection is secured with encryption.
However, it's important to keep both Mosh and the underlying SSH implementation up to date with the latest security patches.

Consider restricting access to the Mosh server ports (60000-61000) to only trusted networks or hosts using firewall rules.

TROUBLESHOOTING

If you encounter issues, check the following:
- Firewall configuration on both client and server.
- Mosh server is installed and running on the remote host.
- SSH connectivity to the remote host is working correctly.
- The 'TERM' environment variable is correctly set. Sometimes setting 'TERM=xterm-256color' helps.

HISTORY

Mosh was developed as an alternative to SSH to address the limitations of TCP-based connections in mobile and unreliable network environments. It was designed to provide a more persistent and responsive terminal experience. Development started in 2012, targeting problems like the disconnections from terminal editors like vim that were common while using ssh on trains or airplanes.

SEE ALSO

ssh(1), tmux(1), screen(1)

Copied to clipboard