Get Better Remote Sessions on Linux With Mosh and Tmux

One of Linux’s strengths is its orientation toward networking, which is largely due to its Unix heritage. There’s a reason why Linux is an operating system of choice for servers.

The main way to remotely access Linux servers is through SSH, or Secure Shell. While it’s useful and secure, it was designed in an era before Wi-Fi and cellular connections became commonplace.

If you move your computer to a different Wi-Fi network or put it to sleep, you might find yourself disconnected with an apparently frozen terminal screen.

Why Mosh + Tmux?

Fortunately, there are a couple of tools that will make remote computing with SSH easier to use and more reliable. You can use them on everything from a physical terminal in a data center to a home server, on a cloud server, even another desktop machine running Unix or a BSD operating system.

Mosh is a tool on top of SSH that makes it aware of changing network connections, and tmux is a “terminal multiplexer” that lets you preserve a shell session across different logins and even share them.

Read More: What Is SSH and What Does It Stand For?

Getting Started With Mosh

Mosh is an open-source project that makes SSH work over roaming and intermittent connections, as many wireless connections are both.

You can switch your computer from a wired to a wired network, to a different wireless network, and your connection will stay up. You can also put your laptop to sleep while connected and after the network reconnects, you’ll be able to type in the terminal window.

Related: Manage Remote SSH Connections With These Linux Commands

If you lose your network connection, Mosh will tell you that it hasn’t heard from the remote server. If the server is slow echoing back characters, it’ll underline what you type until the latest screen comes back.

There’s more technical detail in a paper written by the authors, but installing it as a user is pretty simple. Mosh is widely available in most Linux distributions’ package managers. It’s also available as a Google Chrome app.

Install Mosh on Linux

For example, to install in Ubuntu:

sudo apt install mosh

Of course, the server will also have to have the Mosh server installed. The advantage of Mosh is that the server component can run in user mode, so you don’t have to be root to install it.

Using Mosh

When you want to connect to your remote machine, just type:

mosh username@server

Obviously, you’ll want to replace the “username” and “server” with your username and the name of the machine you’re trying to connect to.

See the documentation if you have more specialized needs, such as connecting to a different port.

If everything works correctly, you’ll have a remote terminal session, just with SSH. You’ll be able to issue commands the same way, but your connection will be more reliable.

Getting Started With Tmux

You have a remote terminal connection that can survive wireless network changes, but what about when you’re finally connected?

You might run a job that takes a long time, such as installing software or even a long compilation job. What happens if you get disconnected. The job will likely quit when the terminal does.

Plus, you can only do one thing at a time in the terminal, even with job control. If you wanted to run a full-screen editor, it will take up the entire terminal.

Tmux is one answer to these problems. It’s a “terminal multiplexer” that lets you run multiple terminals in one window. It’s like a tabbed terminal on a Linux desktop, but much more flexible.

You can detach a session and log out, to come back to it later and pick up right where you left off. You can open multiple terminal windows. You can edit a program in one window in an editor and test it at the command line in another.

Installing tmux is also very easy. Like Mosh, it’s included in many Linux distro package managers. In Ubuntu, you’d just type:

sudo apt install tmux

Obviously, you’ll have to install this on your remote machine to use it there, but it’s quite useful, locally as well.

When it’s installed, you launch tmux by using the command:

tmux

You’ll then launch into a terminal window, similar to one you’ve launched locally. You’ll see a status bar showing which window you’re in.

Tmux divides the screen into “windows” with multiple “panes”, showing their own terminals. You can rearrange and resize these panes, with limitless windows with their own panes.

Because tmux is a terminal-based program, it’s controlled entirely from the keyboard. The default prefix command to execute commands in tmux is Ctrl+B. This can be changed in the configuration file.

The user configuration file is the .tmux.conf in your home directory. The documentation has more details, but the number of customizations you can perform is extensive.

Tmux might have a learning curve, but with other “difficult” programs in the Linux/Unix world, many users find that the time spent put into learning these programs is well spent in what it lets them do.

One of the most powerful options is the ability to detach your session and reattach to it later. To do this, type Ctrl+D. To reattach, type “tmux attach” at the shell, and you’ll find yourself back at your session.

This means that your session will stay up as long as the machine does. This method is also popular for running chat apps like IRC, as people can stay in chat rooms for a long time. It’s also possible to share sessions with other people, such as for pair programming.

Combining Mosh and Tmux

The real power of a remote session comes from the combinations of Mosh and tmux.

With Mosh, you can log in to your remote machine from anywhere and not worry about the reliability of your connection. If you want to run something that might take a long time, you can detach your session and come back to it later.

The combination of Mosh and tmux will allow you maximum reliability and flexibility in your remote logins. There’s a lot more to explore in tmux alone that will unleash the power of your terminal sessions.

Source: makeuseof.com

Related posts

Why You’ll Start Seeing More Original Content on Instagram Soon

Why Doesn’t Copilot Help You Fix Problems in Windows?

Default Passwords Are Being Banned, but You Should Always Change Them Anyway