How do I terminate all idle incoming ssh connections?

Solution 1:

Open up terminal, Ctrl-Alt-T. Then:

  • sudo gedit /etc/ssh/sshd_config

  • Set (or modify) the following options:

    ClientAliveInterval 600
    ClientAliveCountMax 0
    
  • Save and exit the editor.

  • Run sudo service ssh restart

ClientAliveInterval sets the period after which, if the server sees the client as idle, it will send a "keep alive" request. If it gets no response, the client is disconnected. ClientAliveCountMax is the number of times such a keep alive is requested before disconnection. By setting it to 0, the keepalive is never sent and all clients idle for more than 10 minutes (600 seconds) are disconnected.

Solution 2:

Edit /etc/ssh/sshd_config

sudo vim /etc/ssh/sshd_config

Add in this file the ClientAliveInterval option in seconds.

By the MAN page of sshd_config, that you can check here :

ClientAliveInterval: Sets a timeout interval in seconds after which if no data has been received from the client, sshd will send a message through the encrypted channel to request a response from the client. The default is 0, indicating that these messages will not be sent to the client. This option applies to protocol version 2 only.