Network and DNS in Linux: ping and dig

Updated November 1, 2026

ping checks whether packets reach a host, and dig looks up an IP address from a domain name.

Common cases

ping example.com            # do packets get through, and how fast
ping -c 4 example.com       # exactly 4 packets, then stop
dig example.com             # the IP for a name
dig +short example.com      # just the address, nothing else
dig MX example.com          # the domain's mail servers
cat /etc/resolv.conf        # which DNS server is used

Sample output

$ ping -c 3 example.com
PING example.com (172.66.147.243) 56(84) bytes of data.
64 bytes from 172.66.147.243: icmp_seq=1 ttl=55 time=159 ms
64 bytes from 172.66.147.243: icmp_seq=2 ttl=55 time=281 ms
64 bytes from 172.66.147.243: icmp_seq=3 ttl=55 time=153 ms

--- example.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 152.577/197.721/281.414/59.240 ms
$ dig +short example.com
172.66.147.243
104.20.23.154

How it works: name, address and reachability

These are two different questions. dig answers 'what address does this name have': it asks a DNS server (its address is in /etc/resolv.conf) and returns an IP. ping answers 'do packets reach this address': it sends short ICMP packets and waits for a reply. So if ping by name fails, check dig first. Maybe the name does not resolve, and the link is fine. For debugging you can hard-code names in /etc/hosts, bypassing DNS.

Common mistakes

A silent ping proves nothing. ping -c 3 microsoft.com answers 3 packets transmitted, 0 received, 100% packet loss and exits 1, while the site opens fine in a browser: the host and its load balancer drop ICMP. A ping that works proves packets get through; a ping that fails proves nothing. Check a service by its port instead, say curl -I https://microsoft.com.

For a name that does not exist, dig +short prints nothing and exits 0. dig +short nosuchhost.example.com; echo $? gives an empty line and 0. The missing name shows up only in the full output: ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN. So in a script you test the string, not the exit code: [ -n "$(dig +short "$host")" ].

dig does not look at /etc/hosts, but programs do. Add the line 10.0.0.7 db.internal.example.com to /etc/hosts: ping db.internal.example.com goes to 10.0.0.7, while dig +short db.internal.example.com says nothing and the full answer reports NXDOMAIN. dig asks the DNS server directly; programs go through the resolver library and the order set in /etc/nsswitch.conf. To ask the way a program asks, use getent hosts db.internal.example.com.

In the book

This topic is covered in full in the chapter Networking basics.

See also

ip addr and ip link, Sockets: ss, curl and wget.