mosh
Mosh: The Mobile Shell
Mosh (mobile shell) is a remote terminal application that allows roaming, supports intermittent connectivity, and provides intelligent local echo. Unlike SSH, Mosh keeps your session alive even when your IP changes, you switch networks, or your connection drops for a moment.
Why Mosh?
- Roaming: Move between Wi-Fi, cellular, and wired networks without losing your session.
- Intermittent connectivity: If your connection drops, Mosh patiently waits and resumes automatically when the network returns.
- Local echo: Mosh predicts your typing and displays it instantly, even on high-latency links, making it feel snappy.
- No root required on the client: Only the server side needs
mosh-serverinstalled.
Installation
On the Server (Debian/Ubuntu)
1 | sudo apt update |
On the Client
macOS:
1 | brew install mosh |
Debian/Ubuntu:
1 | sudo apt install mosh |
Arch Linux:
1 | sudo pacman -S mosh |
Using the Mosh Client
The simplest usage is very similar to SSH:
1 | mosh user@hostname |
You can also pass SSH options or specify a command:
1 | mosh user@hostname --ssh="ssh -p 2222" |
Mosh uses UDP ports in the range 60000–61000 by default. If your server is behind a firewall, make sure to open the required range:
1 | sudo ufw allow 60000:61000/udp |
Or for iptables:
1 | sudo iptables -A INPUT -p udp --dport 60000:61000 -j ACCEPT |
Booting mosh-server with systemd in Debian
By default, mosh starts mosh-server on demand via SSH. However, if you want mosh-server to be available immediately after boot (for example, on a headless server or to reduce connection latency), you can create a systemd user service.
Step 1: Create the systemd user service file
Create the directory and file:
1 | mkdir -p ~/.config/systemd/user/ |
Edit ~/.config/systemd/user/mosh-server.service:
1 | [Unit] |
Note: The
StandardInput=socketline allows systemd to allocate a UDP socket formosh-serverand pass it in. This is optional; without it,mosh-serverwill bind to a dynamic port as usual.
Step 2: Enable and start the service
1 | systemctl --user daemon-reload |
Check the status:
1 | systemctl --user status mosh-server.service |
Step 3: Ensure user services persist after logout (Debian default)
Debian enables lingering for user services by default via systemd-logind, but if your session is terminated when you log out of SSH, enable lingering explicitly:
1 | sudo loginctl enable-linger $USER |
Step 4: Connect from the client
If you started mosh-server manually via systemd, it prints a connection command. Read the journal:
1 | journalctl --user -u mosh-server.service |
Look for a line like:
1 | MOSH CONNECT 60001 XXXXXXXXXXXXXXXX |
Then connect from the client:
1 | mosh-client 192.168.1.100 60001 |
Or simply use the usual mosh user@host and let SSH spawn a new server instance if you prefer the on-demand model.
Tips
- Restrict port range: If you want to limit the UDP port range, set the environment variable
MOSH_SERVER_CMDor use--server:1
mosh user@host --server="mosh-server new -p 60000:60010"
- Tmux/screen integration: Mosh works great with
tmuxorscreen. Start a persistent session and reattach anytime:1
mosh user@host -- tmux new -A -s remote
- IPv6: Mosh fully supports IPv6. Just connect to an IPv6 address or hostname with AAAA records.
Conclusion
Mosh is an excellent complement (or replacement) to SSH for mobile or unreliable network conditions. Combined with a systemd user service on Debian, you can have a fast-resuming remote shell ready at boot time. Give it a try and enjoy truly mobile terminal sessions!
References:
- Mosh Official Website
- Mosh GitHub Repository
man mosh,man mosh-server,man mosh-client