Linux Network Configuration

Deeply understand Linux network interface configuration, IP address management, routing configuration, and network service management

Back to Tutorial List

1. Network Configuration Overview

Network configuration is an important part of Linux system administration, which involves network interface configuration, IP address allocation, routing table settings, DNS resolution configuration, etc. Correct network configuration is crucial for system network connectivity, service access, and communication.

1.1 Linux Network Configuration Layers

  • Physical Layer: Network Interface Card (NIC), network cables, switches and other physical devices.
  • Data Link Layer: MAC address, Ethernet protocol, etc.
  • Network Layer: IP address, routing table, ICMP protocol, etc.
  • Transport Layer: TCP, UDP protocols, etc.
  • Application Layer: HTTP, SSH, FTP and other application protocols.

1.2 Linux Network Configuration Tools

  • ifconfig: Traditional network interface configuration command.
  • ip: Modern network configuration command with more powerful functions.
  • route: Traditional routing configuration command.
  • netstat: Network status viewing command.
  • ss: Modern network status viewing command, replacing netstat.
  • ping: Network connectivity testing command.
  • traceroute: Network path tracing command.
  • nslookup: DNS query command.
  • dig: More powerful DNS query command.
  • hostname: Hostname configuration command.

2. Network Interface Configuration

Network interfaces are the bridge between Linux systems and networks, and configuring network interfaces is the foundation of network configuration.

2.1 Viewing Network Interfaces

# View network interfaces using ifconfig
ifconfig

# View network interfaces using ip command
ip addr
ip link show

# View detailed network interface information
ip -s link

# View specific network interface
ifconfig eth0
ip addr show eth0

2.2 Configuring Network Interfaces

2.2.1 Temporarily Configuring Network Interfaces

# configuration IP address using ifconfig
ifconfig eth0 192.168.1.100 netmask 255.255.255.0

# configuration IP address using ip command
ip addr add 192.168.1.100/24 dev eth0

# Enable network interface
ifconfig eth0 up
ip link set eth0 up

# Disable network interface
ifconfig eth0 down
ip link set eth0 down

# configuration MTU of network interface
ifconfig eth0 mtu 1500
ip link set eth0 mtu 1500

# configuration MAC address of network interface
ifconfig eth0 hw ether 00:11:22:33:44:55
ip link set eth0 address 00:11:22:33:44:55

2.2.2 Permanently Configuring Network Interfaces

Permanently configuring network interfaces requires modifying configuration files, which are located in different places for different Linux distributions.

2.2.2.1 Debian/Ubuntu Systems
# Edit network interface configuration file
sudo nano /etc/network/interfaces

# Static IP configuration example
auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

# DHCP configuration example
auto eth0
iface eth0 inet dhcp

# Restart network service
sudo systemctl restart networking
sudo ifdown eth0 && sudo ifup eth0
2.2.2.2 CentOS/RHEL Systems
# Edit network interface configuration file
sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0

# Static IP configuration example
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

# DHCP configuration example
TYPE=Ethernet
BOOTPROTO=dhcp
NAME=eth0
DEVICE=eth0
ONBOOT=yes

# Restart network service
sudo systemctl restart network
sudo ifdown eth0 && sudo ifup eth0
2.2.2.3 Using Networkmanagementr
# Install Networkmanagementr
sudo apt install network-manager  # Debian/Ubuntu
sudo yum install Networkmanagementr  # CentOS/RHEL

# configuration network using nmcli command

# View connections
nmcli con show

# View devices
nmcli dev show

# Create new Ethernet connection
nmcli con add type ethernet con-name eth0 ifname eth0 ip4 192.168.1.100/24 gw4 192.168.1.1

# configuration DNS
nmcli con mod eth0 ipv4.dns "8.8.8.8 8.8.4.4"

# Enable connection
nmcli con up eth0

# Disable connection
nmcli con down eth0

3. IP Address managementment

IP addresses are unique identifiers for network devices in a network, and proper IP address management is crucial for network communication.

3.1 IP Address Basics

  • IPv4 address: 32-bit address in dotted decimal format, such as 192.168.1.1.
  • IPv6 address: 128-bit address in colon-separated hexadecimal format, such as 2001:0db8:85a3:0000:0000:8a2e:0370:7334.
  • Subnet mask: Used to distinguish network address and host address, such as 255.255.255.0.
  • CIDR notation: Such as 192.168.1.0/24, indicating network address is 192.168.1.0 with 24-bit subnet mask.

3.2 Configuring IPv4 Addresses

# Temporarily configure IPv4 address
ip addr add 192.168.1.100/24 dev eth0

# Delete IPv4 address
ip addr del 192.168.1.100/24 dev eth0

# View IPv4 addresses
ip -4 addr show eth0

3.3 Configuring IPv6 Addresses

# Temporarily configure IPv6 address
ip addr add 2001:0db8:85a3:0000:0000:8a2e:0370:7334/64 dev eth0

# Delete IPv6 address
ip addr del 2001:0db8:85a3:0000:0000:8a2e:0370:7334/64 dev eth0

# View IPv6 addresses
ip -6 addr show eth0

