Home Terminal multiplexers How To Autostart Tmux Session On Remote System When Logging In Via SSH In Linux

How To Autostart Tmux Session On Remote System When Logging In Via SSH In Linux

By sk
Published: Last Updated on 14.2K views

As a system admin, you may frequently access your remote servers via SSH. In this brief guide, I am going to explain why and how to autostart Tmux session on a remote system when logging in via SSH in Linux.


It is always a good practice to run a long running process inside a Tmux session when working with remote systems via SSH. Because, it prevents you from losing the control of the running process when the network connection suddenly is terminated.

Just in case the network connection gets dropped for any reason, the processes inside the Tmux session will keep running on the remote systems, so you can re-attach to the Tmux session using "tmux attach" command once the network connection is back online.

OK, but what if you forgot to start the Tmux session in the first place? No matter how careful we're, sometimes we forget to start the Tmux session. Here is a simple way to avoid this problem. You can autostart Tmux session on the remote systems when logging via SSH.

This is especially helpful if you lost the network connection when upgrading a remote Linux server via SSH from your local system.

Please note that the following tip will automatically start a Tmux session whenever you try to log in to your Linux system either locally or remotely via SSH from another system.

Autostart Tmux Session on Remote System when Logging in Via SSH

To autostart Tmux session when connecting via SSH, edit your remote system's ~/.bash_profile file:

$ nano ~/.bash_profile

If the file is not available, just create it.

And add the following lines in it:

if [ -z "$TMUX" ]; then
    tmux attach -t default || tmux new -s default

Save and close the file. Log out and log back into the remote systems. You will be automatically landed inside a new Tmux session named 'default'.

Now, start any process inside the Tmux session and detach it from by pressing Ctrl+b followed by d. The process will keep running inside the Tmux session even after the network connection is dropped. Once the network connect is back, SSH into the remote system and you will see that the job is still running!!

Have a look at the following animated output:

Visual Demo - Autostart Tmux Session On Remote System When Logging In Via SSH
Visual Demo - Autostart Tmux Session On Remote System When Logging In Via SSH

Let me explain what I just did in the previous steps.

  1. I connect to my remote Ubuntu server via SSH from my local system. A new Tmux session named "default" is automatically started.
  2. Inside the Tmux session, I run a command (Ex. watch grep \"cpu MHz\" /proc/cpuinfo) to monitor the CPU speed.
  3. Then, I detached from the session by pressing Ctrl+b and d.
  4. And then, I list the Tmux sessions using "tmux ls" command and exit the SSH session.
  5. Finally, I reconnect to the remote system via SSH and verify if the process is keep running inside the Tmux session without any interruption.

Hope you understand.

To stop auto-starting Tmux session, simply delete the lines that added earlier from the your remote system's ~/.bashrc or ~/.bash_profile file.

This simple tip helps you to avoid losing the control of your remote jobs. You don't always have to manually start Tmux every time. Just make Tmux to start automatically whenever you SSH into a remote system as described in this guide.

Suggested Read:

You May Also Like


Juan Castaneda December 2, 2020 - 3:47 am

What if I don’t Loggin to the remote device? tmux will make the reconnection if I don’t loggin or not?
Is mandatory the ssh login in order to reboot tmux?

sk December 2, 2020 - 11:30 am

If you don’t login, tmux won’t start.

Paolo October 2, 2023 - 8:50 am

How can I supplement this script so that it offers to start a new session or connect to an existing session with each new connection? As a selection menu.

sk October 2, 2023 - 5:26 pm Reply

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