Internet sharing (Français)
Cet article explique la marche à suivre pour partager une connexion Internet d'une machine à une autre.
Pré-requis
La machine fonctionnant comme le serveur devra posséder une interface réseau supplémentaire pour établir le lien (data link layer) avec la machine qui recevra la connexion Internet:
- Pour partager Internet avec plusieurs machines un switch peut fournir la connexion physique.
- Un périphérique sans fil peut également partager une conexion vers plusieurs machines, voir Software access point (en) en premier lieu.
- Si vous partagez l'accès à une seule machine un câble croisé (crossover cable) peut suffir. Dans le cas de deux ordinateurs ayant des carte ethernet activant la fonctionnalité MDI-X, un câble normal suffit. Executez
ethtool interface | grep MDI
en tant que root pour en connaitre la disponibilité.
Configuration
Cette section suppose que le périphérique réseau connecté à l'ordinateur client est nommé net0
et que le périphérique réseau connecté à Internet est internet0
.
Toute la configuration s'effectue sur l'ordinateur serveur, sauf l'étape finale #Assigning IP addresses to the client PC(s).
Adresse IP statique
Sur l'ordinateur serveur assignez une adresse statique IPv4 à l'interface connectée aux autres machines. Les 3 premiers bytes de cette adresse ne pourront pas être les mêmes que ceux d'une autre interface, à moins que chaque interface ait un netmasks strictement suppérieur à /24.
# ip link set up dev net0 # ip addr add 192.168.123.100/24 dev net0 # arbitrary address
Pour assigner une adresse statique lors du démarrage, vous pouvez utiliser un gestionnaire de connexion réseau.
Activer la retransmission des paquets
Pour consulter l'état actuel:
# sysctl -a | grep forward
Remarquez que des options existent pour activer la la retransmission de paquet par défaut, par interface, ainsi que des options pour IPv4/IPv6 par interface.
Pur activer la retransmission des paquets après démarrage utilisez:
# sysctl net.ipv4.ip_forward=1
sysctl net.ipv4.conf.interface_name.forwarding=1
à la place.Éditez /etc/sysctl.d/30-ipforward.conf
pour établir les définitivement le changement après redémarrage pour toutes les interfaces:
/etc/sysctl.d/30-ipforward.conf
net.ipv4.ip_forward=1 net.ipv6.conf.default.forwarding=1 net.ipv6.conf.all.forwarding=1
Après ceci, il est conseillé de bien vérifier que le «packet forwarding» est bien actif après reboot.
Activer le NAT
Avec iptables
Installez le paquet iptables. Et utilisez iptables
pour activer le NAT:
# iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE # iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT # iptables -A FORWARD -i net0 -o internet0 -j ACCEPT
If connected via PPPoE, clamp mss to pmtu in order the prevent fragmentation:
# iptables -t mangle -A FORWARD -o ppp0 -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
Read the iptables article for more information (especially saving the rule and applying it automatically on boot). There is also an excellent guide on iptables Simple stateful firewall.
With nftables
Install the nftables package. To enable NAT with nftables, you will have to create the postrouting
chain in a new/existing table:
# nft add table inet nat # nft add chain inet nat postrouting '{ type nat hook postrouting priority 100 ; }'
After that, you have to masquerade the net0
addresses for internet0
:
# nft add rule inet nat postrouting oifname internet0 masquerade
You may want to add some more firewall restrictions on the forwarding (assuming the filter table already exists, like configured in nftables#Server:
# nft add chain inet filter forward '{ type filter hook forward priority 0; policy drop; }' # nft add rule inet filter forward ct state related,established accept # nft add rule inet filter forward iifname net0 oifname internet0 accept
You can find more information on NAT in nftables in the nftables Wiki. If you want to make these changes permanent, follow the instructions on nftables
Assigning IP addresses to the client PC(s)
If you are planning to regularly have several machines using the internet shared by this machine, then is a good idea to install a DHCP server, such as dhcpd or dnsmasq. Then configure a DHCP client (e.g. dhcpcd) on every client PC.
Incoming connections to UDP port 67 has to be allowed for DHCP server. It also necessary to allow incoming connections to UDP/TCP port 53 for DNS requests.
# iptables -I INPUT -p udp --dport 67 -i net0 -j ACCEPT # iptables -I INPUT -p udp --dport 53 -s 192.168.123.0/24 -j ACCEPT # iptables -I INPUT -p tcp --dport 53 -s 192.168.123.0/24 -j ACCEPT
If you are not planning to use this setup regularly, you can manually add an IP to each client instead.
Manually adding an IP
Instead of using DHCP, a static IP address and a default route via 192.168.123.100
can also be configured manually. There are many tools available to configure the network accordingly. One prominent example of such a tool is ip(8), see Network configuration#Network management. Alternatively, one can use a .network
file, see Systemd-networkd#Wired adapter using a static IP to setup a static IP.
Configure a DNS server for each client, see Domain name resolution for details.
That is it. The client PC should now have Internet.
Troubleshooting
If you are able to connect the two PCs but cannot send data (for example, if the client PC makes a DHCP request to the server PC, the server PC receives the request and offers an IP to the client, but the client does not accept it, timing out instead), check that you do not have other iptables rules interfering.
Voir aussi
- le guide Xyne (avec scripts) pour lancer un sous-réseau avec DHCP et DNS
- NetworkManager peut être utilisé pour configurer un partage de connexion.