Linux Networking Guide: Master IPs, Routing, DNS & DHCP

Master Linux networking fundamentals with this comprehensive guide covering IP addresses, routing, DNS, and DHCP configuration for beginners.

The Beginner's Guide to Linux Networking: Mastering IPs, Routing, DNS, and DHCP

Linux networking forms the backbone of modern internet infrastructure, powering everything from personal computers to enterprise servers. Whether you're a system administrator, developer, or curious enthusiast, understanding Linux networking fundamentals is essential for troubleshooting connectivity issues, configuring servers, and building robust network solutions.

This comprehensive guide will walk you through the core concepts of Linux networking, including IP addresses, routing, DNS, and DHCP, complete with practical commands and real-world examples. By the end of this article, you'll have the knowledge and tools to confidently manage Linux network configurations.

Understanding IP Addresses in Linux

What Are IP Addresses?

An Internet Protocol (IP) address serves as a unique identifier for devices on a network, similar to how your home address identifies your location. In Linux systems, IP addresses enable communication between different devices, whether they're on the same local network or across the internet.

There are two main versions of IP addresses:

IPv4 (Internet Protocol version 4): Uses 32-bit addresses written in dotted decimal notation (e.g., 192.168.1.100). IPv4 addresses are divided into four octets, each ranging from 0 to 255.

IPv6 (Internet Protocol version 6): Uses 128-bit addresses written in hexadecimal notation (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334). IPv6 was developed to address the shortage of IPv4 addresses.

IP Address Classes and Subnets

IPv4 addresses are organized into different classes:

- Class A: 1.0.0.0 to 126.255.255.255 (Large networks) - Class B: 128.0.0.0 to 191.255.255.255 (Medium networks) - Class C: 192.0.0.0 to 223.255.255.255 (Small networks)

Private IP ranges reserved for internal networks: - Class A Private: 10.0.0.0 to 10.255.255.255 - Class B Private: 172.16.0.0 to 172.31.255.255 - Class C Private: 192.168.0.0 to 192.168.255.255

Essential IP Commands in Linux

#### Viewing Network Interfaces and IP Addresses

The ip command is the modern standard for network configuration in Linux:

`bash

Display all network interfaces

ip addr show

Show specific interface (e.g., eth0)

ip addr show eth0

Display only IPv4 addresses

ip -4 addr show

Display only IPv6 addresses

ip -6 addr show `

For systems still using older tools:

`bash

Legacy command (deprecated but still available)

ifconfig

Show specific interface

ifconfig eth0 `

#### Configuring IP Addresses

Temporary IP Configuration (lost after reboot):

`bash

Add IP address to interface

sudo ip addr add 192.168.1.100/24 dev eth0

Remove IP address from interface

sudo ip addr del 192.168.1.100/24 dev eth0

Bring interface up

sudo ip link set eth0 up

Bring interface down

sudo ip link set eth0 down `

Permanent IP Configuration varies by Linux distribution:

Ubuntu/Debian (using Netplan):

`yaml

/etc/netplan/01-network-manager-all.yaml

network: version: 2 renderer: networkd ethernets: eth0: addresses: - 192.168.1.100/24 gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 8.8.4.4] `

Apply configuration: `bash sudo netplan apply `

CentOS/RHEL/Fedora:

`bash

/etc/sysconfig/network-scripts/ifcfg-eth0

TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no NAME=eth0 DEVICE=eth0 ONBOOT=yes IPADDR=192.168.1.100 PREFIX=24 GATEWAY=192.168.1.1 DNS1=8.8.8.8 DNS2=8.8.4.4 `

Restart networking: `bash sudo systemctl restart network `

Linux Routing Fundamentals

Understanding Routing

Routing determines the path network packets take from source to destination. Linux systems maintain a routing table that specifies which interface and gateway to use for different network destinations.

Viewing the Routing Table

`bash

Modern command to view routing table

ip route show

Show only IPv4 routes

ip -4 route show

Show only IPv6 routes

ip -6 route show

Legacy command (still functional)

route -n

Another useful command

netstat -rn `

Sample routing table output: ` default via 192.168.1.1 dev eth0 proto dhcp metric 100 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100 metric 100 `

Understanding Routing Table Entries

- default: Default route (0.0.0.0/0) - where packets go when no specific route matches - via: Gateway IP address - dev: Network interface - proto: Protocol that installed the route (kernel, dhcp, static) - scope: Route scope (link, global) - src: Source IP address for outgoing packets - metric: Route priority (lower values preferred)

Adding and Removing Routes

#### Temporary Routes (lost after reboot):

`bash

Add default gateway

sudo ip route add default via 192.168.1.1

Add route to specific network

sudo ip route add 10.0.0.0/8 via 192.168.1.254 dev eth0

Add route through specific interface

sudo ip route add 172.16.0.0/16 dev eth1

Delete route

sudo ip route del 10.0.0.0/8 via 192.168.1.254

Delete default route

sudo ip route del default via 192.168.1.1 `

