LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   close dead ssh connections (https://www.linuxquestions.org/questions/linux-server-73/close-dead-ssh-connections-937984/)

nuliknol 04-03-2012 08:15 PM

close dead ssh connections
 
I have a DSL modem internet connection at home and when I am logged in to my dedicated internet server using ssh, the modem times out my connections when they are idle. The result is, I am accumulating dead ssh connections being idle for days (wasting system resources) that I have to kill with -9 periodically. This is how they look like when I ps:

Code:

....
root    13610  0.0  0.1  5084  1160 ?        S    Apr02  0:00 su -c /usr/bin/xterm
root    13612  0.0  0.2  8104  2364 ?        S    Apr02  0:00  \_ /usr/bin/xterm
root    13616  0.0  0.1  4772  1388 pts/29  Ss+  Apr02  0:00      \_ bash
root    14091  0.0  0.1  5084  1156 ?        S    Apr02  0:00 su -c /usr/bin/xterm
root    14093  0.0  0.2  8100  2488 ?        S    Apr02  0:00  \_ /usr/bin/xterm
root    14095  0.0  0.1  4768  1448 pts/30  Ss+  Apr02  0:00      \_ bash
root    22160  0.0  0.1  5084  1164 ?        S    Apr02  0:00 su -c /usr/bin/xterm
root    22162  0.0  0.2  8100  2480 ?        S    Apr02  0:00  \_ /usr/bin/xterm
root    22164  0.0  0.1  4768  1384 pts/32  Ss+  Apr02  0:00      \_ bash
root      6946  0.0  0.1  5084  1156 ?        S    Apr03  0:00 su -c /usr/bin/xterm
root      6948  0.0  0.2  8100  2360 ?        S    Apr03  0:00  \_ /usr/bin/xterm
root      6950  0.0  0.1  4768  1388 pts/25  Ss+  Apr03  0:00      \_ bash
root    16522  0.0  0.1  5084  1160 ?        S    Apr03  0:00 su -c /usr/bin/xterm
root    16524  0.0  0.2  8100  2484 ?        S    Apr03  0:00  \_ /usr/bin/xterm
root    16526  0.0  0.1  4772  1392 pts/36  Ss+  Apr03  0:00      \_ bash
root    22133  0.0  0.1  5088  1156 ?        S    Apr03  0:00 su -c /usr/bin/xterm
root    22135  0.0  0.2  8100  2364 ?        S    Apr03  0:00  \_ /usr/bin/xterm
root    22137  0.0  0.1  4768  1388 pts/40  Ss+  Apr03  0:00      \_ bash
root    28787  0.0  0.1  5084  1156 ?        S    Apr03  0:00 su -c /usr/bin/xterm
root    28789  0.0  0.2  8100  2488 ?        S    Apr03  0:00  \_ /usr/bin/xterm
root    28791  0.0  0.1  4768  1388 pts/43  Ss+  Apr03  0:00      \_ bash
[root@newze ~]#

I know I can setup ssh client with keep-alive and it will send some data to avoid the connection being expired by the firewall of the modem, but I don't want this (since it is not a clean solution). I want the ssh daemon to kill idle connections and the resulting child processes automatically after some inactivity period (which I can configure somewhere). Is this possible?

My OS is a bit old: centos-release-5-2.el5.centos , hope this is not an issue, I just can't upgrade this server right now, its a productive environment.

Will appreciate any comments very much on this case.
Thanks

anomie 04-03-2012 10:48 PM

Actually, setting ServerAliveInterval 30 (or some such) in your client-side ~/.ssh/config is a clean solution, IMO.

If that's still a no-go, then just let bash(1) time out your session. In your server-side ~/.bash_profile, set TMOUT=600 (or similar).

nuliknol 04-04-2012 08:45 AM

Quote:

Originally Posted by anomie (Post 4644167)
Actually, setting ServerAliveInterval 30 (or some such) in your client-side ~/.ssh/config is a clean solution, IMO.

it is not clean because if the modem is reset and new ip is assigned, the connections will remain on the server side.

Quote:


If that's still a no-go, then just let bash(1) time out your session. In your server-side ~/.bash_profile, set TMOUT=600 (or similar).
this one looks good. thanks!

nuliknol 04-04-2012 09:00 AM

I found it, ClientAliveInterval configured on the server in sshd_conf should do what I need. Testing...

anomie 04-04-2012 05:42 PM

Quote:

Originally Posted by nuliknol
it is not clean because if the modem is reset and new ip is assigned, the connections will remain on the server side.

If that's a regular concern, then consider using GNU screen or tmux.

I often work during my bus ride in to work. (The connection quality is terrible, and is dropped at least once during the ride.) With tmux, I simply re-attach session when the connection is active again.

unSpawn 04-04-2012 06:25 PM

Also see Autossh to re-establish connections automagically.


All times are GMT -5. The time now is 05:31 AM.