Chapter 7.1 - Network Interfaces
Learning Objectives
By the end of this chapter, you will be able to:
- Understand Linux network interfaces
- Configure interfaces with ip and ifconfig
- Manage IP addresses
- Understand network configuration files
- Enable/disable interfaces
Network Interfaces
Types of Interfaces
Types:
- eth0, eth1: Physical Ethernet interfaces
- wlan0: WiFi interface
- lo: Loopback (127.0.0.1)
- virbr0, veth: Virtual interfaces (VM, containers)
Basic Commands
ip - Modern Command
# View all interfaces
ip addr show
ip a
ip address
# View specific interface
ip addr show eth0
# View only active interfaces
ip link show up
# Statistics
ip -s link show
ifconfig - Classic Command
# View all interfaces
ifconfig -a
# View specific interface
ifconfig eth0
# Install if necessary (net-tools)
sudo apt install net-tools # Ubuntu/Debian
IP Configuration
Add an IP Address
# With ip
sudo ip addr add 192.168.1.100/24 dev eth0
# With ifconfig
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
# Verify
ip addr show eth0
Remove an IP Address
# With ip
sudo ip addr del 192.168.1.100/24 dev eth0
# With ifconfig
sudo ifconfig eth0 0.0.0.0
Multiple IP Addresses
# Add multiple IPs on an interface
sudo ip addr add 192.168.1.100/24 dev eth0
sudo ip addr add 192.168.1.101/24 dev eth0
# View all IPs
ip addr show eth0
Enable/Disable Interfaces
With ip
# Enable
sudo ip link set eth0 up
# Disable
sudo ip link set eth0 down
# View status
ip link show eth0
With ifconfig
# Enable
sudo ifconfig eth0 up
# Disable
sudo ifconfig eth0 down
Permanent Configuration
Ubuntu/Debian (Netplan)
# /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: true
# Or static:
# addresses:
# - 192.168.1.100/24
# gateway4: 192.168.1.1
# nameservers:
# addresses:
# - 8.8.8.8
# - 8.8.4.4
Apply:
sudo netplan apply
sudo netplan try # Test with rollback
Red Hat/CentOS (NetworkManager)
# File: /etc/sysconfig/network-scripts/ifcfg-eth0
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
Restart:
sudo systemctl restart NetworkManager
Manual Configuration (systemd-networkd)
# /etc/systemd/network/20-wired.network
[Match]
Name=eth0
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=8.8.4.4
Interface Information
Full Details
# With ip
ip -d link show eth0 # Details
ip -s -s link show eth0 # Detailed statistics
# With ethtool (install if necessary)
sudo apt install ethtool
ethtool eth0 # Hardware information
ethtool -S eth0 # Statistics
MTU
# View MTU
ip link show eth0 | grep mtu
# Change MTU
sudo ip link set eth0 mtu 1500
Practical Examples
Static Configuration
#!/bin/bash
# Network configuration script
INTERFACE="eth0"
IP="192.168.1.100"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.1"
# Configure IP
sudo ip addr add $IP/24 dev $INTERFACE
# Enable interface
sudo ip link set $INTERFACE up
# Add default route
sudo ip route add default via $GATEWAY
Verification
#!/bin/bash
# Verify network configuration
echo "=== Interfaces ==="
ip addr show
echo "=== Routes ==="
ip route show
echo "=== Connections ==="
ss -tuln
Summary
In this chapter, you learned:
Interfaces: eth0, wlan0, lo, virtual interfaces
Commands: ip (modern), ifconfig (classic)
Configuration: IP, netmask, enable/disable
Permanent: Netplan (Ubuntu), NetworkManager (Red Hat), systemd-networkd
Information: ip -d, ethtool for details
Practical: Configuration and verification scripts
Next Steps
Chapter 7.2: Routing and Routing Tables
Chapter 7.3: DNS and Name Resolution
Chapter created on: December 2024