Home Tmux How Tmux Saved My Work and Why You Should Use It for Unstable SSH Connections in Linux

How Tmux Saved My Work and Why You Should Use It for Unstable SSH Connections in Linux

By sk
1.2K views 5 mins read

Have you ever been running a long task on a remote server, only to lose your SSH connection halfway through? That happened to me a few times. In this post, I’ll share how Tmux saved my work when SSH kept dropping and why you should use it for unstable SSH connections in Linux.

The Problem

The other day I was downloading a large database backup from a remote server. It was a big file and the download was going to take hours.

While it was running, our network started having problems. My SSH connection kept disconnecting. The network was unstable.

Normally, if the connection breaks, the download stops too. That means I’d have to start over.

But this time, everything was fine. The download kept going.

Why?

Because I was running the backup task inside a tmux session!

What is Tmux?

Tmux is a terminal multiplexer that allows you to run multiple terminal sessions within a single window. It lets you manage and switch between different tasks, keep processes running in the background, and even reconnect to sessions if your connection drops.

Tmux is especially useful for remote work via SSH, as it keeps your commands running even when the connection is unstable. You can leave a session and come back later. Your work is still there.

How Tmux Helped Me during Unstable SSH Connections

Before I started the download, I opened a new tmux session using the following command:

tmux new -s backup_task

Then I started the download inside that tmux session, using a command like:

wget https://example.com/big-database.sql.gz

Later, when my SSH connection dropped, I logged in again and ran:

tmux attach -t backup_task

And just like that, I was back where I left off. The download was still running. I didn’t lose any progress.

I also implemented a simple workaround to automatically start a tmux session on a remote system when logging in via SSH. This has been helpful on multiple occasions when I forgot to start the tmux session initially.

If you're interested, check the following link to know how I did it:

Why I Recommend Tmux

If you work on remote servers or use SSH a lot, tmux is a very useful tool. It helps in many ways:

  • Keeps your work running, even if the connection drops,
  • Lets you leave and come back later,
  • Lets you split your screen into parts,
  • Makes it easy to scroll, copy, and paste.

Next time you’re about to run anything important remotely, do not forget to run tmux new -s my_session before hitting Enter. Your future self will thank you when Wi-Fi betrays you.

I personally use tmux, but you can also use screen, another good terminal multiplexer. Both tmux and screen are reliable tools for managing terminal sessions.

When Tmux Can’t Help

Tmux is a lifesaver for commands running on the remote server (like installing software or downloading files to the server).

But if your task depends on your local SSH connection, like downloading a file from the server to your laptop, then Tmux won’t save you.

For example, if you try to download a file from the remote server to your local machine (e.g., scp server:file.txt .), the transfer will fail when SSH drops. Because your local machine is the receiver.

Tmux will only help in the following scenario.

For instance, let us say you’re accessing your remote server via SSH. You open a tmux session and start downloading a file from the Internet to your remote server (e.g., wget or curl on the server).

After a while, your local computer's SSH connection to the remote server drops. What happens now?

The download continues uninterrupted on the remote server because Tmux keeps it running. When you reconnect via SSH later, you can reattach the Tmux session and check the download’s progress.

The download depends on the remote server's internet connection, not your local SSH connection.

Tmux acts like a "shield" for the remote server’s terminal. It doesn’t care if your local SSH disconnects.

Key Takeaway:

Tmux won’t save transfers that rely on your local SSH connection, but it can protect remote-side operations (like fetching a file to the server). For downloads to your local machine, use tools like rsync --partial or fetch the file to the server first.

Hope this helps you understand what tmux can and can't do. In the following sections, I’ll explain you how to get started with tmux.

How To Start Using Tmux

Install Tmux

Tmux is available in the default repositories of most Linux distributions.

On Debian, Ubuntu and its derivatives, you can install tmux using command:

sudo apt install tmux

On Fedora, RHEL:

sudo dnf install tmux

Basic Tmux Usage

Start a new session:

tmux new -s mysession

This is will start a new session called "mysession". Inside that you can run your commands.

Then, detach from the session by pressing Ctrl + b, then d. Don't worry the commands will still be running inside the tmux session.

You can reattach later using command:

tmux attach -t mysession

Thank You, Tmux Developers

I want to thank everyone who works on tmux and other similar tools. These projects don’t get much attention, but they play a very important role in system administration.

If you use the terminal often, I highly recommend trying tmux or screen. Both are easy to learn and can help protect your work when something goes wrong.

Recommended Read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More