#### Permanent Routes

Ubuntu/Debian (using Netplan): `yaml network: version: 2 ethernets: eth0: addresses: [192.168.1.100/24] gateway4: 192.168.1.1 routes: - to: 10.0.0.0/8 via: 192.168.1.254 - to: 172.16.0.0/16 via: 192.168.1.253 `

CentOS/RHEL - create route files: `bash

/etc/sysconfig/network-scripts/route-eth0

10.0.0.0/8 via 192.168.1.254 dev eth0 172.16.0.0/16 via 192.168.1.253 dev eth0 `

Advanced Routing Commands

`bash

Show route to specific destination

ip route get 8.8.8.8

Show routing cache

ip route show cache

Flush routing cache

sudo ip route flush cache

Show routing table for specific table

ip route show table main `

DNS (Domain Name System) Configuration

Understanding DNS

DNS translates human-readable domain names (like google.com) into IP addresses (like 142.250.191.14) that computers use to communicate. Linux systems use various files and services to manage DNS resolution.

DNS Configuration Files

#### /etc/resolv.conf

This file contains DNS server information:

`bash

View current DNS configuration

cat /etc/resolv.conf `

Sample content: ` nameserver 8.8.8.8 nameserver 8.8.4.4 search example.com domain example.com options timeout:2 attempts:3 `

Configuration options: - nameserver: DNS server IP addresses (up to 3) - search: Domain search list - domain: Local domain name - options: Various resolver options

#### /etc/hosts

Local hostname-to-IP mappings:

`bash

View hosts file

cat /etc/hosts `

Sample content: ` 127.0.0.1 localhost 127.0.1.1 mycomputer 192.168.1.10 server1.example.com server1 192.168.1.11 server2.example.com server2 `

#### /etc/nsswitch.conf

Controls the order of name resolution methods:

`bash

View name service switch configuration

cat /etc/nsswitch.conf | grep hosts `

Common configuration: ` hosts: files dns `

This means check /etc/hosts first, then DNS servers.

DNS Testing Commands

#### Basic DNS Lookup Commands

`bash

Basic hostname resolution

nslookup google.com

More detailed DNS information

dig google.com

Reverse DNS lookup

dig -x 8.8.8.8

Query specific record types

dig google.com MX # Mail exchange records dig google.com NS # Name server records dig google.com TXT # Text records dig google.com AAAA # IPv6 addresses

Use specific DNS server

dig @8.8.8.8 google.com

Short output format

dig +short google.com `

#### Advanced DNS Troubleshooting

`bash

Trace DNS resolution path

dig +trace google.com

Show all DNS record types

dig google.com ANY

Query with TCP instead of UDP

dig +tcp google.com

Show query time and server used

dig google.com +stats

Continuous DNS monitoring

watch -n 1 'dig +short google.com' `

#### Host Command

`bash

Simple hostname lookup

host google.com

Reverse lookup

host 8.8.8.8

Query specific record type

host -t MX google.com host -t NS google.com `

Configuring DNS Servers

#### Temporary DNS Configuration

`bash

Edit resolv.conf directly (temporary)

sudo nano /etc/resolv.conf `

Add nameservers: ` nameserver 8.8.8.8 nameserver 1.1.1.1 `

#### Permanent DNS Configuration

Ubuntu/Debian (systemd-resolved):

`bash

Check current DNS settings

systemd-resolve --status

Configure via Netplan

sudo nano /etc/netplan/01-network-manager-all.yaml `

`yaml network: version: 2 ethernets: eth0: addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 1.1.1.1] search: [example.com] `

CentOS/RHEL:

`bash

Edit network interface configuration

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0 `

Add DNS entries: ` DNS1=8.8.8.8 DNS2=1.1.1.1 DOMAIN=example.com `

DHCP (Dynamic Host Configuration Protocol)

Understanding DHCP

DHCP automatically assigns IP addresses, subnet masks, gateways, and DNS servers to network devices. This eliminates the need for manual IP configuration and prevents IP address conflicts.

DHCP Client Configuration

#### Viewing DHCP Information

`bash

Show DHCP lease information

cat /var/lib/dhcp/dhclient.leases

For NetworkManager systems

nmcli connection show

Show DHCP client status

sudo systemctl status dhclient `

#### Configuring DHCP Client

Ubuntu/Debian (Netplan): `yaml network: version: 2 ethernets: eth0: dhcp4: true dhcp6: true `

CentOS/RHEL: `bash

/etc/sysconfig/network-scripts/ifcfg-eth0

BOOTPROTO=dhcp ONBOOT=yes `

#### Manual DHCP Operations

`bash

Request new DHCP lease

sudo dhclient eth0

Release current DHCP lease

sudo dhclient -r eth0

Renew DHCP lease

sudo dhclient -r eth0 && sudo dhclient eth0

Use specific DHCP server

sudo dhclient -s 192.168.1.1 eth0 `

DHCP Server Configuration

#### Installing DHCP Server

