ssh connection to raspberrypi hangs after rougly 30 seconds, unless I ping the raspberrypi.

The raspberry pi is at raspberrypi.lan

I have a script ~/troubleshoot.sh

#! /bin/bash
while :; do
    sleep 1; echo $SECONDS

I can run this while logged into raspberrypi.lan via ssh, or run it remotely from my laptop:

ssh raspberrypi ./troubleshoot.sh
 19:34:35 up 11 days, 23:05,  2 users,  load average: 0.00, 0.01, 0.00
               total        used        free      shared  buff/cache   available
Mem:          439596       73516      112016         160      254064      308364
Swap:         102396      101940         456

... 4, 5, 6 etc ...


This will hang when $SECONDS is around 30 ... it might be as low as 28, or as high as 34. When run remotely, I have to use Ctrl+C to terminate.

If I'm logged into raspberrypi.lan via ssh, I see the same behavior, but I have to use Enter ~ ..

... so something is causing the ssh session to hang.

If I open a separate terminal window on my laptop and run

ping raspberrypi.lan

the ssh session will not hang; I can stay logged on indefinitely.


I had an old version of Raspbian running on the pi -- so old that the repositories were well out of date. ssh worked under that version. I loaded Raspbian GNU/Linux 11 (bullseye) on a new SD card and enabled ssh, then noticed that my ssh sessions were freezing after a short period of time.


  • Hardware: Raspberry Pi 1B

  • OS/Distro Version: Raspbian GNU/Linux 11 (bullseye)

  • Network: Wired, dual stack (ipv4, ipv6)

    $ host raspberrypi.lan raspberrypi.lan has address raspberrypi.lan has IPv6 address 2607:fb90:2c88:43b2:b04c:80cb:0:167

  • IP addresses are dynamic on both IPV4 and IPV6

  • sshd: OpenSSH_8.4p1 Raspbian-5+deb11u1, OpenSSL 1.1.1n 15 Mar 2022

  • /etc/ssh/ssh_config: Include /etc/ssh/ssh_config.d/.conf Host * SendEnv LANG LC_ HashKnownHosts yes GSSAPIAuthentication yes

  • no files in /etc/ssh/ssh_config.d/

  • /etc/ssh/ssh_config is identical to that of another server that I have no issues logging in to.

  • ~/.ssh/config on laptop: Host raspberrypi User barton Hostname raspberrypi.lan ServerAliveInterval=60 ServerAliveCountMax=10

What I've tried so far

  • ~/troubleshoot.sh shows uptime -- this is now over 12 days. This rules out power issues causing the pi to reboot.
  • raspberrypi.lan is constantly pingable -- this isn't a network connectivity issue.
  • All ip addresses on the lan are dynamic; this isn't an IP address conflict.
  • I used the check.sh script posted by @goldilocks in his reply to Raspberry pi always stops responding to SSH after some hours powered to determine that sshd is not dying.
  • Tried adding ServerAliveInterval 60 ServerAliveCountMax 10 to /etc/ssh/ssh_config and restarting sshd. No change.
  • Looked in /var/log/syslog and /var/log/messages for ssh errors. Didn't see anything.
  • Tried connecting using ssh -v -v -v raspeberrypi.lan didn't see anything unusual (much as I suspected, since I was connecting correctly).
  • Ran journalctl -u ssh -- I probably need to go a bit farther down this path, because I was tired when I tried it, and didn't think to nohup it and redirect to a file.

1 Answer 1


I actually found a clue/work-around while writing up the question.

The ping that was keeping things alive was returning ipv6 packets:

$ ping raspberrypi.lan
PING raspberrypi.lan(raspberrypi.lan (2607:fb90:2c88:43b2:b04c:80cb:0:167)) 56 data bytes
64 bytes from raspberrypi.lan (2607:fb90:2c88:43b2:b04c:80cb:0:167): icmp_seq=1 ttl=64 time=5.61 ms
64 bytes from raspberrypi.lan (2607:fb90:2c88:43b2:b04c:80cb:0:167): icmp_seq=2 ttl=64 time=11.4 ms
64 bytes from raspberrypi.lan (2607:fb90:2c88:43b2:b04c:80cb:0:167): icmp_seq=3 ttl=64 time=67.9 ms
64 bytes from raspberrypi.lan (2607:fb90:2c88:43b2:b04c:80cb:0:167): icmp_seq=4 ttl=64 time=78.5 ms

I tried adding AddressFamily=inet6 to ~/.ssh/config:

Host raspberrypi
    User barton
    Hostname raspberrypi.lan

... this didn't work, but when I changed it to AddressFamily=inet -- forcing traffic over IPV4 -- I was able to connect without pinging raspberrypi.lan.

I think that the ipv6 network is timing out somehow, and the ping is keeping it alive, but the ipv4 network is staying alive. It's still a mystery, but at least I have a workaround that doesn't involve setting up a ping in parallel to my ssh session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.