ssh: connect to host x.x.x.x port 22: Connection timed out

Did you just change some network configuration on your Ubuntu virtual machine and now you’re not able to connect? Here’s how you can fix the ssh connection.

how to fix – ssh: connect to host x.x.x.x port 22: Connection timed out


I spinned up an Ubuntu (18.04.3 LTS, Bionic Beaver) virtual machine on Azure for an experiment. I was setting up apache2 server (for a web server) and in the process I made some changes in network configuration (I literally allowed IPv4 & IPv6 in the firewall, that’s all). While making those changes I got a clear message on the screen that the current network may be unstable but it continued to work flawlessly until the connection was closed. The very next day and I wasn’t able to connect to the Ubuntu VM using ssh on the same IP/Port. Though the web server was responding well at port 80.

What I tried first?

I had forgotten the warning message I received earlier, about the network instability, while making the changes in network configuration and now I had this. So, at first I jumped on to Azure Portal and I tried restarting the virtual machine, it didn’t work. I removed and re-added the allow port 22 under the Networking tab, it also didn’t work.

How to fix “ssh: connection to host X.X.X.X on port:22”?

So, I did some digging on the internet and I found two lines of script required to fix the connection. It’s a firewall issue, it gets updated while changing the network configuration. Thankfully, Azure Portal offers a console to run shell scripts from the browser itself.

You can find the “RunShellScript” option under Operations > Run Command on the virtual machine’s setting blade. I believe all the cloud providers offer similar connectivity for the VMs using which you can connect to the VM using ssh or RDP for recovery purposes. Please refer to the snapshot below

RunShellScript from Azure portal

After I ran the two above lines, I tried to connect the virtual machine on the ssh and the connection was successful.

Edit: I later discovered that the commands in the browser console above can be run without sudo prefix.

You can later verify the status of the firewall using “sudo ufw status” on the same window.

Microsoft Azure offers a wide range of tools to troubleshoot network errors, check out the documentation for more details

This resolution may apply for other Linux OS as well but I haven’t verified. You may want to read more article Microsoft Azure on DevsDaily.

I welcome any suggestions or feedback to this post, Happy learning!

3 thoughts on “ssh: connect to host x.x.x.x port 22: Connection timed out”

  1. Hello, Sunny. I am dealing with exact same error message in Windows 10. I tried disabling firewall altogether – but it didn’t work. Router port filter list is empty as well (I think this means router isn’t blocking any ports).
    Any suggestions?


Leave a Comment