SSH connection to VM timeouts repeatedly after 1-3 hours

Varun Yadav 20 Reputation points
2025-04-29T07:54:33.24+00:00

Hi,
I am running a Virtual Machine with Ubuntu as OS. and it has ssh connection open at port 22. now this connection keeps getting timed out every few hours and when I restart the VM it works fine. VM metrics do not show anything out of ordinary. How can I solve this please reply ASAP.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,743 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Burlachenko 4,550 Reputation points
    2025-04-29T11:29:14.87+00:00

    Dear Varun,

    Thank you for reaching out. SSH timeouts can occur due to idle sessions, firewall rules, or TCP/IP settings. Try to adjust SSH Timeout: ModifyUser's image to include:

    ClientAliveInterval 300

    ClientAliveCountMax 2

    Then restart SSH: sudo systemctl restart sshd.

    Check Azure NSG Rules: Ensure your Network Security Group (NSG) allows persistent SSH traffic. Review rules here.

    Enable Boot Diagnostics: Verify VM health via Azure Boot Diagnostics.

    TCP Keepalive: Enable keepalive on your client (~/.ssh/config):

    ServerAliveInterval 60

    For further details, refer to Azure’s SSH troubleshooting guide.

    Let me know if the issue persists.

    https://learn.microsoft.com/en-us/troubleshoot/azure/virtual-machines/linux/troubleshoot-ssh-connection

    
    Best regards,
    Alex
    PS If my answer help to you, please Accept my answer.
    PPS that is my answer and it is not a comment :)
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.