- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Keeping SSH session alive on Linux
Secure Shell (SSH) is a network protocol that allows secure remote connections between two systems. It is commonly used to access and manage Linux servers remotely. However, one of the problems with using SSH is that your session can be terminated due to downtime or network outages. This can be frustrating, especially if you're working on a long-running task that requires an uninterrupted connection. In this article, we will discuss various methods of keeping an SSH session alive on Linux.
Using the ClientAlive Interval Option
One way to prevent an SSH session from closing due to inactivity is to use the “ClientAliveInterval” option. This option sends a null packet to the server at a specified interval to keep the connection alive.
To use the ClientAliveInterval option, you will need to edit the SSH server configuration file, typically located in “/etc/ssh/sshd_config”. Open the file in a text editor and add the following line −
This will send a null packet to the server every 60 seconds to keep the connection alive. The interval can be set to a value that suits your needs. After making the changes, save the file and restart the SSH server for the changes to take effect. On most systems, you can do this using the following command −
$ sudo systemctl restart ssh
You can verify that the ClientAliveInterval switch is working by examining the log file located at “/var/log/auth.log”. You should see a message similar to the following −
Jan 4 2023 10:30:01 server sshd: Connection from 192.168.43.1 port 5676 Jan 4 2023 10:30:01 server sshd: ClientAliveInterval 60
Using the Nohup Command
Another method to keep an SSH session alive is to use the nohup command. The nohup command allows you to run a command in the background, even after you log off the Keeping SSH session alive on Linuxv system. This can be useful if you need to perform a long-running task that requires an uninterrupted connection.
To use the nohup command, simply prefix the command you want to run with "nohup" and redirect the output to a file. For instance −
$ nohup mycommand > mycommand.out 2> mycommand.err < /dev/null &
This will run the "mycommand" command in the background and redirect the output to the "mycommand.out" and "mycommand.err" files. The "< /dev/null" part redirects stdin (standard input) to /dev/null, allowing the command to run without waiting for input. The "&" at the end puts the command in the background.
To check the status of the command, you can use the "jobs" command. This will display a list of all background processes running on your system.
$ jobs + Running nohup mycommand > mycommand.out 2> mycommand.err < /dev/null &
To bring a background job to the front, you can use the "fg" command followed by the job number. For instance:
$ fg %1
This will bring up the first background job (in this case "mycommand").
Using the Screen Command
Another method to keep an SSH session alive is to use the screen command. The screen command allows you to create virtual terminal sessions that can run independently of the main terminal. This can be useful if you need to run multiple tasks simultaneously or if you want to keep a task running after you log off the system. To use the screen command, simply type "screen" at a terminal prompt and press Enter. This will create a new virtual terminal session. You can then run any command you want within the screen session.
To log out of the screen session, use the key combination "Ctrl+a" followed by the "d" key. This will take you back to the main terminal, but the screen session will continue to run in the background.
To reconnect to a screen session, use the "screen -r" command. A list of available screen sessions will appear, and you can select the one you want to reconnect.
Here is an example showing how to keep the SSH session alive with screen command −
$ screen [screen is now running] $ <execute few commands> [ctrl+a d] [detached from screen] $ screen -r [screen session restored]
In this article, we discuss several methods of keeping an SSH session alive on Linux. Using the ClientAliveInterval switch, the nohup command, or the screen command, you can ensure that your SSH sessions remain alive and that your tasks can run without interruption. With these tools in your arsenal, you can easily manage your Linux servers remotely and stay productive even when working on long-running tasks.
Kickstart Your Career
Get certified by completing the courseGet Started