Network Management Commands
Introduction
Managing network configurations and statuses is crucial for system administrators to ensure connectivity and troubleshoot issues. In this tutorial, we will explore various network management commands available on Linux systems. Each command will be explained with examples to provide a clear understanding of their usage.
1. ifconfig
The ifconfig command is used to configure network interfaces. It allows you to view and change the IP address, netmask, and other network parameters.
Example: Display all network interfaces
ifconfig
eth0 Link encap:Ethernet HWaddr 00:0c:29:68:8d:41 inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe68:8d41/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1294 errors:0 dropped:0 overruns:0 frame:0 TX packets:234 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:176487 (176.4 KB) TX bytes:35234 (35.2 KB)
To assign an IP address to a network interface:
ifconfig eth0 192.168.1.10
2. ip
The ip command is a powerful tool for network management, replacing many functions of the older ifconfig command.
Example: Display IP addresses
ip addr show
1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:68:8d:41 brd ff:ff:ff:ff:ff:ff inet 192.168.1.2/24 brd 192.168.1.255 scope global dynamic eth0 valid_lft 86310sec preferred_lft 86310sec inet6 fe80::20c:29ff:fe68:8d41/64 scope link valid_lft forever preferred_lft forever
To add an IP address to an interface:
ip addr add 192.168.1.10/24 dev eth0
3. netstat
The netstat command displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
Example: Display all active connections
netstat -a
Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 192.168.1.2:ssh 192.168.1.3:54022 ESTABLISHED tcp 0 0 127.0.0.1:ipp 0.0.0.0:* LISTEN tcp6 0 0 [::]:ssh [::]:* LISTEN udp 0 0 0.0.0.0:bootpc 0.0.0.0:* udp6 0 0 [::]:dhcpv6-client [::]:*
4. ping
The ping command sends ICMP ECHO_REQUEST packets to network hosts. It helps determine if a particular host is reachable across a network.
Example: Ping a host
ping google.com
PING google.com (172.217.16.174) 56(84) bytes of data. 64 bytes from lhr25s10-in-f14.1e100.net (172.217.16.174): icmp_seq=1 ttl=54 time=9.85 ms 64 bytes from lhr25s10-in-f14.1e100.net (172.217.16.174): icmp_seq=2 ttl=54 time=10.2 ms 64 bytes from lhr25s10-in-f14.1e100.net (172.217.16.174): icmp_seq=3 ttl=54 time=9.94 ms 64 bytes from lhr25s10-in-f14.1e100.net (172.217.16.174): icmp_seq=4 ttl=54 time=10.0 ms --- google.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3003ms rtt min/avg/max/mdev = 9.850/10.010/10.240/0.152 ms
5. traceroute
The traceroute command shows the route taken by packets to reach a network host. It helps in diagnosing network routing issues.
Example: Trace the route to a host
traceroute google.com
traceroute to google.com (172.217.16.174), 30 hops max, 60 byte packets 1 _gateway (192.168.1.1) 0.382 ms 0.451 ms 0.509 ms 2 192.168.0.1 (192.168.0.1) 1.316 ms 1.410 ms 1.503 ms 3 10.10.10.1 (10.10.10.1) 2.026 ms 2.116 ms 2.181 ms 4 100.64.1.1 (100.64.1.1) 3.734 ms 3.836 ms 3.901 ms 5 172.217.16.174 (172.217.16.174) 4.216 ms 4.341 ms 4.421 ms
6. nslookup
The nslookup command queries the DNS to obtain domain name or IP address mapping information.
Example: Look up the IP address for a domain
nslookup google.com
Server: 8.8.8.8 Address: 8.8.8.8#53 Non-authoritative answer: Name: google.com Address: 172.217.16.174
7. dig
The dig command is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers returned by the nameserver.
Example: Query DNS information for a domain
dig google.com
; <<>> DiG 9.16.1-Ubuntu <<>> google.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57235 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; QUESTION SECTION: ;google.com. IN A ;; ANSWER SECTION: google.com. 299 IN A 172.217.16.174 ;; Query time: 19 msec ;; SERVER: 127.0.0.53#53(127.0.0.53) ;; WHEN: Mon Mar 08 12:10:38 UTC 2021 ;; MSG SIZE rcvd: 55
8. route
The route command displays and modifies the IP routing table. It is used to set up static routes to specific hosts or networks via an interface.
Example: Display the routing table
route -n
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
To add a static route:
sudo route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1
Conclusion
In this tutorial, we have covered several essential network management commands used in Linux. These commands are vital tools for network configuration, troubleshooting, and maintenance. By mastering them, system administrators can effectively manage and diagnose network-related issues.