Side by side
| SSH | Mosh | |
|---|---|---|
| Transport | TCP, usually port 22 | SSH to log in, then UDP |
| Network change or client IP change | Session breaks | Session continues |
| Typing on a slow link | Waits for the server | Shows typing locally, while checking with the server |
| Needs on the server | sshd | sshd and mosh-server. Root is not required |
| Scrollback | Handled by your terminal | Only the visible screen is kept in sync |
| Port, agent and X11 forwarding | Yes | No |
| File copy, scripts | scp, sftp, ssh host command | Not for this. It is for interactive terminals |
What it does in practice
We opened a Mosh session, removed the client container from its network for 6 seconds, and put it back. Mosh showed a bar on top with Last reply 10 seconds ago. [To quit: Ctrl-^ .]. Then the session continued in the same shell, and a command typed afterward ran normally.
A plain SSH session ends in this kind of situation if the network stays away long enough, or if your IP address changes. See Broken pipe and keepalive.
Install
Mosh has to be installed on both your machine and the server:
sudo apt-get install mosh # Debian, Ubuntu
brew install mosh # macOS with Homebrew
Open the UDP ports
mosh-server takes the first free UDP port starting at 60001. In our test it was 60001. Open the range on the server's firewall, for example with ufw:
sudo ufw allow 60000:61000/udp
If you run only a few sessions, a smaller range is enough. If you forward TCP 22 on a router to reach the server, forward these UDP ports too. Mosh's site names a firewall that blocks UDP as a cause when nothing is received from the server.
Connect
mosh user@server
mosh --ssh="ssh -p 2222" user@server
mosh -p 60001 user@server
Use --ssh for a different SSH port and -p to choose the UDP port. -p does not change the SSH port. To quit a session that no longer responds, press Ctrl+^ and then ..
Limits to expect
- No scrollback. Mosh only syncs the visible screen. Mosh's own advice is to run tmux or screen on the server for history. See the tmux cheat sheet. To return to the same shell after the client closes, see keep SSH sessions alive with tmux.
- No forwarding. If you need a tunnel at the same time, open it with a separate
sshcommand. - UTF-8 only. With a non UTF-8 locale,
mosh-serverstops withmosh-server needs a UTF-8 native locale to run.. Checklocaleon both sides. - UDP has to get through. Networks that only allow TCP will not work.
- Not protection against a reboot.
mosh-serveris an ordinary process on the server, so a reboot ends it.
Security for the session comes from a key that SSH exchanges at the start, after which traffic is encrypted and authenticated with AES-128 in OCB3 mode, according to the Mosh site.
Questions
Is Mosh faster than SSH?
Not in throughput. Mosh feels faster on slow links because it shows your typing at once. For copying files or tunnels, SSH is the tool.
Which ports does Mosh use?
It logs in over SSH, usually on TCP port 22, then uses a UDP port between 60000 and 61000 on the server, starting at 60001.
Does Mosh replace SSH?
No. Mosh uses SSH to log in and start mosh-server. Keep SSH for file copies, port forwarding and scripts.
What does mosh did not make a successful connection mean?
UDP from your machine does not reach mosh-server. The full message ends with Please verify that UDP port 60001 is not firewalled and can reach the server. Open 60000:61000/udp on the server and in your provider's firewall, and check that the network you are on lets UDP through.