Network Configuration and Troubleshooting
Category: Advanced Linux Administration
Type: Linux Commands
Generated on: 2025-07-10 03:14:57
For: System Administration, Development & Technical Interviews
Linux Network Configuration & Troubleshooting Cheatsheet (Advanced)
Section titled “Linux Network Configuration & Troubleshooting Cheatsheet (Advanced)”This cheatsheet provides a comprehensive guide to network configuration and troubleshooting on Linux systems. It’s designed for both sysadmins and developers, covering essential commands, practical examples, and advanced techniques.
1. Command Overview
This section lists commands used to configure, monitor, and troubleshoot network interfaces, routing, DNS, and network services.
2. Basic Syntax
This section defines the general structure and common options for each command.
3. Practical Examples
This section showcases real-world usage scenarios with sample commands and expected output.
4. Common Options
This section highlights the most useful flags and parameters for each command.
5. Advanced Usage
This section demonstrates complex examples and command combinations.
6. Tips & Tricks
This section offers pro tips and shortcuts for efficient network management.
7. Troubleshooting
This section addresses common errors and provides solutions.
8. Related Commands
This section lists related commands and alternatives for specific tasks.
I. Network Interface Configuration
1. ip - Network Configuration Utility
-
Command Overview: A powerful tool to manage network interfaces, addresses, routes, and tunnels. Replaces older tools like
ifconfigandroute. -
Basic Syntax:
ip [OPTIONS] OBJECT {COMMAND | help}OBJECT:link,addr,route,tunnel,neigh(ARP)
-
Practical Examples:
Terminal window # Show all network interfacesip link show# Output example:# 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000# link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00# 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000# link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff# Show IP address information for eth0ip addr show eth0# Output example:# 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000# link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff# inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0# valid_lft forever preferred_lft forever# inet6 fe80::a8bb:ccff:fedd:eeff/64 scope link# valid_lft forever preferred_lft forever# Add an IP address to eth0sudo ip addr add 192.168.1.20/24 dev eth0# Delete an IP address from eth0sudo ip addr del 192.168.1.20/24 dev eth0# Bring an interface upsudo ip link set dev eth0 up# Bring an interface downsudo ip link set dev eth0 down -
Common Options:
link show: Show network interfacesaddr show: Show IP addressesroute show: Show routing tablelink set: Modify link attributes (up/down, MTU, etc.)addr add: Add an IP addressaddr del: Delete an IP address
-
Advanced Usage:
Terminal window # Create a virtual ethernet pair (veth)sudo ip link add veth0 type veth peer name veth1# Assign IP addresses to the veth pairsudo ip addr add 10.0.0.1/24 dev veth0sudo ip addr add 10.0.0.2/24 dev veth1# Bring up the veth interfacessudo ip link set dev veth0 upsudo ip link set dev veth1 up -
Tips & Tricks:
- Use
ip -cfor colorized output, making it easier to read. - Use tab completion to explore available options and objects.
- Use
-
Troubleshooting:
RTNETLINK answers: File exists: Address already assigned.RTNETLINK answers: No such device: Interface name incorrect.
-
Related Commands:
ifconfig(deprecated),route(deprecated),netstat(deprecated).ssis better thannetstat.
2. ifconfig - (Deprecated) Network Interface Configuration
-
Command Overview: A legacy command for configuring and displaying network interface information. Largely superseded by
ip. Useipinstead. However, useful for quick information gathering on older systems. -
Basic Syntax:
ifconfig [INTERFACE] [OPTIONS] -
Practical Examples:
Terminal window # Show all network interfacesifconfig -a# Show information for eth0ifconfig eth0# Assign an IP address and netmask to eth0sudo ifconfig eth0 192.168.1.10 netmask 255.255.255.0# Bring an interface upsudo ifconfig eth0 up# Bring an interface downsudo ifconfig eth0 down -
Common Options:
-a: Show all interfaces, including inactive ones.up: Activate the interface.down: Deactivate the interface.
-
Advanced Usage: Less versatile than
ip, advanced usage is limited. -
Tips & Tricks: Use
ipinstead whenever possible. -
Troubleshooting: Often requires
sudofor configuration changes. -
Related Commands:
ip,route,netstat.
3. nmcli - NetworkManager Command-Line Interface
-
Command Overview: A command-line tool for controlling NetworkManager, which manages network connections. Useful for managing connections, devices, and setting up VPNs.
-
Basic Syntax:
nmcli [OPTIONS] OBJECT {COMMAND | help}OBJECT:connection,device,networking
-
Practical Examples:
Terminal window # Show all network connectionsnmcli connection show# Show status of all network devicesnmcli device status# Show details about the eth0 devicenmcli device show eth0# Activate a connection named "MyWiredConnection"nmcli connection up "MyWiredConnection"# Deactivate a connection named "MyWiredConnection"nmcli connection down "MyWiredConnection"# Create a new wired connection (Requires more parameters in real usage)# nmcli connection add con-name "MyNewConnection" type ethernet ifname eth0 ip4 192.168.1.30/24 gw4 192.168.1.1#Modify a connection#nmcli connection modify "MyNewConnection" ipv4.dns "8.8.8.8,8.8.4.4" -
Common Options:
connection show: List network connections.device status: Show device status.connection up: Activate a connection.connection down: Deactivate a connection.connection add: Create a new connection.connection modify: Modify an existing connection.connection delete: Delete an existing connection.
-
Advanced Usage:
Terminal window # Create a VPN connection (requires specific VPN configuration)# nmcli connection add type vpn con-name "MyVPN" ifname "*" vpn.service-type openvpn vpn.data "username=myuser,password=mypass,remote=vpn.example.com"# nmcli connection up MyVPN -
Tips & Tricks:
- Use
nmcli con showto find connection names. - Use
nmcli device show <device>to see the current connection assigned to a device.
- Use
-
Troubleshooting:
- Check NetworkManager logs:
/var/log/syslogorjournalctl -u NetworkManager - Ensure NetworkManager service is running:
systemctl status NetworkManager
- Check NetworkManager logs:
-
Related Commands:
networkctl,systemctl.
II. Routing Configuration
1. ip route - Manage Routing Table
-
Command Overview: Used for displaying and manipulating the routing table.
-
Basic Syntax:
ip route {add | del | show | flush} [OPTIONS] -
Practical Examples:
Terminal window # Show the routing tableip route show# Output example:# default via 192.168.1.1 dev eth0# 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10# Add a route to the 10.0.0.0/24 network via 192.168.1.1sudo ip route add 10.0.0.0/24 via 192.168.1.1# Delete a route to the 10.0.0.0/24 networksudo ip route del 10.0.0.0/24# Add a default routesudo ip route add default via 192.168.1.1 dev eth0# Flush the entire routing table (USE WITH CAUTION!)# sudo ip route flush cache #Flushes the cache only# sudo ip route flush all #Flushes all routes -
Common Options:
show: Display the routing table.add: Add a new route.del: Delete an existing route.default: Specifies the default gateway.via: Specifies the gateway IP address.dev: Specifies the outgoing interface.
-
Advanced Usage:
Terminal window # Add a route with a specific metric (lower metric = higher priority)sudo ip route add 10.0.0.0/24 via 192.168.1.1 metric 100# Add a route for a specific source addresssudo ip route add 10.0.0.0/24 via 192.168.1.1 src 192.168.1.10 -
Tips & Tricks:
- Use
ip route get <destination>to find the route used for a specific destination.
- Use
-
Troubleshooting:
Network is unreachable: No route to the destination.Gateway X.X.X.X is not on the same broadcast domain as device Y: Gateway and interface are on different networks.
-
Related Commands:
route(deprecated),traceroute,ping.
2. route - (Deprecated) Manipulate Routing Table
-
Command Overview: A legacy command for managing the routing table. Largely superseded by
ip route. Useip routeinstead. -
Basic Syntax:
route [OPTIONS] [COMMAND] destination [gw GATEWAY] -
Practical Examples:
Terminal window # Show the routing tableroute -n# Add a route to the 10.0.0.0/24 network via 192.168.1.1sudo route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.1# Delete a route to the 10.0.0.0/24 networksudo route del -net 10.0.0.0 netmask 255.255.255.0# Add a default routesudo route add default gw 192.168.1.1 -
Common Options:
-n: Display numerical addresses instead of hostnames.add: Add a new route.del: Delete an existing route.default: Specifies the default gateway.
-
Advanced Usage: Less versatile than
ip route, advanced usage is limited. -
Tips & Tricks: Use
ip routeinstead whenever possible. -
Troubleshooting: Often requires
sudofor configuration changes. -
Related Commands:
ip route,netstat.
III. DNS Configuration & Troubleshooting
1. dig - DNS Lookup Utility
-
Command Overview: A powerful tool for querying DNS servers and retrieving information about domain names.
-
Basic Syntax:
dig [OPTIONS] name [type] [query options] -
Practical Examples:
Terminal window # Perform a basic DNS lookup for example.comdig example.com# Output example (truncated):# ;; ANSWER SECTION:# example.com. 141 IN A 93.184.216.34# Query a specific DNS serverdig @8.8.8.8 example.com# Query for MX recordsdig example.com MX# Query for NS recordsdig example.com NS# Query for all record types (ANY) - Use with caution, can return a lot of datadig example.com ANY -
Common Options:
@server: Specify a DNS server to query.MX: Query for MX (mail exchange) records.NS: Query for NS (name server) records.A: Query for A (address) records.CNAME: Query for CNAME (canonical name) records.ANY: Query for all record types. Use with caution.+trace: Trace the DNS resolution path.+short: Display only the answer section.
-
Advanced Usage:
Terminal window # Perform a reverse DNS lookupdig -x 8.8.8.8# Batch DNS lookups from a filedig -f domains.txt -
Tips & Tricks:
- Use
dig +trace example.comto trace the DNS resolution process. - Use
dig +short example.comfor a concise output. - Use
dig -t axfr <domain> @<nameserver>for a zone transfer. This is often restricted and should only be used with explicit permission.
- Use
-
Troubleshooting:
connection timed out; no servers could be reached: DNS server unreachable.SERVFAIL: DNS server encountered an error.NXDOMAIN: Domain name does not exist.
-
Related Commands:
nslookup,host,ping,traceroute.
2. nslookup - (Deprecated) DNS Lookup Utility
-
Command Overview: A legacy DNS lookup tool. Largely superseded by
dig.digis preferred. -
Basic Syntax:
nslookup [OPTIONS] [name | -] [server] -
Practical Examples:
Terminal window # Perform a basic DNS lookup for example.comnslookup example.com# Query a specific DNS servernslookup example.com 8.8.8.8# Change to query for MX recordsnslookup> set type=MX> example.com -
Common Options:
server: Specify a DNS server to query.set type=: Set the query type (e.g., MX, NS, A).
-
Advanced Usage: Less versatile than
dig, advanced usage is limited. -
Tips & Tricks: Use
diginstead whenever possible. -
Troubleshooting: Often provides less detailed information than
dig. -
Related Commands:
dig,host,ping,traceroute.
3. host - DNS Lookup Utility
-
Command Overview: A simple DNS lookup utility.
-
Basic Syntax:
host [OPTIONS] name [server] -
Practical Examples:
Terminal window # Perform a basic DNS lookup for example.comhost example.com# Query a specific DNS serverhost example.com 8.8.8.8# Query for MX recordshost -t mx example.com -
Common Options:
-t: Specify the record type (e.g., mx, ns, a).server: Specify a DNS server to query.
-
Advanced Usage: Limited compared to
dig. -
Tips & Tricks: A quick and easy alternative to
digfor basic lookups. -
Troubleshooting: Provides less detailed information than
dig. -
Related Commands:
dig,nslookup,ping,traceroute.
4. /etc/resolv.conf - DNS Resolver Configuration File
-
Command Overview: This file specifies the DNS servers that the system will use for name resolution. Often managed by
NetworkManagerorsystemd-resolved. -
Basic Syntax:
nameserver <IP_ADDRESS>search <domain_list>options <options> -
Practical Examples:
# Example /etc/resolv.confnameserver 8.8.8.8nameserver 8.8.4.4search example.comoptions timeout:1 attempts:2 -
Common Options:
nameserver: Specifies a DNS server IP address.search: Specifies a list of domains to search when resolving unqualified hostnames.options: Specifies resolver options (e.g., timeout, attempts).
-
Advanced Usage:
- Be cautious when manually editing this file, as changes might be overwritten by NetworkManager or systemd-resolved. Use the appropriate management tools instead.
-
Tips & Tricks:
- Use
resolvectl statusto check the status of systemd-resolved. - Use
nmcli dev show <device>to check the DNS servers configured by NetworkManager.
- Use
-
Troubleshooting:
- If DNS resolution is failing, check that the DNS server IP addresses are correct and reachable.
- Check that the
searchdomain list is configured correctly.
-
Related Commands:
dig,nslookup,resolvectl.
IV. Network Monitoring & Troubleshooting
1. ping - Test Network Connectivity
-
Command Overview: A basic tool for testing network connectivity by sending ICMP echo requests to a target host.
-
Basic Syntax:
ping [OPTIONS] destination -
Practical Examples:
Terminal window # Ping example.comping example.com# Ping 8.8.8.8ping 8.8.8.8# Ping with a specific packet sizeping -s 1000 example.com# Ping continuously until interruptedping -t example.com # In some environments, use -i instead of -t for interval -
Common Options:
-c count: Send a specified number of packets.-s size: Specify the packet size.-t ttl: Set the Time To Live. On some systems-ispecifies interval.
-
Advanced Usage:
Terminal window # Ping with a specific source IP addresssudo ping -I eth0 8.8.8.8 # Requires sudo because it may involve raw sockets# Ping with a specific intervalsudo ping -i 0.2 example.com #Requires sudo due to use of short interval -
Tips & Tricks:
- Use
ping -c 4 example.comto send 4 packets and then stop. - High packet loss or long round-trip times indicate network problems.
- Use
-
Troubleshooting:
Destination Host Unreachable: No route to the destination.Request timeout: No response from the destination.
-
Related Commands:
traceroute,mtr,tcpdump.
2. traceroute / tracepath - Trace Route to Destination
-
Command Overview: Traces the route packets take to reach a destination by displaying each hop along the path.
tracepathdoesn’t require root. -
Basic Syntax:
traceroute [OPTIONS] destinationtracepath [OPTIONS] destination -
Practical Examples:
Terminal window # Trace the route to example.comtraceroute example.com# Trace the route to example.com using ICMPtraceroute -I example.com#Tracepath to example.comtracepath example.com -
Common Options:
-I: Use ICMP instead of UDP for tracing.-m max_hops: Set the maximum number of hops.
-
Advanced Usage:
Terminal window # Trace the route with a specific source IP addresssudo traceroute -s 192.168.1.10 example.com# Trace the route using TCP SYN packetstraceroute -T -p 80 example.com -
Tips & Tricks:
traceroutecan help identify where network latency or packet loss is occurring.- If a hop shows asterisks (
* * *), it indicates that the hop is not responding to traceroute probes.
-
Troubleshooting:
- Timeouts at specific hops indicate network problems or firewalls blocking traffic.
- Inconsistent routes can indicate routing issues.
-
Related Commands:
ping,mtr,tcpdump.
3. mtr - Network Diagnostic Tool (Combined Ping & Traceroute)
-
Command Overview: A combination of
pingandtraceroute, providing real-time network statistics for each hop along the route to a destination. -
Basic Syntax:
mtr [OPTIONS] destination -
Practical Examples:
Terminal window # Run mtr to example.commtr example.com# Run mtr in report mode (text-based report)mtr -r -c 10 example.com > report.txt -
Common Options:
-r: Report mode (text-based).-c count: Number of pings to send in report mode.-n: Do not resolve hostnames.-z: Specify initial ping size.
-
Advanced Usage:
Terminal window # Run mtr with a specific interfacemtr -i eth0 example.com -
Tips & Tricks:
mtrprovides a continuous view of network performance, making it easier to identify intermittent issues.- Use report mode (
-r) to generate a text-based report for later analysis.
-
Troubleshooting:
- High packet loss or latency at specific hops indicates network problems.
-
Related Commands:
ping,traceroute,tcpdump.
4. tcpdump - Packet Analyzer
-
Command Overview: A powerful command-line packet analyzer that captures and displays network traffic. Requires root privileges.
-
Basic Syntax:
tcpdump [OPTIONS] [expression] -
Practical Examples:
Terminal window # Capture all traffic on the eth0 interfacesudo tcpdump -i eth0# Capture traffic to or from port 80sudo tcpdump -i eth0 port 80# Capture traffic to a specific hostsudo tcpdump -i eth0 host example.com# Capture traffic to a specific networksudo tcpdump -i eth0 net 192.168.1.0/24# Capture only TCP packetssudo tcpdump -i eth0 tcp# Capture only ICMP packetssudo tcpdump -i eth0 icmp# Write the captured packets to a filesudo tcpdump -i eth0 -w capture.pcap -
Common Options:
-i interface: Specify the interface to capture traffic on.-w file: Write the captured packets to a file (pcap format).-r file: Read packets from a file (pcap format).-n: Do not resolve hostnames or port numbers.-v,-vv,-vvv: Increase verbosity.port: Filter by port number.host: Filter by host address.net: Filter by network address.tcp,udp,icmp: Filter by protocol.
-
Advanced Usage:
Terminal window # Capture packets with a specific TCP flag set (e.g., SYN)sudo tcpdump -i eth0 "tcp[tcpflags] & tcp-syn != 0"# Capture packets larger than a specific sizesudo tcpdump -i eth0 "greater 1000"# Capture packets and save them to a ring buffersudo tcpdump -i eth0 -w capture.pcap -G 60 -W 5 # rotate files every 60 seconds, keep 5 files. -
Tips & Tricks:
- Use filters to narrow down the captured traffic and avoid overwhelming the output.
- Use
-nto avoid DNS lookups, which can slow down the capture process. - Use Wireshark to analyze pcap files graphically.
-
Troubleshooting:
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes: tcpdump is running and capturing traffic.- No output: No traffic matching the specified filter.
tcpdump: pcap_loop: The interface on which the dump was started is no longer up: Interface went down during capture.
-
Related Commands:
tshark(Wireshark command-line tool),wireshark,netstat,ss.
5. ss - Socket Statistics
-
Command Overview: A modern replacement for
netstatthat provides more detailed information about network sockets. Faster and more efficient thannetstat. -
Basic Syntax:
ss [OPTIONS] [FILTER] -
Practical Examples:
Terminal window # Show all TCP socketsss -t -a# Show all listening TCP socketsss -t -l# Show all UDP socketsss -u -a# Show sockets connected to port 80ss -t -a port = 80# Show sockets connected to a specific IP addressss -t -a dst 192.168.1.10#Show the process using the socketss -p#Show summary statisticsss -s -
Common Options:
-t: Show TCP sockets.-u: Show UDP sockets.-a: Show all sockets.-l: Show listening sockets.-p: Show the process using the socket.-n: Do not resolve service names.-s: Show summary statistics.
-
Advanced Usage:
Terminal window # Show TCP sockets in state ESTABLISHEDss -t state established# Show sockets with a specific local address and portss -t -a src 192.168.1.10:22 -
Tips & Tricks:
ssis much faster and more efficient thannetstat.- Use filters to narrow down the output and find specific sockets.
-
Troubleshooting:
Statecolumn indicates the current state of the socket (e.g., ESTABLISHED, LISTEN, CLOSE_WAIT).Recv-QandSend-Qcolumns indicate the number of bytes in the receive and send queues, respectively. High values can indicate network congestion or application issues.
-
Related Commands:
netstat(deprecated),tcpdump,lsof.
6. netstat - (Deprecated) Network Statistics
-
Command Overview: A legacy tool for displaying network connections, routing tables, interface statistics, and masquerade connections. Largely superseded by
ssandip. -
Basic Syntax:
netstat [OPTIONS] -
Practical Examples:
Terminal window # Show all active network connectionsnetstat -a# Show listening portsnetstat -l# Show TCP connectionsnetstat -t# Show UDP connectionsnetstat -u# Show routing tablenetstat -r# Show interface statisticsnetstat -i# Show the program using the socketnetstat -p -
Common Options:
-a: Show all sockets (both listening and non-listening).-l: Show only listening sockets.-t: Show TCP sockets.-u: Show UDP sockets.-r: Show routing table.-i: Show interface statistics.-n: Do not resolve service names.-p: Show the process ID and name associated with each socket.
-
Advanced Usage: Limited compared to
ss. -
Tips & Tricks: Use
ssinstead whenever possible. -
Troubleshooting:
netstatprovides less detailed information and is slower thanss. -
Related Commands:
ss,tcpdump,lsof.
7. lsof - List Open Files
-
Command Overview: Lists all open files and the processes that are using them. Can be used to identify which processes are using specific network ports.
-
Basic Syntax:
lsof [OPTIONS] [file...] -
Practical Examples:
Terminal window # List all open fileslsof# List open files associated with a specific process IDlsof -p 1234# List open files associated with a specific userlsof -u username# List open files associated with a specific network portlsof -i :80# List open files associated with a specific TCP connectionlsof -i TCP:22# List open files associated with a specific UDP portlsof -i UDP:53 -
Common Options:
-p pid: List files opened by process ID.-u user: List files opened by user.-i [protocol][@hostname|hostaddr][:port]: List files opened by network connection.
-
Advanced Usage:
Terminal window # Find the process listening on port 80lsof -i :80# Find all processes listening on any portlsof -i -n -P | grep LISTEN -
Tips & Tricks:
lsofcan be used to identify which processes are preventing a network port from being bound.- Use
-nto avoid hostname lookups, which can speed up the command. - Use
-Pto avoid port name lookups.
-
Troubleshooting:
- No output: No processes are using the specified file or network connection.
- Multiple processes using the same port: A configuration error.
-
Related Commands:
ss,netstat,ps.
V. Firewall Configuration
1. iptables - (Legacy) Firewall Configuration
-
Command Overview: A legacy command-line firewall utility that allows you to configure the Linux kernel’s built-in packet filtering system. Largely superseded by
nftables. Still important to understand. -
Basic Syntax:
iptables [OPTIONS] command [chain] [rule-specification] -
Practical Examples:
Terminal window # List all iptables rulessudo iptables -L# List all iptables rules with verbose outputsudo iptables -L -v# List all iptables rules with numeric output (no DNS lookups)sudo iptables -L -n# Flush all iptables rules (USE WITH CAUTION!)sudo iptables -F# Set the default policy for the INPUT chain to DROPsudo iptables -P INPUT DROP# Allow incoming SSH trafficsudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT# Allow outgoing HTTP trafficsudo iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT# Allow incoming ping requestssudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT# Block traffic from a specific IP addresssudo iptables -A INPUT -s 192.168.1.10 -j DROP# Save iptables rulessudo iptables-save > /etc/iptables/rules.v4 -
Common Options:
-A chain: Append a new rule to the specified chain.-D chain rule-number: Delete a rule from the specified chain by rule number.-D chain rule-specification: Delete a rule from the specified chain by rule specification.-I chain rule-number: Insert a new rule at the specified position in the chain.-R chain rule-number: Replace a rule in the specified chain.-L: List all rules in the specified chain.-F: Flush all rules in the specified chain.-P chain target: Set the default policy for the specified chain.-s source: Specify the source IP address or network.-d destination: Specify the destination IP address or network.- `-