This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
сервис_nat [2019/07/17 11:24] val [Ubuntu (iptables)] |
сервис_nat [2024/04/23 13:57] val [nftables] |
||
---|---|---|---|
Line 8: | Line 8: | ||
=== Заполнение таблицы nat (eth1 - внешний интерфейс) === | === Заполнение таблицы nat (eth1 - внешний интерфейс) === | ||
<code> | <code> | ||
+ | debian11# apt install iptables | ||
+ | |||
root@gate:~# apt install conntrack | root@gate:~# apt install conntrack | ||
Line 30: | Line 32: | ||
</code> | </code> | ||
- | === Сохранение состояния iptables === | + | === Управление состоянием iptables === |
+ | |||
+ | == Вариант 1 == | ||
+ | |||
+ | == Сохранение состояния iptables == | ||
<code> | <code> | ||
root@gate:~# iptables-save > /etc/iptables.rules | root@gate:~# iptables-save > /etc/iptables.rules | ||
</code> | </code> | ||
- | === Восстановление состояния iptables === | + | == Восстановление состояния iptables == |
<code> | <code> | ||
root@gate:~# iptables-restore < /etc/iptables.rules | root@gate:~# iptables-restore < /etc/iptables.rules | ||
</code> | </code> | ||
- | === Восстановление состояния iptables при загрузке === | + | == Восстановление состояния iptables при загрузке == |
<code> | <code> | ||
root@gate:~# cat /etc/network/interfaces | root@gate:~# cat /etc/network/interfaces | ||
Line 49: | Line 55: | ||
pre-up iptables-restore < /etc/iptables.rules | pre-up iptables-restore < /etc/iptables.rules | ||
... | ... | ||
+ | </code> | ||
+ | |||
+ | == Вариант 2 == | ||
+ | <code> | ||
+ | # apt install iptables-persistent | ||
+ | |||
+ | # netfilter-persistent save | ||
+ | </code> | ||
+ | |||
+ | ==== nftables ==== | ||
+ | |||
+ | * [[https://wiki.nftables.org/wiki-nftables/index.php/Performing_Network_Address_Translation_(NAT)|Performing Network Address Translation (NAT)]] | ||
+ | * https://wiki.debian.org/nftables | ||
+ | |||
+ | <code> | ||
+ | gate### apt install nftables | ||
+ | |||
+ | gate# nft add table nat | ||
+ | |||
+ | gate# nft 'add chain nat postrouting { type nat hook postrouting priority 100 ; }' | ||
+ | |||
+ | gate# nft add rule nat postrouting ip saddr 192.168.X.0/24 oif eth1 snat to 172.16.1.X | ||
+ | gate# nft add rule nat postrouting ip saddr 192.168.100+X.0/24 oif eth1 snat to 172.16.1.X | ||
+ | |||
+ | gate# nft list ruleset | ||
+ | |||
+ | gate# nft flush ruleset | ||
+ | |||
+ | gate# systemctl enable nftables.service --now | ||
+ | |||
+ | gate# cat /etc/nftables.conf | ||
+ | </code><code> | ||
+ | ... | ||
+ | table ip nat { | ||
+ | chain postrouting { | ||
+ | type nat hook postrouting priority srcnat; policy accept; | ||
+ | ip saddr 192.168.100+X.0/24 oif "eth1" snat to 172.16.1.X | ||
+ | } | ||
+ | } | ||
+ | </code><code> | ||
+ | gate# systemctl reload nftables.service | ||
</code> | </code> | ||
Line 91: | Line 138: | ||
iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 2222 -j DNAT --to-destination 192.168.X.10:22 | iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 2222 -j DNAT --to-destination 192.168.X.10:22 | ||
- | #iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 25 -j DNAT --to-destination 192.168.X.10:25 | ||
iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 53 -j DNAT --to-destination 192.168.X.10:53 | iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 53 -j DNAT --to-destination 192.168.X.10:53 | ||
iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p udp --dport 53 -j DNAT --to-destination 192.168.X.10:53 | iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p udp --dport 53 -j DNAT --to-destination 192.168.X.10:53 | ||
- | iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 80 -j DNAT --to-destination 192.168.X.10:80 | + | #iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 25 -j DNAT --to-destination 192.168.X.10:25 |
- | iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 143 -j DNAT --to-destination 192.168.X.10:143 | + | #iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 465 -j DNAT --to-destination 192.168.X.10:465 |
+ | #iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 587 -j DNAT --to-destination 192.168.X.10:587 | ||
+ | #iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 143 -j DNAT --to-destination 192.168.X.10:143 | ||
+ | #iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 80 -j DNAT --to-destination 192.168.X.10:80 | ||
+ | #iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p tcp --dport 5222 -j DNAT --to-destination 192.168.X.10:5222 | ||
- | #iptables -t nat -A PREROUTING -p tcp -s 192.168.100+X.0/24 --dport 80 -j REDIRECT --to-port 3128 | ||
#iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p udp --dport 5060 -j DNAT --to-destination 192.168.X.10:5060 | #iptables -t nat -A PREROUTING -i eth1 --destination 172.16.1.X -p udp --dport 5060 -j DNAT --to-destination 192.168.X.10:5060 | ||
Line 151: | Line 200: | ||
[gate:/etc] # /etc/rc.d/pf reload | [gate:/etc] # /etc/rc.d/pf reload | ||
</code> | </code> | ||
+ | |||
+ | ===== Поддержка протоколов приложений ===== | ||
+ | |||
+ | ==== tftp ==== | ||
+ | |||
+ | * [[https://unix.stackexchange.com/questions/579508/iptables-rules-to-forward-tftp-via-nat|iptables rules to forward tftp via NAT]] | ||
+ | |||
===== Мониторинг соединений ===== | ===== Мониторинг соединений ===== | ||
Line 175: | Line 231: | ||
[gate:~] # pfctl -F state | [gate:~] # pfctl -F state | ||
</code> | </code> | ||
+ | |||
+ | |||
+ | |||
===== Дополнительные материалы ===== | ===== Дополнительные материалы ===== | ||