
There are a variety of commands that can be used to test connectivity to another computer in Linux:
- ping
- ping6
- traceroute
- traceroute6
- tracepath
- tracepath6
- netcat
- nc
PING
The ping command tests connectivity using ICMP and IPv4.
[root@server1 ~]# ping -c4 www.example.com
PING www.example.com (93.184.216.34) 56(84) bytes of data.
64 bytes from 93.184.216.34: icmp_seq=1 ttl=57 time=60.3 ms
64 bytes from 93.184.216.34: icmp_seq=2 ttl=57 time=20.7 ms
64 bytes from 93.184.216.34: icmp_seq=3 ttl=57 time=76.0 ms
64 bytes from 93.184.216.34: icmp_seq=4 ttl=57 time=56.6 ms
--- www.example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 4054ms
rtt min/avg/max/mdev = 20.728/53.465/76.093/20.261 ms
The ping6 command tests connectivity using ICMP and IPv6.
[root@server1 ~]# ping6 -c4 ipv6.example.com
PING www.example.com (2606:2800:220:1:248:1893:25c8:1946) 56 bytes of data.
From FS1 icmp_seq=1 ttl=57 time=60.3 ms
From FS1 icmp_seq=2 ttl=57 time=20.7 ms
From FS1 icmp_seq=3 ttl=57 time=76.0 ms
From FS1 icmp_seq=4 ttl=57 time=56.6 ms
--- www.example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 4054ms
rtt min/avg/max/mdev = 20.728/53.465/76.093/20.261 ms
TRACEROUTE
The traceroute command is used to view the hops a packet takes from the source device to the destination device. A hop is the transmission of a packet from router "a" to router "b" to router "c" and so on. The hop count is not incremented when a packet traverses a switch or end-point. Also, a hop is not counted when the packet enters the first router in the path. Sometimes this is referred to as hop 0. A hop is counted when a packet exits a router.
The traceroute command lists hop 0 as "1", hop 1 as "2", and so on.
[root@server1 ~]# traceroute www.example.com
traceroute to www.example.com (93.184.216.34), 30 hops max, 60 byte packets
1 r1.local.com (192.168.0.1) 0.319 ms 0.362 ms 0.411 ms
2 142.254.152.173 (142.254.152.173) 14.738 ms 14.739 ms 14.704 ms
3 ae62.applwibp02h.midwest.rr.com (24.164.240.217) 32.846 ms 32.832 ms 32.815 ms
4 93.184.216.34 (93.184.216.34) 21.621 ms 19.627 ms 27.608 ms
If the traceroute command is unable to complete the route from source device to destination device, the traceroute command will display wildards. This means that some device in the path was unable to accept the packet. The device that was unable to accept the packet could be a router, switch, or end point. The traceroute command can confirm that a packet cannot be routed to the destination computer. However, the traceroute command cannot identify why a packet cannot be routed to the next hop.
Note: A router will drop a traceroute packet when the time to live (TTL) value reaches zero.
[root@server1 ~]# traceroute www.bogus.com
traceroute to www.example.com (93.184.216.34), 30 hops max, 60 byte packets
1 r1.local.com (192.168.0.1) 0.319 ms 0.362 ms 0.411 ms
2 142.254.152.173 (142.254.152.173) 14.738 ms 14.739 ms 14.704 ms
3 ae62.applwibp02h.midwest.rr.com (24.164.240.217) 32.846 ms 32.832 ms 32.815 ms
4 * * *
5 * * *
6 * * *
7 * * *
8 * * *
The traceroute6 command is used to trace the hops to an IPv6 target.
[root@server1 ~]# traceroute6 ipv6.example.com
traceroute to ipv6.example.com (2606:2800:220:1:248:1893:25c8:1946), 30 hops max, 80 byte packets
1 2001:560:a1:18:bca1:13ff:fe77:7f38 (2001:560:a1:18:bca1:13ff:fe77:7f38) 0.378 ms 0.400 ms 0.510 ms
2 2001:470:66:84::1 (2001:470:66:84::1) 30.653 ms 31.679 ms 33.852 ms
3 gige-g5-1.core1.fmt1.he.net (2001:470:0:206::1) 61.420 ms 61.954 ms 62.452 ms
4 ipv6.example.com (2606:2800:220:1:248:1893:25c8:1946) 40.381 ms 42.268 ms 43.335 ms
TRACEPATH
The tracepath command is very similar to the traceroute command.
[root@server1 ~]# tracepath www.example.com
1?: [LOCALHOST] pmtu 1500
1: r1.local.com 0.609ms
2: 142.254.152.73 12.102ms
3: ae62.applwibp02h.midwest.rr.com 23.540ms
4: 93.184.216.34 27.608ms
If the tracepath command is unable to complete the route from source device to destination device, the traceroute command will display no reply.
[root@server1 ~]# tracepath www.example.com
1?: [LOCALHOST] pmtu 1500
1: r1.local.com 0.609ms
2: 142.254.152.73 12.102ms
3: ae62.applwibp02h.midwest.rr.com 23.540ms
4: no reply
5: no reply
6: no reply
7: no reply
8: no reply
. . .
The tracepath6 command is used to trace the hops to an IPv6 target.
[root@server1 ~]# tracepath6 ipv6.example.com
tracepath to ipv6.example.com (2606:2800:220:1:248:1893:25c8:1946), 30 hops max, 80 byte packets
1 2001:560:a1:18:bca1:13ff:fe77:7f38 (2001:560:a1:18:bca1:13ff:fe77:7f38) 0.378 ms 0.400 ms 0.510 ms
2 2001:470:66:84::1 (2001:470:66:84::1) 30.653 ms 31.679 ms 33.852 ms
3 gige-g5-1.core1.fmt1.he.net (2001:470:0:206::1) 61.420 ms 61.954 ms 62.452 ms
4 ipv6.example.com (2606:2800:220:1:248:1893:25c8:1946) 40.381 ms 42.268 ms 43.335 ms
NETCAT
The nc (netcat) command can be used to connect to a remote server on a certain port. For example, to connect to an SMTP server:
[root@server1 ~]# nc mx1.example.com 25
220 mx1.example.com ESMTP Postfix
. . .
By default, netcat uses TCP. The -u option can be used to connect using UDP.
[root@server1 ~]# nc -u dns1.example.com 53
. . .
The -l (listen) option can be used to listen on a certain port. For example, to configure server1 to listen on port 1234. Then, server2 can connect to server1 on port 1234.
[root@server1 ~]# nc -l 1234
Redirection can be used to redirect input and output to a file, to effectively enable file transfer. On server1, redirect output to a file.
[root@server1 ~]# nc -l 1234 > example.log
On server2, send the content of local.log to server1.
[root@server2 ~]# nc server1.example.com 1234 < local.log
Did you find this article helpful?
If so, consider buying me a coffee over at