Disconnect Frozen/Hung SSH Session in Linux

This article will go through how to disconnect frozen/hung SSH session in Linux. SSH can hung due to several reasons, we are going to look at several examples of how to disconnect frozen SSH.

Manually disconnect frozen/hung SSH Session in Linux

  • To disconnect the frozen SSH session, press Enter ,~ and . key respectively.

Enter key – goes to the next line
~ – is used as an escape character by the SSH client
. – instruct the client to terminate

Automatically disconnect frozen/hung SSH Session in Linux

  • We are going to use ServerAliveInterval in the examples below to automatically disconnect frozen SSH session in Linux.
  • In the example below, let us connect to nixpc2 and use -o ServerAliveInterval=tt where tt is the timeout value. The session below will be disconnected after 5 seconds.
ssh -vvv -i /export/home/itnixpro/.ssh/id_rsa -o ServerAliveInterval=5 -o ServerAliveCountMax=2 root@nixpc2 "/sbin/ifdown bond0"

Sample output

debug3: send packet: type 80
debug3: send packet: type 80
debug3: send packet: type 1
packet_write_wait: Connection to nixpc2  port 22: Broken pipe
  • Another option is to create a configuration inside the home directory.
sudo touch /home/itnixpro/.ssh/ssh_config

Paste the config below into the file. Note, you can replace * with specific IP so that the rule will only apply to the specified IP address.

Host *
 ServerAliveCountMax 3
 ServerAliveInterval 10
  • You can also add the disconnection rule to the default SSH configuration. Open the configuration using the command below.
sudo nano /etc/ssh/ssh_config

Then paste the content below.

Host *
 ServerAliveCountMax 3
 ServerAliveInterval 10
  • That concludes our guide on how to disconnect frozen/hung SSH session in Linux.

Read more about SSH

Other Tutorial

How to kill a process in Linux

How to find path to a command in Linux

How to move or copy a directory in Linux

System administrator | Software Developer | DevOps

Leave a Comment