Mosh, also known as “Mobile Shell,” is a command-line tool used for connecting remote Linux servers. Mosh is optimized for mobile working. It allows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes. Mosh is an alternative to SSH and provides more features than Secure Shell. It is available for all major operating systems including Linux, FreeBSD, Solaris, Mac OS X, and Android.

In this post, we will show you how to install and use Mosh command-line tool on Linux.

Install Mosh

For Debian based Linux distributions, you can install the Mosh using the following command:

apt-get install mosh

For CentOS-based Linux distributions, you can install the Mosh using the following command:

yum install mosh

Once the Mosh is installed, you can verify the installed version of Mosh with the following command:

mosh --version

You should get the following output:

mosh 1.3.2 [build mosh 1.3.2]
Copyright 2012 Keith Winstein <[email protected]>
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

You can also see all options available with Mosh using the following command:

mosh --help

You should get the following output:

Usage: /usr/bin/mosh [options] [--] [user@]host [command...]
        --client=PATH        mosh client on local machine
                                (default: "mosh-client")
        --server=COMMAND     mosh server on remote machine
                                (default: "mosh-server")

        --predict=adaptive      local echo for slower links [default]
-a      --predict=always        use local echo even on fast links
-n      --predict=never         never use local echo
        --predict=experimental  aggressively echo even when incorrect

-4      --family=inet        use IPv4 only
-6      --family=inet6       use IPv6 only
        --family=auto        autodetect network type for single-family hosts only
        --family=all         try all network types
        --family=prefer-inet use all network types, but try IPv4 first [default]
        --family=prefer-inet6 use all network types, but try IPv6 first
-p PORT[:PORT2]
        --port=PORT[:PORT2]  server-side UDP port or range
                                (No effect on server-side SSH port)
        --bind-server={ssh|any|IP}  ask the server to reply from an IP address
                                       (default: "ssh")

        --ssh=COMMAND        ssh command to run when setting up session
                                (example: "ssh -p 2222")
                                (default: "ssh")

        --no-ssh-pty         do not allocate a pseudo tty on ssh connection

        --no-init            do not send terminal initialization string

        --local              run mosh-server locally without using ssh

        --experimental-remote-ip=(local|remote|proxy)  select the method for
                             discovering the remote IP address to use for mosh
                             (default: "proxy")

        --help               this message
        --version            version and copyright information

Working with Mosh

Mosh uses the same SSH method to connect to the remote server. Run the following command to connect to the remote server:

mosh root@your-server-ip

You will be asked to provide your root password to connect to the remote server.

If your remote SSH server is listening on port 2200, then you can specify it using the following command:

mosh --ssh="ssh -p 2200" root@your-server-ip

One of the best features of Mosh is that it will send you a notification if your network connection is lost. Once the network connection is restored, it will be synced to the server and you will be connected to the remote server automatically, so you don’t have to close the Mosh window.

Conclusion

In the above post, you learned how to install and use Mosh command-line tool on your Linux VPS from Atlantic.Net. It is a great tool compared to SSH. Generally, Mosh is used in slow network connections. You can visit Mosh documentation page for more information.