How to Keep Remote SSH Sessions Running after Disconnection

superior_hosting_service

Keep SSH Sessions Running
Keep-SSH-Sessions-Running

SSH (Secure Shell) is the end to end encrypted networking system that allows users to get access remotely from client to server or system. Because of its asymmetric cryptography security system, it’s pretty much safe and secure to access the server even from an unsafe client network. But sometimes due to inactivity or bad network signal, SSH remote access can get disconnected. Whatever the reason is, as a Linux administrator, it’s a vital job to keep SSH sessions and processes running after disconnection.

Reasons Why SSH Sessions Get Disconnected


In a nutshell, SSH tunnel proxy error, network timeout, using the wrong network port, or even not logged into your system as root user can also get you disconnected from the SSH remote administration. In this post, we are going to discuss the most frequently asked question about SSH; how to keep SSH sessions and processes running after disconnection is occurred.

1. Keep Running SSH Sessions Using the screen Command


The screen command is the most used and useful command for SSH administration. The screen command can resolve hostname problems for both IPv4 and IPv6 addresses. With root functionalities, the screen command can log out or dismiss a session from the client end to the server end. If you’re a very newbie at Linux SSH, here are some CLI that can help you to install and find manuals of the screen command.

For Ubuntu:

$ sudo apt install screen

For OpenSUSE:

$ sudo zypper install screen

For Arch Linux:

$ sudo pacman -S screen

For Red Hat Enterprise Linux:

$ sudo yum install screen

After you’re done installing, start monitoring your system start screen.

$ screen
$ screen --help
Screen
screen-for-Reasons-why-SSH-Connections-Get-Disconnected

Once you have the access, SSH into the server, you want to get access remotely. If you’re planning a long night work from your station, but you don’t want to get your SSH client down, you can use the screen command. This can detach your screen session, but no worries, the tasks you initiated will be completed.

To detach your screen press Ctrl-A and then Ctrl-D from your keyboard. You can log in to monitor the work progress anytime from your terminal. To re-connect or connect with the existing one with the session, use the following CLI.

$ screen -r
screen -D -r

2. Keep Running SSH Sessions Using the tmux Tool


The tmux or terminal multiplexer is a very popular and useful tool for programmers and system administrators that allows users to switch between program to program in the same terminal. To keep SSH sessions running in the background, tmux can be a very quick solution. It can detach and reattach applications where the processes are kept running in the background. Here is the installation instruction of tmux for Linux distros.

For Arch Linux:

$ sudo pacman -S tmux

For Red Hat Enterprise Linux:

$ sudo yum install tmux

For Ubuntu:

$ sudo apt-get install tmux

For OpenSUSE:

$ sudo zypper install tmux

After installing tmux, start the tmux session using terminal commands. It won’t take much time to get installed. Once you’ve done installing you can now get started with tmux. To start tmux, just type tmux in the terminal.

$ tmux
tmux
Basic-tmux-Tutorial-Windows-Panes-and-Sessions-over-SSH

If you need to run more than one terminal multiplexer to keep all SSH sessions running in the background, in that case, you may find difficulties to switch between tmux to tmux. Here are the command lines to switch one form another.

$ tmux detach
$ tmux attach
$ tmux attach -t 2

To check all the tmux screen use the tmux list command.

$ tmux ls

3. Keep Running SSH Sessions Using the byobu Tool


The byobu tool is mostly used for remote SSH administration and on-screen monitoring on Linux. It is an open source-sourced software and can be used alongside the tmux or the screen tool. You can see the current status, notifications, and messages with the byobu tool. Though the byobu comes installed by default with Ubuntu. But if it doesn’t, you can install it inside your Linux machine. To check whether byobu is installed or not in your system by checking the other versions of byobu.

$ byobu --version

Then enable byobu, it will help to keep your SSH sessions running in the background after even disconnection.

$ byobu-enable

Here the installation processes are explained with CLI.

For Ubuntu:

$ sudo apt install byobu

For OpenSUSE:

$ sudo zypper addrepo https://download.opensuse.org/repositories/utilities/openSUSE_Leap_42.3/utilities.repo
$ sudo zypper refresh
$ sudo zypper install byobu

For Arch Linux:

$ yaourt -S byobu
$ packer -S byobu

For Red Hat Enterprise Linux:

$ sudo yum install byobu

For Fedora:

