Witam. Posiadam domowy serwer z dwoma interfejsami sieciowymi.
eth0 - łączy się z internetem, działa w podsieci 192.168.0.0/27
eth1 - łączy się z klientami w podsieci 192.168.6.0/24
Na komputerze jest zainstalowany Debian Jessie. Chciałbym jednak rozszerzyć nieco funkcjonalność serwera, konfigurując go jako router - urządzenia dołączone do interfejsu eth1 miałyby możliwość komunikować się ze światem zewnętrznym na interfejsie eth0.
Na serwerze funkcjonuje już usługa DHCP. Niestety, mam problem z przekierowaniem adresów.
Zawartość pliku /proc/sys/net/ipv4/ip_forward ma się następująco:
1
Z kolei plik /etc/sysctl.conf wygląda tak:
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
#
#kernel.domainname = example.com
# Uncomment the following to stop low-level messages on console
#kernel.printk = 3 4 1 3
##############################################################3
# Functions previously found in netbase
#
# Uncomment the next two lines to enable Spoof protection (reverse-path filter)
# Turn on Source Address Verification in all interfaces to
# prevent some spoofing attacks
#net.ipv4.conf.default.rp_filter=1
#net.ipv4.conf.all.rp_filter=1
# Uncomment the next line to enable TCP/IP SYN cookies
# See http://lwn.net/Articles/277146/
# Note: This may impact IPv6 TCP sessions too
#net.ipv4.tcp_syncookies=1
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
# Uncomment the next line to enable packet forwarding for IPv6
# Enabling this option disables Stateless Address Autoconfiguration
# based on Router Advertisements for this host
#net.ipv6.conf.all.forwarding=1
###################################################################
# Additional settings - these settings can improve the network
# security of the host and prevent against some network attacks
# including spoofing attacks and man in the middle attacks through
# redirection. Some network environments, however, require that these
# settings are disabled so review and enable them as needed.
#
# Do not accept ICMP redirects (prevent MITM attacks)
#net.ipv4.conf.all.accept_redirects = 0
#net.ipv6.conf.all.accept_redirects = 0
# _or_
# Accept ICMP redirects only for gateways listed in our default
# gateway list (enabled by default)
# net.ipv4.conf.all.secure_redirects = 1
#
# Do not send ICMP redirects (we are not a router)
#net.ipv4.conf.all.send_redirects = 0
#
# Do not accept IP source route packets (we are not a router)
#net.ipv4.conf.all.accept_source_route = 0
#net.ipv6.conf.all.accept_source_route = 0
#
# Log Martian Packets
#net.ipv4.conf.all.log_martians = 1
#
Niestety, o ile komputer dołączony do interfejsu eth1 widzi sieć 192.168.6.0/24, to niestety nie ma dostępu do internetu. Próbowałem znaleźć rozwiązanie problemu. Niestety, każdy tekst dotyczący stawiania routera, podaje inną konfigurację iptables. Co więc robię źle?
To już trochę wiekowy poradnik. Obecnie tworzenie skryptu startowego to kiepski pomysł, składnia też się trochą zmieniła, ale dosyć szybko zauważysz w czym problem.
default via 192.168.0.1 dev eth0
192.168.0.0/27 dev eth0 proto kernel scope link src 192.168.0.13
192.168.6.0/24 dev eth1 proto kernel scope link src 192.168.6.1
Czyli reguły w łańcuchu forward powinny być po prostu zamienione miejscami?
#!/bin/sh
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A FORWARD -o lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A FORWARD -s 192.168.0.0/27 -j ACCEPT
iptables -A FORWARD -s 192.168.6.0/24 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.0.0/21 -j MASQUERADE
Generalnie przy polityce accept reguły dla połączeń nawiązanych i dla samego łańcucha forward są zbędne. Je tworzy się dla polityki drop. Prawdopodobnie robisz translację z 192.168.6.0 na 192.168.0.0, a sama podsieci 192.168.0.0 również może wymagać translacji, dlatego robisz nat dla nadsieci 192.168.0.0/21 (w jednej regule dla obu podsieci).
ip a
ip route
iptables -L
iptables -t nat -L
cat /proc/sys/net/ipv4/ip_forward
ping 192.168.6.x
ping 8.8.8.8
Za 192.168.6.x podstaw ip komputera w tej podsieci.
Dobra. Widzę, że serwer wpięty jest jeszcze do jakiegoś routera. Musisz dodać na routerze trasę statyczną do podsieci 192.168.6.0/24 przez ip serwera. Chociaż nat powinien to załatwić bez problemu, ale podwójny nat jest zbedny. Dodaj trasę na routerze, do którego wpięty jest serwer.
Pokaż jeszcze wyniki poleceń z hosta w podsieci 192.168.6.0/24.
No właśnie. Zapomniałem o tym wspomnieć. Ba, uznałem to wręcz za mało istotne. Żeby było śmieszniej, to serwer jest podpięty do routera, który jest podpięty do routera podpiętego do routera, który dopiero łączy się z internetem. Niestety, router do którego podpięty jest serwer, umożliwia wyłącznie wprowadzanie reguł routingu dla interfejsu WAN.
Zepnij wszystkie routery przełącznikami. Nie wiem co masz za router, ale 99% routerów umożliwia dodawanie tras statycznych. Ewentualnie spróbuj jeszcze raz z NATem, ale wykorzystaj snat (maskaradę wykorzystuje się głównie przy zmiennym IP).
iptables -A POSTROUTING -s 192.168.6.0/24 -j SNAT --to 192.168.0.x