DEV Community

Wardterm
Wardterm

Posted on Edited on

How to use Mosh on Mac and iPhone, and keep sessions alive with tmux

SSH drops the moment your laptop sleeps, your train goes into a tunnel or your iPhone switches from Wi-Fi to 5G. Mosh fixes the connection part. tmux fixes the "my job died with it" part. Together they make a remote shell you can close and reopen from anywhere.

This guide sets up both on a Linux server and shows how to connect from a Mac and from an iPhone.

The short answer

# on the server
sudo apt install mosh tmux          # Debian / Ubuntu
sudo ufw allow 60000:61000/udp      # only if ufw is on

# on the Mac
brew install mosh
mosh user@your-server -- tmux new -A -s main
Enter fullscreen mode Exit fullscreen mode

tmux new -A -s main attaches to the session called main if it exists, or creates it. Run the same command again tomorrow and you are back where you left off.

What Mosh does, and what it does not

Mosh (mobile shell) logs in over SSH, starts mosh-server on the remote machine, and then talks to it over UDP. Because the session is not tied to one TCP connection:

  • it survives a change of IP address (Wi-Fi to cellular, home to office);
  • it survives the laptop sleeping or the iPhone locking;
  • it shows what you type right away, even on a slow link, instead of waiting for the server's echo.

What it does not do: keep your programs running if the server side of the session ends, scroll back through history the way your terminal does, or forward ports. For the first two, you add tmux.

Install on the server

Debian and Ubuntu:

sudo apt update
sudo apt install mosh tmux
Enter fullscreen mode Exit fullscreen mode

Fedora, RHEL and friends:

sudo dnf install mosh tmux
Enter fullscreen mode Exit fullscreen mode

Mosh uses one UDP port per session in the range 60000 to 61000. If a firewall is on, open it:

sudo ufw allow 60000:61000/udp
Enter fullscreen mode Exit fullscreen mode

On a cloud server, add the same range as an inbound UDP rule in the provider's firewall too.

Mosh also needs a UTF-8 locale on the server. If you get an error about LANG or LC_ALL, this usually fixes it:

sudo locale-gen en_US.UTF-8
Enter fullscreen mode Exit fullscreen mode

Connect from a Mac

Install the client with Homebrew and connect the same way you would with SSH:

brew install mosh
mosh user@your-server
Enter fullscreen mode Exit fullscreen mode

If SSH runs on another port, pass the SSH command through:

mosh --ssh="ssh -p 2222" user@your-server
Enter fullscreen mode Exit fullscreen mode

Your ~/.ssh/config is used as usual, so a host alias works too: mosh myserver.

Add tmux so the work survives

Mosh keeps the connection alive. tmux keeps the programs alive, even if you close the Mosh session on purpose or the server reboots your login.

Start or reattach in one step:

mosh myserver -- tmux new -A -s main
Enter fullscreen mode Exit fullscreen mode

The keys you need on day one:

Keys What it does
Ctrl-b d detach: leave everything running and disconnect
Ctrl-b c new window
Ctrl-b n / Ctrl-b p next / previous window
Ctrl-b % split side by side
Ctrl-b [ scroll back (press q to leave)

Later, from any device:

tmux ls                  # list sessions
tmux attach -t main      # go back in
Enter fullscreen mode Exit fullscreen mode

This is the setup many people use to leave a long build or a database migration running on a server and check on it from the phone.

Connect from an iPhone or iPad

iOS has no built-in terminal, so you need an SSH app that supports Mosh. Look for three things:

  • Mosh support, not only SSH, so the session survives the phone locking;
  • key handling you trust, ideally keys that never leave the device;
  • your servers and keys on every device, so you don't set them up again on the phone.

The flow is the same as on the Mac: connect with Mosh, then tmux new -A -s main.

We make Wardterm, an SSH client for Mac, Windows, Linux, iPhone, iPad and Android. Mosh is included on every device, and your servers, keys and saved commands live in one end-to-end encrypted vault that syncs across them, so a server you add on the Mac is already on the phone. It's free on one device: https://wardterm.com/

Troubleshooting

"mosh-server: command not found"
Mosh is not installed on the server, or not on the PATH of a non-interactive SSH login. Install it, or point to it: mosh --server=/usr/local/bin/mosh-server user@host.

It hangs at "Connecting..." and then times out
UDP is blocked. Open 60000-61000/udp on the server firewall and on the cloud provider's firewall. Corporate and hotel networks often block UDP: SSH plus tmux still works there.

"The locale requested by LC_ALL=... isn't available"
Generate a UTF-8 locale on the server (sudo locale-gen en_US.UTF-8) or set LANG=en_US.UTF-8 on the client.

Scrolling with the trackpad does nothing
Mosh does not keep scrollback. Use tmux: Ctrl-b [, then arrow keys or Page Up.

Summary

  • Mosh keeps the connection alive across network changes and sleep.
  • tmux keeps your programs alive and lets you reattach from any device.
  • mosh myserver -- tmux new -A -s main is the one command to remember.

A longer comparison of Mosh clients for Mac and iPhone is on our blog: https://wardterm.com/blog/best-mosh-client/

Top comments (0)