$ sudo dnf install byobu

To get started with byobu, just type byobu in the terminal and hit Enter.

$ byobu

Now, if you’re using the tmux or the screen, you can choose and select between any of them for backend collaborated.

$ byobu-select-backend

Now, you can manage and keep your SSH sessions running in the background after disconnection, use the system keyboard function keys. To get started with SSH alongside byobu, press CTRL+SHIFT+F2 from your keyboard to enable the SSH environment. To move your SSH sessions forward and backward you can use ALT+UP and ALT+DOWN.

To disconnect your SSH sessions press F6 from the keyboard. Now, if you’re looking for detaching but not disconnect the session, here you go. To detach SSH sessions and still get connected, press SHIFT+F6. Here is one more extra keyboard command for you. To keep only current the screen session active and close all other windows press ALT+F6.

4. Keep Running SSH Sessions Using the nohup Command


The nohup or ‘no hangup’ command is a very useful alternative tool for the screen or the tmux. It also allows users to keep the SSH sessions running even after they got disconnected. The nohup command tells the system to run all processes in the background by avoiding the signal hang up (SIGHUP).

To check the nohup manuals and options form your Linux terminal, type the following command in the terminal and hit Enter.

$ nohup options

To check the currently active job lists, use the -l command from your terminal.

$ jobs -l

Now, for SSH connection, to avoid connection lost and keep your sessions running after disconnection, use the nohup command lines following by your job. You can also get the output list of your jobs in a text file by using the cat command alongside the nohup command.

$ nohup ./hello.sh
$ cat nohup.out

To run a process in the background, you can use the & symbol just after the nohup command. Like, if I want to test ping for wordpress.com in the background, the command line will be just like the following line below. And when you think your job is done, to see the outcome, use the pgrep command.

$ nohup ping ubuntupit.com &
$ pgrep -a ping

5. Keep Running SSH Sessions Using the disown Command


If you don’t have the system root privileges, maybe this one is going to solve your problem. The disown command can make any task unlisted from your running system log. Thus, it can hide an undergoing process to avoid all the auto log out errors or the signal hang up (SIGHUP). You can actually use the diswon process management command to keep your SSH sessions running by hiding your task.

To remove or hide your task from the task manager, use the diswon command in the terminal.

$ disown <task>

To check the current status of the task, use the current shell command.

$ current shell

You can also set a list of tasks at a single line terminal command.

$ disown jobs1
$ disown jobs1 jobs2 ... jobsn

And to check the task list, use the -l command.

$ jobs -l

To remove all current jobs from the window, type the -a syntax following by the disown command.

$ disown -a

To remove only one or the running job from the window, use the -r syntax following by the diswon command.

$ disown -r

Now for SSH, to run a job in the background after the session is connected, use the -h syntax. This process can keep your SSH sessions running even after disconnection.

$ disown -h jobID
$ disown -h %2

Fixing the timeout Error of SSH in RHEL


When you’re connected to the SSH on Red Hat Enterprise Linux (RHEL), you may get a frequent timeout problem and get your connection lost. This problem occurs due to the invalid response time form either the client end or the host end. In your Red Hat root directory, you have to find and edit the sshd_config.txt file to solve these timeout problem. Once you’re done, you can get your SSH connection back even after getting disconnected.

There inside the sshd_config.txt, file you will find two options named as ClientAliveInterval and ClientAliveCountMax, you need to edit these log files to maximize the server to client response time. The timeout interval is calculated by multiplying the values of ClientAliveInterval and ClientAliveCountMax.

All you need is to edit the time duration of this two values according to your system and network responding duration. Let you want to maximize the time 10 minutes for the client interval end and 5 times for the client count, then your text registry inside the sshd_config.txt file will be like below. And then, restart the SSH.

ClientAliveInterval 10m
ClientAliveCountMax 5

For a system administrator, keep SSH sessions and processes running is a crucial job to satisfy the client and to fulfill the task. Getting disconnected and frequent sessions lost is very much annoying and bothersome. So in this post, we have tried to discuss and illustrate the reasons why SSH sessions get disconnected and also described all the possible methods on how to keep SSH sessions running after disconnection.

If you have ever gone through the hassle of SSH session disconnection and know how annoying it could be, please share your experience with us. And also do comment if you have anything to add or ask anything related to this post in the comment section. Don’t forget to share this post with your friends on social media.