Network configuration
Table of contents
- Fundamental network concepts
- Network configuration files
- Commands: ip, ifconfig, nmcli
- Static vs DHCP configuration
- /etc/hosts and /etc/resolv.conf
- Hands-on exercises
1 - Fundamental network concepts
The layered model
Essential terms
| Term | Description | Example |
|---|---|---|
| IP address | Network identifier | 192.168.1.100 |
| Subnet mask | Defines the network | 255.255.255.0 (/24) |
| Gateway | Exit router | 192.168.1.1 |
| DNS | Name resolution | 8.8.8.8 |
| DHCP | Automatic IP assignment | - |
| MAC | Physical address | aa:bb:cc:dd:ee:ff |
CIDR notation
192.168.1.0/24
│ │
│ └── Nombre de bits pour le réseau (24 bits = 255.255.255.0)
└── Adresse réseau
| CIDR | Mask | Hosts |
|---|---|---|
| /8 | 255.0.0.0 | 16M |
| /16 | 255.255.0.0 | 65K |
| /24 | 255.255.255.0 | 254 |
| /32 | 255.255.255.255 | 1 |
Network interfaces
| Type | Name | Description |
|---|---|---|
| Loopback | lo | Local interface (127.0.0.1) |
| Ethernet | eth0, enp0s3 | Network cable |
| Wi-Fi | wlan0, wlp2s0 | Wireless |
| Bridge | br0, docker0 | Network bridge |
| Virtual | veth* | Containers |
🔝 Back to table of contents
2 - Network configuration files
Debian/Ubuntu (Netplan)
# /etc/netplan/00-installer-config.yaml
network:
version: 2
ethernets:
eth0:
dhcp4: true
eth1:
addresses:
- 192.168.1.100/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
# Appliquer la configuration
sudo netplan apply
# Tester avant d'appliquer
sudo netplan try
Ubuntu (old: /etc/network/interfaces)
# /etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
auto eth1
iface eth1 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8
RHEL/CentOS/Fedora
# /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
PREFIX=24
GATEWAY=192.168.1.1
DNS1=8.8.8.8
🔝 Back to table of contents
3 - Commands: ip, ifconfig, nmcli
ip - The modern command
# Voir les interfaces
ip link show
ip link
# Voir les adresses IP
ip addr show
ip a
# Voir une interface spécifique
ip addr show eth0
# Activer/désactiver une interface
sudo ip link set eth0 up
sudo ip link set eth0 down
# Ajouter une adresse IP
sudo ip addr add 192.168.1.100/24 dev eth0
# Supprimer une adresse
sudo ip addr del 192.168.1.100/24 dev eth0
# Voir la table de routage
ip route show
ip r
# Ajouter une route
sudo ip route add 10.0.0.0/8 via 192.168.1.1
# Route par défaut
sudo ip route add default via 192.168.1.1
ifconfig - The legacy command
# Installer si nécessaire
sudo apt install net-tools
# Voir les interfaces
ifconfig
# Interface spécifique
ifconfig eth0
# Configurer une IP (temporaire)
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
# Activer/désactiver
sudo ifconfig eth0 up
sudo ifconfig eth0 down
note
ifconfig is deprecated. Prefer ip, which is more powerful and modern.
nmcli - NetworkManager CLI
# État des connexions
nmcli connection show
# Détails d'une connexion
nmcli connection show "Wired connection 1"
# Activer une connexion
nmcli connection up "Wired connection 1"
# Désactiver
nmcli connection down "Wired connection 1"
# Créer une connexion statique
nmcli connection add con-name "static-eth0" \
type ethernet \
ifname eth0 \
ip4 192.168.1.100/24 \
gw4 192.168.1.1
# Modifier une connexion
nmcli connection modify "static-eth0" ipv4.dns "8.8.8.8 8.8.4.4"
# Appliquer les changements
nmcli connection up "static-eth0"
Comparison table
| Action | ip | ifconfig | nmcli |
|---|---|---|---|
| View IPs | ip a | ifconfig | nmcli |
| Enable | ip link set up | ifconfig up | nmcli con up |
| Add IP | ip addr add | ifconfig | nmcli con mod |
| Routes | ip route | route | nmcli |
🔝 Back to table of contents
4 - Static vs DHCP configuration
DHCP (automatic)
DHCP advantages:
- Automatic configuration
- No IP conflicts
- Centralized management
Disadvantages:
- IP can change
- Depends on the DHCP server
Static IP
Advantages:
- Fixed and predictable IP
- Necessary for servers
- No DHCP dependency
Disadvantages:
- Manual configuration
- Risk of IP conflict
When to use what?
| Situation | Recommendation |
|---|---|
| Workstation | DHCP |
| Web server | Static IP |
| Database | Static IP |
| Laptop | DHCP |
| Critical infrastructure | Static IP |
Netplan configuration - Examples
# DHCP
network:
version: 2
ethernets:
eth0:
dhcp4: true
# Statique
network:
version: 2
ethernets:
eth0:
dhcp4: false
addresses:
- 192.168.1.100/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
🔝 Back to table of contents
5 - /etc/hosts and /etc/resolv.conf
/etc/hosts - Local resolution
# /etc/hosts
127.0.0.1 localhost
127.0.1.1 mon-pc
192.168.1.10 serveur-web web
192.168.1.20 serveur-db db
Usage:
- Resolution without DNS
- Override DNS names
- Testing and development
# Test
ping web
# Résout en 192.168.1.10
/etc/resolv.conf - DNS configuration
# /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
search example.com
| Directive | Description |
|---|---|
nameserver | DNS server (max 3) |
search | Search domains |
domain | Default domain |
warning
On modern systems, /etc/resolv.conf is often managed automatically by NetworkManager or systemd-resolved. Do not modify it directly!
systemd-resolved
# Voir la configuration DNS réelle
resolvectl status
# Ou
systemd-resolve --status
# Tester la résolution
resolvectl query google.com
Resolution order (nsswitch.conf)
# /etc/nsswitch.conf
hosts: files dns
Order: /etc/hosts first, then DNS.
🔝 Back to table of contents
6 - Hands-on exercises
Exercise 1: Explore the configuration
# 1. Affichez vos interfaces
ip a
# 2. Notez votre adresse IP et masque
ip -4 addr show | grep inet
# 3. Affichez la passerelle
ip route | grep default
# 4. Affichez les DNS
cat /etc/resolv.conf
# ou
resolvectl status
Exercise 2: /etc/hosts
# 1. Ajoutez une entrée de test
sudo nano /etc/hosts
# Ajoutez : 127.0.0.1 montest
# 2. Testez
ping -c 2 montest
# 3. Supprimez l'entrée
sudo nano /etc/hosts
# Supprimez la ligne
Exercise 3: Manipulate an interface (with caution)
# 1. Notez votre configuration actuelle
ip a show eth0
# 2. Ajoutez une IP secondaire
sudo ip addr add 192.168.100.1/24 dev eth0
# 3. Vérifiez
ip a show eth0
# 4. Supprimez l'IP ajoutée
sudo ip addr del 192.168.100.1/24 dev eth0
Quiz
Q1. Which command displays the IP addresses?
Answer
ip addr show or ip a
Q2. Which file allows local name resolution?
Answer
/etc/hosts
Q3. How do you apply a Netplan configuration?
Answer
sudo netplan apply
🔝 Back to table of contents
Key takeaways
ip areplacesifconfigfor viewing IPsip routeto view/modify routes- Netplan on modern Ubuntu, NetworkManager on RHEL
/etc/hostsfor local resolution- DHCP for workstations, static IP for servers
- Always use
netplan trybeforeapply(auto rollback)