Skip to main content

Network configuration


Table of contents

  1. Fundamental network concepts
  2. Network configuration files
  3. Commands: ip, ifconfig, nmcli
  4. Static vs DHCP configuration
  5. /etc/hosts and /etc/resolv.conf
  6. Hands-on exercises


1 - Fundamental network concepts

The layered model

Essential terms

TermDescriptionExample
IP addressNetwork identifier192.168.1.100
Subnet maskDefines the network255.255.255.0 (/24)
GatewayExit router192.168.1.1
DNSName resolution8.8.8.8
DHCPAutomatic IP assignment-
MACPhysical addressaa: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
CIDRMaskHosts
/8255.0.0.016M
/16255.255.0.065K
/24255.255.255.0254
/32255.255.255.2551

Network interfaces

TypeNameDescription
LoopbackloLocal interface (127.0.0.1)
Etherneteth0, enp0s3Network cable
Wi-Fiwlan0, wlp2s0Wireless
Bridgebr0, docker0Network bridge
Virtualveth*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

Actionipifconfignmcli
View IPsip aifconfignmcli
Enableip link set upifconfig upnmcli con up
Add IPip addr addifconfignmcli con mod
Routesip routeroutenmcli

🔝 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?

SituationRecommendation
WorkstationDHCP
Web serverStatic IP
DatabaseStatic IP
LaptopDHCP
Critical infrastructureStatic 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
DirectiveDescription
nameserverDNS server (max 3)
searchSearch domains
domainDefault 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 a replaces ifconfig for viewing IPs
  • ip route to view/modify routes
  • Netplan on modern Ubuntu, NetworkManager on RHEL
  • /etc/hosts for local resolution
  • DHCP for workstations, static IP for servers
  • Always use netplan try before apply (auto rollback)

🔝 Back to table of contents


← Previous chapter | Next chapter: Network diagnostics →