The script checks for SSH sessions that have been idle for a specified amount of time and terminates them: #!/bin/bash Be cautious with automation, though, as it might inadvertently terminate active sessions if not configured correctly.īelow is a sample Bash script that you can modify and use to kill inactive SSH sessions. Also, see htop: Quick Guide & Customization, 90 frequently used Linux Commands, and top, atop, others.Īutomating the Process with a script ( ssh_idle_killer.sh)įor a more automated approach, you can write a script that periodically checks for idle SSH sessions and kills them based on specific criteria (like duration of inactivity). Notice below the inactive ssh session with the ‘top’ command left running. Using htop to Identify and Kill Idle SSH SessionsĪnother method for identifying idle (orphaned ssh sessions) is by using htop. This command sends a signal to terminate the session. Replace with the actual process ID of the idle SSH session. Once you have identified an idle session’s PID, you can terminate it using the kill command. However, as a basic approach, you could look for sessions with low CPU and memory usage, indicating potential idleness. Determining Which Sessions are Idleĭetermining idleness can be somewhat subjective and depends on the specific criteria you set (like CPU usage, time since last activity, etc.). Each line represents a session, and the first number after the username is the process ID (PID) of the session. This command lists all active SSH sessions. Run the following command: ps aux | grep sshd: To find idle SSH sessions, you can use ps combined with grep to filter SSH-related processes. This approach allows you to directly target and terminate SSH sessions that have been inactive. ![]() Now to prevent this from happening in the future, lets edit /etc/ssh/sshd_config and add these two config lines: ClientAliveInterval 600ĬlientAliveCountMax 3 Using ps and grep to Identify and Kill Inactive SSH SessionsĪn alternative and effective method to manage and kill idle SSH sessions involves using the ps (process status) and grep commands. Replace 3649 with your idle ssh session PID. ![]() To kill the idle ssh session, type: kill 3649 The line including “pstree” would be your session! :) Now look for the session’s parent PID from the line without the “pstree” command. But here’s an easy after-the-fact method: Including editing your sshd_config, discussed below. ![]() There are a few ways to kill idle ssh sessions. Using for example the “ w” command, you see something like the following: 22:47:28 up 315 days, 21:09, 2 users, load average: 9.04, 8.17, 7.30 Using w or pstree to Identify then Kill Idle SSH Sessions Automatically kill idle SSH sessions by editing sshd_config.ssh_idle_killer.sh Script Modification Instructions.Automating the Process with a script (ssh_idle_killer.sh).Using htop to Identify and Kill Idle SSH Sessions.Using ps and grep to Identify and Kill Inactive SSH Sessions.Using w or pstree to Identify then Kill Idle SSH Sessions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |