This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
сервис_nat [2019/07/17 11:24] val [Ubuntu (iptables)] |
сервис_nat [2025/10/20 07:20] (current) val [nftables] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Сервис NAT ====== | ====== Сервис NAT ====== | ||
| - | * [[http://stackoff.ru/ip/|Определение внутреннего IP-адреса за NAT и VPN через WebRTC]] | + | * [[https://stackoff.ru/ip/|Определение внутреннего IP-адреса за NAT и VPN через WebRTC]] |
| ===== Трансляция на основе адреса отправителя ===== | ===== Трансляция на основе адреса отправителя ===== | ||
| Line 8: | Line 8: | ||
| === Заполнение таблицы nat (eth1 - внешний интерфейс) === | === Заполнение таблицы nat (eth1 - внешний интерфейс) === | ||
| <code> | <code> | ||
| + | debian11_12# 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 | ||
| + | |||
| + | * Сервис SalesPlatform Vtiger CRM Russian [[Сервис SalesPlatform Vtiger CRM Russian#Информация по установке в LXC]] | ||
| + | |||
| + | <code> | ||
| + | gate### apt install nftables | ||
| + | |||
| + | gate# man nft | ||
| + | |||
| + | gate# nft add table nat | ||
| + | |||
| + | gate# nft list tables | ||
| + | |||
| + | gate# nft 'add chain nat postrouting { type nat hook postrouting priority srcnat ; }' | ||
| + | |||
| + | 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 | ||
| + | #oifname "eth1" masquerade | ||
| + | } | ||
| + | } | ||
| + | </code><code> | ||
| + | gate# systemctl reload nftables.service | ||
| </code> | </code> | ||
| Line 91: | Line 145: | ||
| 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 111: | Line 167: | ||
| </code> | </code> | ||
| + | ==== Debian/Ubuntu (nftables) ==== | ||
| + | |||
| + | <code> | ||
| + | root@gate:~# cat /etc/nftables.conf | ||
| + | </code><code> | ||
| + | ... | ||
| + | table ip nat { | ||
| + | ... | ||
| + | chain prerouting { | ||
| + | type nat hook prerouting priority dstnat; policy accept; | ||
| + | ip daddr 172.16.1.X tcp dport 2222 dnat to 192.168.X.10:22 | ||
| + | ip daddr 172.16.1.X udp dport 53 dnat to 192.168.X.10 | ||
| + | ip daddr 172.16.1.X tcp dport 53 dnat to 192.168.X.10 | ||
| + | |||
| + | ip daddr 172.16.1.X tcp dport { 25, 80, 143, 465, 587, 5222 } dnat to 192.168.X.10 | ||
| + | |||
| + | ip daddr 172.16.1.X udp dport { 10000-20000 } dnat to 192.168.X.10 | ||
| + | ip daddr 172.16.1.X udp dport { 5060 } dnat to 192.168.X.10:5060 | ||
| + | # ip daddr 172.16.1.X udp dport { 6050 } dnat to 192.168.X.10:5060 | ||
| + | |||
| + | ip daddr 172.16.1.X tcp dport 5061 dnat to 192.168.X.10 | ||
| + | ip daddr 172.16.1.X udp dport 4569 dnat to 192.168.X.10 | ||
| + | |||
| + | } | ||
| + | } | ||
| + | </code><code> | ||
| + | root@gate:~# systemctl restart nftables.service | ||
| + | </code> | ||
| ==== FreeBSD (pf) ==== | ==== FreeBSD (pf) ==== | ||
| <code> | <code> | ||
| Line 151: | Line 235: | ||
| [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 266: | ||
| [gate:~] # pfctl -F state | [gate:~] # pfctl -F state | ||
| </code> | </code> | ||
| + | |||
| + | |||
| + | |||
| ===== Дополнительные материалы ===== | ===== Дополнительные материалы ===== | ||