Nous allons appliquer le routage vu la semaine dernière au cas d'étude de notre petite ferme de serveur (le labo d'il y a 2 semaines).
Configurer les interfaces: éditer le fichier
# The primary network interface auto eth0 iface eth0 inet static address 192.168.2.51 netmask 255.255.255.0 gateway 192.168.2.2 network 192.168.2.0
auto eth1 iface eth1 inet static address 10.0.0.1 netmask 255.255.255.0 network 10.0.0.0
Redémarrer le service réseau:
/etc/init.d/networking restart
vi /etc/network/options:
ip_forward=yes
Redémarrer le service réseau:
/etc/init.d/networking restart
Il faut que les paquets partant d'un serveur donnent l'impression qu'ils partent du routeur: source NAT en postrouting:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
ou
iptables -t nat -A POSTROUTING --dst 10.0.0.0/24 -j SNAT --to-source 192.168.2.51
= pour le trafic dont la destination était 10.0.0.x, mets comme source du paquet 192.18.2.51
Pour chaque service, il va falloir mettre une ligne. Server web (10.0.0.2): http et https:
iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 80 -j DNAT --to-destination 10.0.0.2 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 443 -j DNAT --to-destination 10.0.0.2
Serveur ftp (10.0.0.2):
iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 21 -j DNAT --to-destination 10.0.0.2
Serveur de noms (10.0.0.3):
iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 53 -j DNAT --to-destination 10.0.0.3
Serveur mail (10.0.0.4): smtp et smtps:
iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 25 -j DNAT --to-destination 10.0.0.4 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 465 -j DNAT --to-destination 10.0.0.4
Serveur mail (10.0.0.4): pop et pops:
iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 110 -j DNAT --to-destination 10.0.0.4 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 995 -j DNAT --to-destination 10.0.0.4
Serveur mail (10.0.0.4): imap et imaps:
iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 143 -j DNAT --to-destination 10.0.0.4 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 993 -j DNAT --to-destination 10.0.0.4
Accès ssh:
iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 2202 -j DNAT --to-destination 10.0.0.2:22 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 2203 -j DNAT --to-destination 10.0.0.3:22 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 2204 -j DNAT --to-destination 10.0.0.4:22 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 2205 -j DNAT --to-destination 10.0.0.5:22
Rassemblons cela dans un script:
#!/bin/bash iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 80 -j DNAT --to-destination 10.0.0.2 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 443 -j DNAT --to-destination 10.0.0.2 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 21 -j DNAT --to-destination 10.0.0.2 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 53 -j DNAT --to-destination 10.0.0.3 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 25 -j DNAT --to-destination 10.0.0.4 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 465 -j DNAT --to-destination 10.0.0.4 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 110 -j DNAT --to-destination 10.0.0.4 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 995 -j DNAT --to-destination 10.0.0.4 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 993 -j DNAT --to-destination 10.0.0.4 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 143 -j DNAT --to-destination 10.0.0.4 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 2202 -j DNAT --to-destination 10.0.0.2:22 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 2203 -j DNAT --to-destination 10.0.0.3:22 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 2204 -j DNAT --to-destination 10.0.0.4:22 iptables -t nat -A PREROUTING --dst 192.168.2.51 -p tcp --dport 2205 -j DNAT --to-destination 10.0.0.5:22
Ensuite, il faut que ce script soit chargé au moment ou les interfaces réseaux s'activent. cf. le script attaché qui fonctionne (testé).
Modifier les adresses des serveurs (aussi le fichier /etc/networking/interfaces):