# Enable IPv6
sysctl -w net.ipv6.conf.all.disable_ipv6=0

# Disable IPv6
sysctl -w net.ipv6.conf.all.disable_ipv6=1

3.4 Network Interface Aliases

Network interface aliases allow configuring multiple IP addresses on a single physical network interface.

# Temporarily create network interface alias
ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0
ip addr add 192.168.1.101/24 dev eth0 label eth0:0

# View network interface aliases
ifconfig
ip addr show

# Delete network interface alias
ifconfig eth0:0 down
ip addr del 192.168.1.101/24 dev eth0

4. Routing Configuration

Routing refers to the transmission path of data packets from source address to destination address, and routing configuration is crucial for network communication.

4.1 查看routing表

# usingroutecommands查看routing表
route -n

# usingipcommands查看routing表
ip route show
ip route list

# 查看IPv6routing表
ip -6 route show

4.2 添加routing

# usingroutecommands添加routing
# 添加networkrouting
route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1

# 添加主机routing
route add -host 192.168.2.100 gw 192.168.1.1

# 添加默认routing
route add default gw 192.168.1.1

# usingipcommands添加routing
# 添加networkrouting
ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0

# 添加主机routing
ip route add 192.168.2.100/32 via 192.168.1.1 dev eth0

# 添加默认routing
ip route add default via 192.168.1.1 dev eth0

# 添加IPv6routing
ip -6 route add 2001:db8::/32 via 2001:db8:1::1 dev eth0

4.3 deleterouting

# usingroutecommandsdeleterouting
# deletenetworkrouting
route del -net 192.168.2.0 netmask 255.255.255.0

# delete主机routing
route del -host 192.168.2.100

# delete默认routing
route del default

# usingipcommandsdeleterouting
# deletenetworkrouting
ip route del 192.168.2.0/24

# delete主机routing
ip route del 192.168.2.100/32

# delete默认routing
ip route del default

# deleteIPv6routing
ip -6 route del 2001:db8::/32

4.4 永久configurationrouting

# Debian/Ubuntusystem: 编辑/etc/network/interfacesfile
auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    up route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.2

# CentOS/RHELsystem: 编辑/etc/sysconfig/network-scripts/route-eth0file
192.168.2.0/24 via 192.168.1.2 dev eth0

#  or 编辑/etc/sysconfig/static-routesfile
any net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.2

# usingNetworkmanagementrconfigurationrouting
nmcli con mod eth0 +ipv4.routes "192.168.2.0/24 192.168.1.2"
nmcli con up eth0

5. DNS Configuration

DNS (Domain Name System) is a system that converts domain names to IP addresses, and correct DNS configuration is crucial for network access.

5.1 Viewing DNS Configuration

# View /etc/resolv.conf file
cat /etc/resolv.conf

# View current DNS servers
nmcli dev show | grep DNS

# Test DNS resolution using nslookup
nslookup example.com

# Test DNS resolution using dig
dig example.com

5.2 Configuring DNS

# Temporarily configure DNS (edit /etc/resolv.conf file)
sudo nano /etc/resolv.conf

# Add DNS servers
nameserver 8.8.8.8
nameserver 8.8.4.4

# Permanently configure DNS

# Debian/Ubuntu systems: Edit /etc/network/interfaces file
auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

# CentOS/RHEL systems: Edit /etc/sysconfig/network-scripts/ifcfg-eth0 file
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

# configuration DNS using Networkmanagementr
nmcli con mod eth0 ipv4.dns "8.8.8.8 8.8.4.4"
mcli con up eth0

5.3 Local Hostname Resolution

# Edit /etc/hosts file
sudo nano /etc/hosts

# Add local hostname resolution
127.0.0.1       localhost
127.0.1.1       hostname
192.168.1.100   server.example.com server

# View hostname
hostname

# Set hostname
sudo hostnamectl set-hostname server.example.com

# Or edit /etc/hostname file
sudo nano /etc/hostname
server.example.com

6. Network Service managementment

Network services are the core of Linux systems providing network functionality, and proper management of network services is crucial for the system's network functionality.

6.1 Network Service Overview

  • Networkmanagementr: Modern network management service.
  • networking: Traditional network management service (Debian/Ubuntu).
  • network: Traditional network management service (CentOS/RHEL).
  • ssh: Secure Shell service for remote login.
  • apache2/httpd: Web server.
  • mysql/mariadb: Database server.
  • postfix: Mail server.
  • iptables/firewalld: Firewall service.

6.2 Managing Network Services

# management network services using systemctl

# View network service status
systemctl status Networkmanagementr
systemctl status networking
systemctl status network

# Start network services
systemctl start Networkmanagementr
systemctl start networking
systemctl start network

# Stop network services
systemctl stop Networkmanagementr
systemctl stop networking
systemctl stop network

# Restart network services
systemctl restart Networkmanagementr
systemctl restart networking
systemctl restart network

# Enable network services (auto-start on boot)
systemctl enable Networkmanagementr
systemctl enable networking
systemctl enable network

# Disable network services (disable auto-start on boot)
systemctl disable Networkmanagementr
systemctl disable networking
systemctl disable network

