Роутер и проброс портов на Debian 10.9

Подробная инструкция по настройке роутера в связке iptables + dnsmasq на Debian GNU/Linux, а также проброс портов по IP-адресам машин в локальной сети.

Исходные данные

Имеем на машине 2 сетевых интерфейса:

enp0s3 — для внешней сети 192.168.1.0/24

enp0s8 — для внутренней сети 192.168.52.0/24

Также у нас есть шлюз во внешней сети. Это маршрутизатор с IP-адресом 192.168.1.1

Наша задача — настроить доступ в Интернет для внутренней сети.

Открываем файл /etc/sysctl.conf

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# nano /etc/sysctl.conf
# nano /etc/sysctl.conf
# nano /etc/sysctl.conf

и в самом конце пропишем строчку:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
net.ipv4.ip_forward = 1
net.ipv4.ip_forward = 1
net.ipv4.ip_forward = 1

Проверяем

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# sysctl -p
# sysctl -p
# sysctl -p

Должен быть такой ответ:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
net.ipv4.ip_forward = 1
net.ipv4.ip_forward = 1
net.ipv4.ip_forward = 1

Теперь создадим правила iptables. Для этого создадим каталог /etc/firewall/ и файл /etc/firewall/iptables.sh

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# mkdir /etc/firewall/
# touch /etc/firewall/iptables.sh
# mkdir /etc/firewall/ # touch /etc/firewall/iptables.sh
# mkdir /etc/firewall/
# touch /etc/firewall/iptables.sh

открываем его

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# nano /etc/firewall/iptables.sh
# nano /etc/firewall/iptables.sh
# nano /etc/firewall/iptables.sh

и в нём пропишем такой скрипт:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/sh
iptables -F
iptables -t nat -A POSTROUTING -o enp0s+ -j MASQUERADE
iptables -t filter -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t filter -A INPUT -p icmp -j ACCEPT
iptables -t filter -A INPUT -i lo -j ACCEPT
iptables -t filter -A INPUT -i enp0s+ -j ACCEPT
iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t filter -A FORWARD -p icmp -j ACCEPT
iptables -t filter -A FORWARD -i lo -j ACCEPT
iptables -t filter -A FORWARD -i enp0s+ -j ACCEPT
iptables -t filter -A FORWARD -o enp0s+ -j ACCEPT
iptables -t filter -A INPUT -j REJECT --reject-with icmp-host-prohibited
iptables -t filter -A FORWARD -j REJECT --reject-with icmp-host-prohibited
iptables-save > /etc/network/iptables
#!/bin/sh iptables -F iptables -t nat -A POSTROUTING -o enp0s+ -j MASQUERADE iptables -t filter -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -t filter -A INPUT -p icmp -j ACCEPT iptables -t filter -A INPUT -i lo -j ACCEPT iptables -t filter -A INPUT -i enp0s+ -j ACCEPT iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -t filter -A FORWARD -p icmp -j ACCEPT iptables -t filter -A FORWARD -i lo -j ACCEPT iptables -t filter -A FORWARD -i enp0s+ -j ACCEPT iptables -t filter -A FORWARD -o enp0s+ -j ACCEPT iptables -t filter -A INPUT -j REJECT --reject-with icmp-host-prohibited iptables -t filter -A FORWARD -j REJECT --reject-with icmp-host-prohibited iptables-save > /etc/network/iptables
#!/bin/sh

iptables -F
iptables -t nat -A POSTROUTING -o enp0s+ -j MASQUERADE
iptables -t filter -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t filter -A INPUT -p icmp -j ACCEPT
iptables -t filter -A INPUT -i lo -j ACCEPT
iptables -t filter -A INPUT -i enp0s+ -j ACCEPT
iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t filter -A FORWARD -p icmp -j ACCEPT
iptables -t filter -A FORWARD -i lo -j ACCEPT
iptables -t filter -A FORWARD -i enp0s+ -j ACCEPT
iptables -t filter -A FORWARD -o enp0s+ -j ACCEPT
iptables -t filter -A INPUT -j REJECT --reject-with icmp-host-prohibited
iptables -t filter -A FORWARD -j REJECT --reject-with icmp-host-prohibited

iptables-save > /etc/network/iptables

Делаем его исполняемым

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# chmod u+x /etc/firewall/iptables.sh
# chmod u+x /etc/firewall/iptables.sh
# chmod u+x /etc/firewall/iptables.sh

Теперь откроем файл /etc/network/interfaces

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# nano /etc/network/interfaces
# nano /etc/network/interfaces
# nano /etc/network/interfaces

и в самом конце пропишем строчку:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pre-up iptables-restore < /etc/network/iptables
pre-up iptables-restore < /etc/network/iptables
pre-up iptables-restore < /etc/network/iptables

Также создадим файл /etc/network/iptables

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# touch /etc/network/iptables
# touch /etc/network/iptables
# touch /etc/network/iptables