Ubuntu/Debian: `bash sudo apt update sudo apt install isc-dhcp-server `

CentOS/RHEL: `bash sudo yum install dhcp

or for newer versions

sudo dnf install dhcp-server `

#### Basic DHCP Server Configuration

Edit the main configuration file: `bash sudo nano /etc/dhcp/dhcpd.conf `

Sample configuration: `

Global settings

default-lease-time 600; max-lease-time 7200; authoritative;

Subnet configuration

subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.100 192.168.1.200; option routers 192.168.1.1; option domain-name-servers 8.8.8.8, 8.8.4.4; option domain-name "example.com"; option broadcast-address 192.168.1.255; }

Static IP reservation

host server1 { hardware ethernet 00:11:22:33:44:55; fixed-address 192.168.1.10; } `

#### Starting and Managing DHCP Server

`bash

Start DHCP server

sudo systemctl start dhcpd

Enable DHCP server at boot

sudo systemctl enable dhcpd

Check DHCP server status

sudo systemctl status dhcpd

View DHCP server logs

sudo journalctl -u dhcpd -f

Restart DHCP server

sudo systemctl restart dhcpd `

DHCP Troubleshooting Commands

`bash

Monitor DHCP traffic

sudo tcpdump -i eth0 port 67 or port 68

Check DHCP lease database

cat /var/lib/dhcpd/dhcpd.leases

Test DHCP server response

sudo nmap --script broadcast-dhcp-discover

Validate DHCP configuration

sudo dhcpd -t -cf /etc/dhcp/dhcpd.conf `

Network Troubleshooting Commands

Connectivity Testing

`bash

Test basic connectivity

ping google.com ping -c 4 8.8.8.8

Test IPv6 connectivity

ping6 google.com

Continuous ping

ping -i 1 google.com

Ping with timestamp

ping google.com | while read pong; do echo "$(date): $pong"; done `

Port and Service Testing

`bash

Test specific port connectivity

telnet google.com 80 nc -zv google.com 80

Scan for open ports

nmap -p 1-1000 192.168.1.1

Test UDP port

nc -u -zv 8.8.8.8 53 `

Network Interface Statistics

`bash

Show network interface statistics

ip -s link show

Continuous network statistics

watch -n 1 'cat /proc/net/dev'

Show network connections

netstat -tuln ss -tuln

Show active connections

netstat -tupln ss -tupln `

Advanced Network Analysis

`bash

Capture network traffic

sudo tcpdump -i eth0

Capture specific traffic

sudo tcpdump -i eth0 host google.com sudo tcpdump -i eth0 port 80

Show ARP table

arp -a ip neigh show

Trace network route

traceroute google.com mtr google.com `

Network Configuration Best Practices

Security Considerations

1. Use strong DNS servers: Consider using secure DNS providers like Cloudflare (1.1.1.1) or Quad9 (9.9.9.9) 2. Implement proper firewall rules: Use iptables or firewalld to control network access 3. Regular monitoring: Monitor network interfaces and connections for unusual activity 4. Secure DHCP: Implement DHCP snooping and MAC address filtering when possible

Performance Optimization

`bash

Adjust network buffer sizes

echo 'net.core.rmem_max = 16777216' >> /etc/sysctl.conf echo 'net.core.wmem_max = 16777216' >> /etc/sysctl.conf

Apply sysctl changes

sudo sysctl -p

Monitor network performance

iftop nethogs iotop `

Documentation and Monitoring

1. Document network configurations: Keep records of IP assignments, routes, and DNS settings 2. Monitor DHCP lease usage: Ensure adequate IP address pools 3. Regular connectivity testing: Implement automated network health checks 4. Log analysis: Regularly review network-related logs for issues

Conclusion

Mastering Linux networking fundamentals—IP addresses, routing, DNS, and DHCP—is essential for anyone working with Linux systems. This guide has provided you with the theoretical knowledge and practical commands needed to configure, troubleshoot, and maintain Linux network configurations effectively.

Remember that networking is a hands-on skill that improves with practice. Start by experimenting with these commands in a safe environment, such as virtual machines or test networks. As you become more comfortable with basic operations, explore advanced topics like network namespaces, advanced routing protocols, and network security.

The commands and concepts covered in this guide form the foundation for more advanced networking topics. Whether you're managing a single server or a complex network infrastructure, these fundamentals will serve you well in your Linux networking journey.

Keep practicing, stay curious, and don't hesitate to consult the manual pages (man command) for detailed information about specific commands and their options. The Linux networking ecosystem is vast and powerful, and with these tools at your disposal, you're well-equipped to tackle any networking challenge that comes your way.

Tags

  • DHCP
  • DNS
  • Linux
  • networking
  • system-administration

Related Articles

Popular Technical Articles & Tutorials

Explore our comprehensive collection of technical articles, programming tutorials, and IT guides written by industry experts:

Browse all 8+ technical articles | Read our IT blog

Linux Networking Guide: Master IPs, Routing, DNS & DHCP