6.3 Configuring Network Services

# configuration Networkmanagementr
# Edit configuration file
sudo nano /etc/Networkmanagementr/Networkmanagementr.conf

# Main configuration options
[main]
dns=default
plugins=ifupdown,keyfile

[ifupdown]
managed=false

[keyfile]
unmanaged-devices=*,except:type:ethernet,except:type:wifi,except:type:wwan

# Restart Networkmanagementr service
systemctl restart Networkmanagementr

7. Network Troubleshooting

Network troubleshooting is an important part of network management. When network problems occur, various tools and methods are needed to diagnose and solve the problems.

7.1 Basic Network Troubleshooting Steps

  1. Check physical connections: Whether network cables are properly plugged in, whether network card indicator lights are normal.
  2. Check network interfaces: Whether network interfaces are enabled, whether IP addresses are configured correctly.
  3. Check routing tables: Whether routing tables are configured correctly, whether default gateway is reachable.
  4. Check DNS configuration: Whether DNS servers are configured correctly, whether domain name resolution is normal.
  5. Check firewall: Whether firewall is blocking network connections.
  6. Check network services: Whether relevant network services are run normally.

7.2 Network Troubleshooting Tools

7.2.1 ping Command

# Test network connectivity
ping 127.0.0.1  # Test local loopback
ping 192.168.1.1  # Test gateway
ping 8.8.8.8  # Test external network
ping example.com  # Test domain name resolution

7.2.2 traceroute Command

# Trace network path
traceroute 8.8.8.8
tracepath 8.8.8.8

# IPv6 trace
traceroute6 2001:4860:4860::8888

7.2.3 netstat and ss Commands

# View network connections
netstat -tuln
ss -tuln

# View network connection status
netstat -ant
ss -ant

# View connections on specific port
netstat -tuln | grep 80
ss -tuln | grep 80

7.2.4 ifconfig and ip Commands

# View network interface status
ifconfig
ip addr show

# View detailed network interface information
ip -s link show eth0

7.2.5 nslookup and dig Commands

# Test DNS resolution
nslookup example.com
nslookup example.com 8.8.8.8

dig example.com
dig example.com @8.8.8.8

7.2.6 tcpdump Command

# Capture network packets
tcpdump -i eth0
tcpdump -i eth0 host 192.168.1.1
tcpdump -i eth0 port 80
tcpdump -i eth0 -w capture.pcap

# analysis captured packets
tcpdump -r capture.pcap

8. Network Configuration Practice

8.1 Case Objective

configuration the network interface of a Linux server, including static IP address, default gateway, DNS servers, etc., to ensure the server can connect to the network normally.

8.2 Implementation Steps

8.2.1 View Current Network Configuration

# View network interfaces
ip addr show

# View routing table
ip route show

# View DNS configuration
cat /etc/resolv.conf

8.2.2 configuration Network Interface

# Debian/Ubuntu systems: Edit /etc/network/interfaces file
auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

# CentOS/RHEL systems: Edit /etc/sysconfig/network-scripts/ifcfg-eth0 file
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

8.2.3 Restart Network Service

# Debian/Ubuntu systems
sudo systemctl restart networking

# CentOS/RHEL systems
sudo systemctl restart network

# Or use ifdown/ifup commands
sudo ifdown eth0 && sudo ifup eth0

8.2.4 Verify Network Configuration

# Verify IP address configuration
ip addr show eth0

# Verify routing configuration
ip route show

# Test network connectivity
ping 192.168.1.1  # Test gateway
ping 8.8.8.8  # Test external network

# Test domain name resolution
ping example.com

# View network connection status
ss -tuln

9. Interactive Exercises

Exercise 1: Network Interface Configuration

Perform the following operations:

  • 1. View network interfaces in the system.
  • 2. configuration a static IP address for the network interface (such as 192.168.1.100/24).
  • 3. configuration a default gateway (such as 192.168.1.1).
  • 4. configuration DNS servers (such as 8.8.8.8 and 8.8.4.4).
  • 5. Restart the network service and verify if the network configuration is correct.

Exercise 2: Routing Configuration

Perform the following operations:

  • 1. View the system's routing table.
  • 2. Add a network route (such as 192.168.2.0/24 via 192.168.1.2).
  • 3. Add a default route (such as via 192.168.1.1).
  • 4. Delete the added network route.
  • 5. Verify if the routing table is correct.

Exercise 3: DNS Configuration

Perform the following operations:

  • 1. View the system's DNS configuration.
  • 2. Modify the /etc/hosts file to add local hostname resolution.
  • 3. Modify the /etc/resolv.conf file to add DNS servers.
  • 4. Test DNS resolution using the nslookup command.
  • 5. Test DNS resolution using the dig command.

Exercise 4: Network Troubleshooting

Perform the following operations:

  • 1. Test network connectivity using the ping command.
  • 2. Trace network path using the traceroute command.
  • 3. View network connection status using the netstat or ss command.
  • 4. Capture network packets using the tcpdump command.
  • 5. analysis the captured packets to identify possible network issuess.