Теперь вернёмся к файлу /etc/firewall/iptables.sh — запускаем скрипт

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# sh /etc/firewall/iptables.sh
# sh /etc/firewall/iptables.sh
# sh /etc/firewall/iptables.sh

Правила файрволла активируются и пропишутся в /etc/network/iptables. В дальнейшем, при перезагрузке системы, система будет тянуть правила из него.

Далее установим dnsmasq

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# apt install dnsmasq
# apt install dnsmasq
# apt install dnsmasq

После установки система сама запустит службу dnsmasq и добавит её в автозагрузку. Это можно проверить командой:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# systemctl status dnsmasq
# systemctl status dnsmasq
# systemctl status dnsmasq

Консоль покажет:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Loaded: loaded
Active: active (running)
Loaded: loaded Active: active (running)
Loaded: loaded
Active: active (running)

После этого открываем файл /etc/dnsmasq.conf

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# nano /etc/dnsmasq.conf
# nano /etc/dnsmasq.conf
# nano /etc/dnsmasq.conf

и в нём пропишем такой конфиг:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
domain-needed
bogus-priv
interface=enp0s8
resolv-file=/etc/resolv.conf
dhcp-range=192.168.52.131,192.168.52.180,24h
cache-size=150
domain-needed bogus-priv interface=enp0s8 resolv-file=/etc/resolv.conf dhcp-range=192.168.52.131,192.168.52.180,24h cache-size=150
domain-needed
bogus-priv
interface=enp0s8
resolv-file=/etc/resolv.conf
dhcp-range=192.168.52.131,192.168.52.180,24h
cache-size=150

Перезапускаем службу dnsmasq

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# systemctl restart dnsmasq
# systemctl restart dnsmasq
# systemctl restart dnsmasq

или лучше вообще перезагрузить машину

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# shutdown -r now
# shutdown -r now
# shutdown -r now

Теперь роутер должен работать и машины в сети получать ip-адреса и dns.

При необходимости в пробросе портов по IP-адресам для доступа из внешней сети нужно снова открыть скрипт /etc/firewall/iptables.sh

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# nano /etc/firewall/iptables.sh
# nano /etc/firewall/iptables.sh
# nano /etc/firewall/iptables.sh

в нём после строчки

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
iptables -t nat -A POSTROUTING -o enp0s+ -j MASQUERADE
iptables -t nat -A POSTROUTING -o enp0s+ -j MASQUERADE
iptables -t nat -A POSTROUTING -o enp0s+ -j MASQUERADE

добавить такие:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
iptables -t nat -A PREROUTING -i enp0s3 -p tcp --dport 1234 -j DNAT --to-destination 192.168.52.111:1234
iptables -t nat -A PREROUTING -i enp0s3 -p udp --dport 5678 -j DNAT --to-destination 192.168.52.112:5678
iptables -t nat -A PREROUTING -i enp0s3 -p tcp --dport 1234 -j DNAT --to-destination 192.168.52.111:1234 iptables -t nat -A PREROUTING -i enp0s3 -p udp --dport 5678 -j DNAT --to-destination 192.168.52.112:5678
iptables -t nat -A PREROUTING -i enp0s3 -p tcp --dport 1234 -j DNAT --to-destination 192.168.52.111:1234
iptables -t nat -A PREROUTING -i enp0s3 -p udp --dport 5678 -j DNAT --to-destination 192.168.52.112:5678

а после строчки

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
iptables -t filter -A INPUT -i enp0s+ -j ACCEPT
iptables -t filter -A INPUT -i enp0s+ -j ACCEPT
iptables -t filter -A INPUT -i enp0s+ -j ACCEPT

добавить такие:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
iptables -t filter -A INPUT -p tcp -m state --state NEW -m tcp --dport 1234 -j ACCEPT
iptables -t filter -A INPUT -p tcp -m state --state NEW -m udp --dport 5678 -j ACCEPT
iptables -t filter -A INPUT -p tcp -m state --state NEW -m tcp --dport 1234 -j ACCEPT iptables -t filter -A INPUT -p tcp -m state --state NEW -m udp --dport 5678 -j ACCEPT
iptables -t filter -A INPUT -p tcp -m state --state NEW -m tcp --dport 1234 -j ACCEPT
iptables -t filter -A INPUT -p tcp -m state --state NEW -m udp --dport 5678 -j ACCEPT
  • 1234 и 5678 — номера портов
  • tcp и udp — протоколы
  • 192.168.52.111 и 192.168.52.112 — ip-адреса машин в локальной (внутренней) сети

Затем снова запускаем скрипт /etc/firewall/iptables.sh

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# sh /etc/firewall/iptables.sh
# sh /etc/firewall/iptables.sh
# sh /etc/firewall/iptables.sh

чтобы изменения вступили в силу.

Новые правила заново перечитаются и запишутся в файл /etc/network/iptables, а уже из этого файла при перезагрузке сервера будут автоматически запускаться эти правила.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *