Setup Mosh

Posted by Ryan Himmelwright on Wed, Dec 4, 2019
Tags linux, homelab, ssh, shell
North Buchanan Boulevard, Durham, NC

Since builing my desktop, whenever I work on another machine, I usually end up ssh’ing back to it to work remotely. It has my files, more power, and much of my work flow is done from a terminal window anyway, so why not? The only issue I have with ssh is that if I have a spotty internet connection, or if I sleep/suspend my laptop while moving around, the ssh session will occasionally time out. Tmux and tmuxinator make this less of an issue, since I can re-attach my session, but I still wish my remote sessions were a bit more seamless. They can be… using mosh.

Mosh

Using mosh to conenct to a server
Using `mosh` to connect to one of my servers

Mosh is a more robust replacement for interactive ssh terminals. It automatically roams and continues to work even as the computer switches networks or is put to sleep. It also responds to typing, even on a bad connection, which cuts down on lag. Lastly, it’s free and open source software, licenced under the GPLv3.

Mosh Install

Mosh should be in most Linux repos, and is also available on BSD, Mac, Windows, and basically everything else. For more information on how to install it on your platform, head over to the getting mosh page. For me, it was a simple dnf/yum install to get it on both my laptop and server:

sudo dnf install mosh

*Note: My centos server required me to first enable the epel repos to get access to mosh. Fedora might also, but I already had it enabled on my laptop.

sudo yum install -y epel-release
sudo yum install -y mosh

Open Firewall Ports

After installing mosh… it might not immediately work. If so, it is likely due to not having the required ports open. Mosh uses UDP ports 60000-61000 for it’s connections. Enable these ports and optionally restart the firewall, before trying mosh again.

Note: If you want to connect from outside the network, remember to also forward these ports on the network.

Connect

We should now be able to connect to the server using mosh. Typical connections look very similar to ssh:

mosh ryan@centos-server

If a specific mosh UDP port needs to be specified (for port-forwarding, for example) use the -p flag:

mosh -p 1234 ryan@centos-server

SSH Options

Mosh uses ssh for the initial connection. Occasionally, particular ssh options might be required in order for mosh to initialize a connection. For example, I typically ssh home on a particular port, so that my router knows which VM to transfer me to. Options like this can be passed to mosh using the --ssh flag:

mosh --ssh="ssh -p 1234" ryan@centos-network

Conclusion

That’s really it. There is a bunch of cool technical stuff going on under the hood of mosh, but on the surface… it is simply useful. For an even better experience with mosh, be sure to check out tmux. Enjoy!

Next Post:
Prev Post:

Resolving Issue #26 All Things Open 2019