'Ping not working for ubuntu virtual machines on azure

I created 2 ubuntu virtual machines on azure and I can log into one machine from the other using "ssh [email protected]" but pinging one virtual machine from another gives no response. same thing with curl.

ping xyz.cloudapp.net 
curl xyz.cloudapp.net 

Modifying the /etc/hosts file with the internal ip addresses of the virtual machines doesn't work either.



Solution 1:[1]

You can try hping3 ping host on port number,

sudo apt-get install hping3
root@compare:~# sudo hping3 -S -p 80 google.com
HPING google.com (eth0 216.58.196.174): S set, 40 headers + 0 data bytes
len=44 ip=216.58.196.174 ttl=53 id=20026 sport=80 flags=SA seq=0 win=42900 rtt=32.8 ms
len=44 ip=216.58.196.174 ttl=53 id=34969 sport=80 flags=SA seq=1 win=42900 rtt=36.6 ms
len=44 ip=216.58.196.174 ttl=53 id=14912 sport=80 flags=SA seq=2 win=42900 rtt=32.6 ms
len=44 ip=216.58.196.174 ttl=53 id=60826 sport=80 flags=SA seq=3 win=42900 rtt=32.5 ms
len=44 ip=216.58.196.174 ttl=53 id=5138 sport=80 flags=SA seq=4 win=42900 rtt=32.3 ms
^C
--- google.com hping statistic ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 32.3/33.4/36.6 ms
root@compare:~#

Solution 2:[2]

UPDATE 2021: As of 2021, you can ping to your azure virtual machine by opening the ICMP port.
Just go to Azure Portal > Virtual Machines > Select your Virtual Machine > Networking > Add inbound port rule > Select ICMP under Protocol (customize as per your needs) > Add

Solution 3:[3]

UPDATE 2022
To launch ping as a standard user, either use the count -c option ping -c 4 google.com, or change the file permission of /bin/ping by adding the SUID bit.

On a Azure virtual machine, with UBUNTU 20.04 or 22.04, the ping program has these permissions:
ls -la /bin/ping
-rwxr-xr-x 1 root root 72776 Jan 30 2020 ping*
Therefore, in order to run ping, you must have root privileges.

This means that if you elevate your privileges to root, you can use ping
$ sudo su
# ping google.com
PING google.com (142.251.36.14) 56(84) bytes of data.
64 bytes from ams15s44-in-f14.1e100.net (142.251.36.14): icmp_seq=1 ttl=56 time=2.42 ms
CTRL-C to exit.

Ping from a VM on azure can be done also in two other ways:

  1. using the -c count options, as a normal user
    $ ping -c 4 google.com

  2. By changing the SUID bit of the ping file, then every user will run the ping program with root privileges
    # chmod 4755 /bin/ping
    Then, you can do the ping with normal user privileges
    $ ping google.com

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hrishi
Solution 2 Shubham Sehgal
Solution 3