6 essential network commands in Linux
This Linux tutorial covers some basic network commands that can be useful in troubleshooting network problems with other servers both on the network and over the Internet, get more information about other servers. 1. Command ping sends echo ICMP requests to the server, which you specify on the command line, and is used to quickly check the network connection to another server. If packets are accepted, the destination device sends the packets back:
# ping 192.168.1.2 PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data. 64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.141 ms 64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.136 ms 64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.109 ms
Please note that ping is not a reliable way to check network connectivity, as many servers block ICMP echo packets by default, so if a server does not respond to pings, this does not mean that it is or is not available. 2. Team netstat (Network Statistics) displays network connections to and from the server, routing tables, network interface statistics, masked connections, etc.
netstat -tunlp | less Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 15501/sshd tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29709/httpd
- -t List all TCP ports;
- -u List of all UDP ports;
- -n Display numeric addresses instead of trying to determine a symbolic host;
- -l Show only sockets;
- -p Show the PID and name of the program that owns each socket.
3. Traceroute is a network troubleshooting command that will show the route of the packet to the destination server (number of flights) and the response time to get to the destination server.
# traceroute 192.168.1.2 traceroute to 192.168.1.2 (192.168.1.2), 30 hops max, 60 byte packets 1 192.168.1.2 (192.168.1.2) 0.471 ms 0.401 ms 0.402 ms
4. Team hostname shows the hostname of the server, and it is also used to set (or change) the name of the proxy server:
# hostname test.ru
To set a new hostname for the server to use:
#sudo hostname new-hostname.ru
# hostname new-hostname.ru
5. Team route is a network utility used to display or modify the routing table.
# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gw.msk.ispsyste 0.0.0.0 UG 100 0 0 eth0 213.159.208.0 0.0.0.0 255.255.254.0 U 100 0 0 eth0
To add a new route in use:
# route add -net 172.0.0.0 netmask 255.255.255.0 dev eth0
To remove a route usage:
# route del -net 172.0.0.0 netmask 255.255.255.0 dev eth0
To remove the default gateway and add a new gateway as default use:
# route delete default gw 192.168.1.1 eth0
# route add default gw 192.168.10.254 eth0
6. dig (Groper Domain Information) is a useful tool for network troubleshooting, primarily used to query DNS information related like CNAME, NS, MX Records, etc.
# dig google.com ; <<>> DiG 9.9.4-RedHat-9.9.4-38.el7_3.2 <<>> google.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 5123 ;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;google.com. IN A ;; ANSWER SECTION: google.com. 94 IN A 173.194.222.113 google.com. 94 IN A 173.194.222.100 google.com. 94 IN A 173.194.222.102 google.com. 94 IN A 173.194.222.101 google.com. 94 IN A 173.194.222.138 google.com. 94 IN A 173.194.222.139 ;; Query time: 0 msec ;; SERVER: 188.120.247.2#53(188.120.247.2) ;; WHEN: Mon Mar 06 21:22:06 MSK 2017 ;; MSG SIZE rcvd: 135