ping / nslookup / dig
ping, nslookup, and dig are commands used for network diagnostics and name resolution. Use them to verify connectivity, investigate DNS records, and troubleshoot network issues.
Syntax
Check connectivity (press Ctrl+C to stop).
ping hostname_or_ip
Send ping a specified number of times.
ping -c count hostname
Look up the IP address of a domain (nslookup).
nslookup domain
Look up DNS records in detail (dig).
dig domain
Look up a specific record type.
dig domain record_type
Query a specific DNS server.
dig @dns_server domain
Commands and Options
| Command / Option | Description |
|---|---|
| ping host | Sends ICMP packets to check connectivity. |
| ping -c count | Specifies the number of packets to send. |
| ping -i seconds | Specifies the interval between packets in seconds. |
| ping -t TTL | Specifies the TTL value. |
| nslookup domain | Looks up the A record (IP address) for a domain. |
| nslookup -type=MX domain | Looks up the MX record (mail server) for a domain. |
| dig domain | Looks up DNS records in detail (defaults to A record). |
| dig domain MX | Looks up the MX record for a domain. |
| dig domain NS | Looks up the NS record (name servers) for a domain. |
| dig domain TXT | Looks up TXT records (SPF, DKIM, etc.) for a domain. |
| dig +short domain | Outputs only the IP address in a concise format. |
| dig @8.8.8.8 domain | Queries Google's DNS server. |
| ss -tuln | Displays open ports and listening services. |
| netstat -tuln | Displays a list of ports, similar to ss (for older systems). |
Sample Code
Send ping exactly 5 times and check response times.
ping -c 5 google.com PING google.com (142.250.196.46): 56 data bytes 64 bytes from 142.250.196.46: icmp_seq=0 ttl=118 time=5.123 ms 64 bytes from 142.250.196.46: icmp_seq=1 ttl=118 time=4.987 ms 64 bytes from 142.250.196.46: icmp_seq=2 ttl=118 time=5.234 ms 64 bytes from 142.250.196.46: icmp_seq=3 ttl=118 time=5.001 ms 64 bytes from 142.250.196.46: icmp_seq=4 ttl=118 time=4.876 ms --- google.com ping statistics --- 5 packets transmitted, 5 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 4.876/5.044/5.234/0.122 ms
Use ping in a script to monitor server availability. The -c 1 flag sends only one packet, and -W 2 sets a 2-second timeout.
health_check.sh
if ping -c 1 -W 2 192.168.1.1 > /dev/null 2>&1; then
echo "Server is running"
else
echo "Server is not responding"
fi
bash health_check.sh
Server is running
You can also enter an if statement directly in the terminal. After pressing Enter following then, a > prompt appears to indicate more input is expected. Enter fi to execute.
if ping -c 1 -W 2 192.168.1.1 > /dev/null 2>&1; then
echo "Server is running"
else
echo "Server is not responding"
fi
Server is running
Retrieve only the A record (IP address) for a domain in a simple format.
dig +short example.com 93.184.216.34
Check the MX record (mail server configuration).
dig example.com MX +short 10 mail.example.com. 20 mail2.example.com.
Check the TXT record (SPF configuration).
dig example.com TXT +short "v=spf1 include:_spf.google.com ~all"
Resolve a domain using Google DNS (8.8.8.8). Useful when you want to bypass your ISP's DNS.
dig @8.8.8.8 example.com +short 93.184.216.34
Perform a reverse lookup to find the domain name for an IP address.
dig -x 93.184.216.34 +short example.com.
Check the MX record using nslookup.
nslookup -type=MX gmail.com Server: 8.8.8.8 Address: 8.8.8.8#53 Non-authoritative answer: gmail.com mail exchanger = 5 gmail-smtp-in.l.google.com. gmail.com mail exchanger = 10 alt1.gmail-smtp-in.l.google.com.
Check open ports on the system.
ss -tuln Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:* tcp LISTEN 0 128 0.0.0.0:443 0.0.0.0:*
Notes
When using ping in scripts for server health monitoring, the standard approach is to combine -c 1 (send only once) with -W 2 (2-second timeout) for fast response detection.
For DNS investigation, use dig +short domain for a quick IP lookup, and dig (without options) for full details. nslookup is an older tool but remains widely used because its interactive mode is easy to work with.
If you find any errors or copyright issues, please contact us.