{"id":1360,"date":"2018-02-07T08:29:26","date_gmt":"2018-02-07T08:29:26","guid":{"rendered":"https:\/\/blog.ssdnodes.com\/blog\/?p=1360"},"modified":"2025-07-16T15:06:20","modified_gmt":"2025-07-16T15:06:20","slug":"tutorial-ditch-ssh-get-started-mosh","status":"publish","type":"post","link":"https:\/\/www.ssdnodes.com\/blog\/tutorial-ditch-ssh-get-started-mosh\/","title":{"rendered":"Tutorial: Ditch SSH, get started with Mosh"},"content":{"rendered":"<div class=\"preview__inner-2\">\n<div id=\"preview1\" class=\"g-b g-b--t1of2 split split-preview\">\n<div id=\"preview\" class=\"preview-html\">\n<p>Secure Shell (SSH) is a fundamental component to virtual private servers (VPSs) and, generally speaking, Linux administration. The ability to connect two machines over a secure channel is invaluable, particularly <a href=\"https:\/\/www.ssdnodes.com\/blog\/tutorial-setting-up-and-securing-ssh-based-authentication\/\">when appropriately done with public keys<\/a>, and many talented developers have bootstrapped SSH to build excellent tools like rsync.<\/p>\n<p>But SSH isn\u2019t perfect. And there\u2019s room for other tools with slightly different goals, or tools that aim to push SSH\u2019s key benefits even further. Enter <a href=\"https:\/\/mosh.org\/\" target=\"_blank\" rel=\"noopener\">Mosh<\/a>, the \u201cmobile shell,\u201d which, according to its developers, \u201callows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes.\u201d<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1361 size-full\" src=\"https:\/\/www.ssdnodes.com\/wp-content\/uploads\/2018\/02\/201802-mosh.png\" alt=\"mosh\" width=\"477\" height=\"427\" srcset=\"https:\/\/www.ssdnodes.com\/wp-content\/uploads\/2018\/02\/201802-mosh.png 477w, https:\/\/www.ssdnodes.com\/wp-content\/uploads\/2018\/02\/201802-mosh-300x269.png 300w\" sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><\/strong><\/p>\n<p>Mosh is meant to be a complete replacement for SSH, and focuses on providing a more robust connection, particularly over spotty internet connections. If you want to get started with the tutorial part of this post right away, hop down to <a href=\"#Installing_Mosh_Step_1_Installing_moshserver_34\">the first step<\/a>.<\/p>\n<\/div>\n<div class=\"cta-inline\"><\/div>\n<div id=\"preview\" class=\"preview-html\">\n<h2>What\u2019s wrong with SSH, again?<\/h2>\n<p><strong>It\u2019s not graceful on spotty connections<\/strong>. Well, sometimes SSH is terrible even when you\u2019re using extremely reliable internet. That\u2019s because when the connection between client and server falters, even momentarily, it can stall entirely, forcing you to kill it and reconnect. This process is annoying once, but after dozens of repetitions, it\u2019s a real annoyance and leech on your time.<\/p>\n<p><strong>SSH has a few security vulnerabilities<\/strong>. No piece of software is infallible, and SSH is no different\u2014over the last two decades, security experts have discovered <a href=\"https:\/\/en.wikipedia.org\/wiki\/Secure_Shell#Vulnerabilities\" target=\"_blank\" rel=\"noopener\">a few vulnerabilities<\/a> in the SSH protocol, including a little something the NSA worked up. Despite these, SSH remains the most popular and respected method of connecting<\/p>\n<p><strong>Network latency leads to lackluster typing performance<\/strong>. When you type into a terminal running an SSH connection, the client sends data about each keystroke to the remote server, which responds in kind. Only once the data has traveled to the server and back will your local terminal show the keys you typed. The inherent latency in network connections creates a noticeable delay between you pressing a key and seeing it on your screen. There\u2019s nothing <em>wrong<\/em> with this delay, but it doesn\u2019t make for a stellar user experience.<\/p>\n<h2>How does Mosh do those things better?<\/h2>\n<p><strong>Using states for more reliability<\/strong>. Mosh uses what it calls the State Synchronization Protocol (SSP), which is responsible for synchronizing two snapshots of the current screen state, one on the server, and another on the client. This UDP-based protocol is aimed at showing the client the most recent server-side state, and is designed to use <a href=\"https:\/\/en.wikipedia.org\/wiki\/Datagram\" target=\"_blank\" rel=\"noopener\">datagrams<\/a>\/\u201cheartbeats\u201d to maintain the connection, even through roaming between Wifi hotspots or between different IP addresses.<\/p>\n<p><strong>A more \u2018conservative design\u2019 on security<\/strong>. The developers of Mosh are quick to admit that vulnerabilities both added to inadvertently and discovered within OpenSSH\/OpenSSL are primarily due to the decades-long lifespan of each, and just how popular they are. The subtext is that Mosh might have better security solely <a href=\"https:\/\/en.wikipedia.org\/wiki\/Security_through_obscurity\" target=\"_blank\" rel=\"noopener\">through obscurity<\/a>.<\/p>\n<p>Still, Mosh\u2019s SSP authenticates every \u201cdatagram,\u201d which prevents some of the potential vulnerabilities in SSH. As of July 2017, no security vulnerabilities have ever been<\/p>\n<p><strong>Significant improvements in latency for better real-world productivity.<\/strong> Because SSP synchronizes two states instead of waiting for data to transfer back and forth, Mosh can make changes to the user interface much faster. According to a study they completed, the median keystroke response time for SSH was 503ms, and only <strong>5ms<\/strong> for Mosh.<\/p>\n<p><strong>Is Mosh perfect?<\/strong> Certainly not. Common criticisms are having to open up a handful of UDP ports, and that it still relies on SSH to create the initial connection. Of course, <code>screen<\/code> and <code>tmux<\/code> also take care of the reliability\/persistence issue <em>without<\/em> opening new ports.<\/p>\n<p>Still, all this adds up to a compelling picture: Mosh might not be perfect for everyone, but it\u2019s definitely worth a test. Let\u2019s get to the installation.<\/p>\n<h2>Installing Mosh, Step 1: Installing mosh-server<\/h2>\n<p>As with SSH, Mosh uses one version of its application on the remote server and another on the local client. Installing on our OS options (Ubuntu, Debian, and CentOS) is a cinch\u2014just use the package manager to install <code>mosh<\/code>.<\/p>\n<pre><code>$ sudo apt-get install mosh    # Ubuntu\/Debian\n$ sudo yum install mosh        # CentOS\n<\/code><\/pre>\n<p>You don\u2019t need to configure anything\u2014you\u2019re good to go on the server front.<\/p>\n<h2>Step 2: Installing mosh-client<\/h2>\n<p>Client-side installation depends entirely on the operating system of the machine you\u2019re reading this on right now. The Mosh team has built clients for OS X, Windows (via Cygwin), plenty of Linux distributions, Chrome, *BSD, and more.<\/p>\n<p>To find them all, visit the <a href=\"https:\/\/mosh.org\/#getting\" target=\"_blank\" rel=\"noopener\"><strong>Getting Mosh<\/strong><\/a> section on the Mosh homepage.<\/p>\n<h2>Step 3 (optional): Configure your server\u2019s firewall<\/h2>\n<p>If your server is configured with a firewall like <code>iptables<\/code>, you will need to open specific ports to allow SSP to synchronize between sever and client. With <code>iptables<\/code>, you can issue the following command to open these UDP ports.<\/p>\n<pre><code>$ sudo iptables -I INPUT 1 -p udp --dport 60000:61000 -j ACCEPT\n<\/code><\/pre>\n<p>You don\u2019t <em>have<\/em> to open all 1,000 ports\u2014Mosh only needs 1 open port per connection. If you only need a few concurrent connections, you can open far fewer ports, like <code>60000:60020<\/code>.<\/p>\n<p>Keep in mind that this <code>iptables<\/code> rule is <strong>not persistent<\/strong>, in that if the server reboots, the rule will disappear. You need to save this rule. There are multiple ways to do this, including <code>iptables-save<\/code> and <code>iptables-persistent<\/code>. Here\u2019s how to do so using <code>iptables-save<\/code>, which you\u2019ll need to execute after escalating your privileges using <code>sudo -i<\/code>.<\/p>\n<pre><code># iptables-save &gt; \/etc\/iptables\/rules.v4            # Ubuntu\/Debian\n# iptables-save &gt; \/etc\/sysconfig\/iptables           # CentOS\n<\/code><\/pre>\n<h2>Step 4: Your first connection<\/h2>\n<p>Once you have Mosh installed on both the server and client, you can try out your first connection. The syntax is mostly the same as with SSH:<\/p>\n<pre><code>$ mosh USER@YOUR-IP-ADDRESS\n<\/code><\/pre>\n<p>So, for example, if you type in <code>ssh person@somedomain.xyz<\/code> to connect to your VPS via SSH right now, you can type in <code>mosh person@somedomain.xyz<\/code> instead, or use the IP address you\u2019ll find in the <a href=\"https:\/\/www.ssdnodes.com\/manage\/\">SSD Nodes dashboard<\/a>.<\/p>\n<p>You\u2019ll be asked to authenticate using SSH, but as soon as that completes, you\u2019ll be transferred over to the Mosh connection.<\/p>\n<p>If you have a less common case, such as an SSH server running on a port other than the standard 22, or you want to specify a particular Mosh UDP port, be sure to check out the <a href=\"https:\/\/mosh.org\/#usage\" target=\"_blank\" rel=\"noopener\">usage area<\/a> on Mosh\u2019s homepage.<\/p>\n<p>Ending the connection is just as easy as getting it started. As with SSH, you can type <code>logout<\/code> or <code>exit<\/code> to close the session. If you need or want to force close the connection, Mosh uses the <code>Ctrl-^<\/code> escape sequence. On most keyboards, that means typing <code>Ctrl-Shift-6<\/code>, followed by a period.<\/p>\n<h2>Stay in touch!<\/h2>\n<p>When I first started using Mosh, I was amazed at how suddenly fast and responsive my remote connections felt. There\u2019s always a place for SSH when it comes to one-off connections to remote servers, but for those I know I\u2019m going to spend lots of time on, Mosh seems like a keeper. Once I start combining it with <a href=\"https:\/\/www.ssdnodes.com\/blog\/tutorial-terminals-tmux\/\"><code>tmux<\/code><\/a> for more flexibility with panes, I feel like I\u2019ll finally be on the right track toward some real productivity on the remote command line.<\/p>\n<p>If you have tips on using Mosh more productively, or just want to chat terminals, you can find me at <a href=\"mailto:joel@ssdnodes.com\">joel@ssdnodes.com<\/a>. Happy Moshing!<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>SSH is super powerful, but it&#8217;s definitely not perfect. Why not give Mosh, which promises better reliability and speed, a try?<\/p>\n","protected":false},"author":20,"featured_media":1370,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[18,30],"tags":[],"class_list":["post-1360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-tutorials"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/1360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/comments?post=1360"}],"version-history":[{"count":5,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/1360\/revisions"}],"predecessor-version":[{"id":13509,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/posts\/1360\/revisions\/13509"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/media\/1370"}],"wp:attachment":[{"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/media?parent=1360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/categories?post=1360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ssdnodes.com\/wp-json\/wp\/v2\/tags?post=1360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}