Mosh from MacOS with scroll

Mosh is an incredibly useful shell replacement.  However, it suffers from a pretty big drawback: it doesn’t allow you to scroll back in the terminal buffer.  This makes it pretty useless when it comes to administering servers, since you can’t run commands like ​​tail -f and expect to achieve anything productive.

Here is a relatively simple way to get back scroll, while still using mosh.

Requirements

  • MacOS and iTerm2
  • Ubuntu on the server side

Install Mosh

First, install on the server:

$ sudo apt install mosh

Then, open up the required ports:

$ sudo ufw allow 60000-61000/udp
$ systemctl restart ufw

Next, install it on your Mac as well:

$ brew install --HEAD mobile-shell

This will let you connect to a server using mosh [email protected] instead of ssh [email protected]myserver.com.

The scroll bit.

On your Mac, make a shell alias called mosht (as opposed to the original mosh):

$ alias mosht="mosh $argv -- tmux a"

In fish that could look something like the following in ~/.config/fish/config.fish:

function mosht --wraps mosh --description 'alias mosht=mosh $argv -- tmux a'
  mosh $argv -- tmux a
end

Since, as far as I can tell, you can’t interpolate arguments using the fish alias extension.

Now, on your server again, make sure that tmux is installed:

$ sudo apt install tmux

Make the following file ~/.tmux.conf:

new-session
set-window-option -g mouse on
set -g history-limit 30000
set -g status off

That’s it, now make sure you are using iTerm2 instead of the regular terminal, and

  • Go to Preferences… -> Profiles -> Default -> Terminal, and
  • Make sure that Report Terminal Type is set to xterm-256color

Close and re-open your terminal window, and then you can connect to your server again using:

mosht [email protected]

Happy scrolling!

Beware: tmux means that you can only do this on a single session. If you open up another session using mosht then both will reflect exactly the same console. Fun, but not useful. Especially when you accidentally do this for the third time, and realize, for the third time as well, that closing one using exit closes them all.

Leave a Reply