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.
Introduction
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 fi
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:
Let me explain what I just did in the previous steps.
- I connect to my remote Ubuntu server via SSH from my local system. A new Tmux session named "default" is automatically started.
- Inside the Tmux session, I run a command (Ex.
watch grep \"cpu MHz\" /proc/cpuinfo
) to monitor the CPU speed. - Then, I detached from the session by pressing
Ctrl+b
andd
. - And then, I list the Tmux sessions using "
tmux ls
" command and exit the SSH session. - 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:
4 comments
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?
If you don’t login, tmux won’t start.
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.
Please check this guide – https://ostechnix.com/enhancing-ssh-login-with-a-tmux-session-selection-menu/