LINUX.ORG.RU

Port-forwarding на Astra Linux.

 , ,


0

2

Доброго времени суток, форумчане)

Имеется ПК1 (Astra) с двумя интерфейсами (внутренний ip1, физически enp1s0 и внешний ip2, физически enp2s0), а также другой ПК2 с одним интерфейсом (внутренний ip3). То есть ip1 напрямую видит ip3. На ip3 крутится веб-интерфейс на 80 порту.

Необходимо: при обращении из внешней сети (условно из ip4 с ПК3) на ip2 перенаправить пакеты на ip3 и получить доступ к веб-интерфейсу (то есть ip4 напрямую не видит ip3, но должен иметь доступ к веб-интерфейсу).

Здесь /etc/sysctl.conf строка net.ipv4.ip_forward=1 раскомментирована.

Прописал следующие правила, не помогло:

sudo iptables -A FORWARD -i enp2s0 -o enp1s0 -j ACCEPT
sudo iptables -t nat -A PREROUTING -p tcp -d ip3 --dport 80 -j DNAT --to-destination ip2:80
sudo iptables -t nat -A POSTROUTING -p tcp --sport 80 --dst ip2 -j SNAT --to-source ip3:80

Вывод iptables (ip3 = 172.16.0.5, ip2 = 192.168.0.50):

sudo iptables -t nat -L POSTROUTING
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  172.17.0.0/16        anywhere            
MASQUERADE  all  --  172.24.0.0/16        anywhere            
MASQUERADE  tcp  --  172.24.0.2           172.24.0.2           tcp dpt:zabbix-trapper
MASQUERADE  tcp  --  172.17.0.2           172.17.0.2           tcp dpt:9000
MASQUERADE  tcp  --  172.24.0.3           172.24.0.3           tcp dpt:zabbix-agent
SNAT       tcp  --  anywhere             192.168.0.50         tcp spt:http to:172.16.0.5:80


sudo iptables -t nat -L PREROUTING
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DOCKER     all  --  anywhere             anywhere             ADDRTYPE match dst-type LOCAL
DNAT       tcp  --  anywhere             172.16.0.5           tcp dpt:http to:192.168.0.50:80


sudo iptables -L               
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
DOCKER-USER  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain DOCKER (2 references)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             172.24.0.2           tcp dpt:zabbix-trapper
ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:9000
ACCEPT     tcp  --  anywhere             172.24.0.3           tcp dpt:zabbix-agent

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination         
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere            

Chain DOCKER-ISOLATION-STAGE-2 (2 references)
target     prot opt source               destination         
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere            

Chain DOCKER-USER (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere  

До добавления правила FORWARD:

sudo iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  22M 2231M DOCKER-USER  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
  22M 2231M DOCKER-ISOLATION-STAGE-1  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  *      docker0  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    0     0 DOCKER     all  --  *      docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  docker0 !docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  docker0 docker0  0.0.0.0/0            0.0.0.0/0           
  11M 1384M ACCEPT     all  --  *      br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
 778K   43M DOCKER     all  --  *      br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0           
9833K  805M ACCEPT     all  --  br-0cd24d8b612a !br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0           
 245K   15M ACCEPT     all  --  br-0cd24d8b612a br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain DOCKER (2 references)
 pkts bytes target     prot opt in     out     source               destination         
 533K   28M ACCEPT     tcp  --  !br-0cd24d8b612a br-0cd24d8b612a  0.0.0.0/0            172.24.0.2           tcp dpt:10051
    0     0 ACCEPT     tcp  --  !docker0 docker0  0.0.0.0/0            172.17.0.2           tcp dpt:9000
    0     0 ACCEPT     tcp  --  !br-0cd24d8b612a br-0cd24d8b612a  0.0.0.0/0            172.24.0.3           tcp dpt:10050

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DOCKER-ISOLATION-STAGE-2  all  --  docker0 !docker0  0.0.0.0/0            0.0.0.0/0           
9833K  805M DOCKER-ISOLATION-STAGE-2  all  --  br-0cd24d8b612a !br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0           
  22M 2231M RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain DOCKER-ISOLATION-STAGE-2 (2 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      docker0  0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0           
9833K  805M RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain DOCKER-USER (1 references)
 pkts bytes target     prot opt in     out     source               destination         
  22M 2231M RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0 


Последнее исправление: Sanbar (всего исправлений: 2)

iptables -t nat -L POSTROUTING
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  172.17.0.0/16        anywhere            
MASQUERADE  all  --  172.24.0.0/16        anywhere            
MASQUERADE  tcp  --  172.24.0.2           172.24.0.2           tcp dpt:zabbix-trapper
MASQUERADE  tcp  --  172.17.0.2           172.17.0.2           tcp dpt:9000
MASQUERADE  tcp  --  172.24.0.3           172.24.0.3           tcp dpt:zabbix-agent

Последние 3 правила никогда не срабатывают.

Если показывать правила, то «iptables -nvxL» или iptables-save - это будет значительно информативнее.

Вставляй свои правила перед правилами докера если сети 172.17.0.0/16 и 172.24.0.0/16 не относятся к докеру.

vel ★★★★★
()
Ответ на: комментарий от vel

Попробую вставить свои правила выше. Но правила ведь созданы мной корректно? Подсети 172.17.0.0 и 172.24.0.0 как раз относятся к контейнерам докера (при разворачивании контейнеров из *yaml, создались эти правила и сети).

Sanbar
() автор топика
Ответ на: комментарий от vel

Вставка правил на 1 место - не помогло. Какие есть ещё идеи? Правила прикрепляю.

sudo iptables -nvxL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination         
       0        0 ACCEPT     all  --  enp2s0 enp1s0  0.0.0.0/0            0.0.0.0/0           
38982310 4047003079 DOCKER-USER  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
38982310 4047003079 DOCKER-ISOLATION-STAGE-1  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
       0        0 ACCEPT     all  --  *      docker0  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
       0        0 DOCKER     all  --  *      docker0  0.0.0.0/0            0.0.0.0/0           
       0        0 ACCEPT     all  --  docker0 !docker0  0.0.0.0/0            0.0.0.0/0           
       0        0 ACCEPT     all  --  docker0 docker0  0.0.0.0/0            0.0.0.0/0           
19809973 2514266541 ACCEPT     all  --  *      br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
 1386957 75974416 DOCKER     all  --  *      br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0           
17785385 1456762374 ACCEPT     all  --  br-0cd24d8b612a !br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0           
  446930 27093048 ACCEPT     all  --  br-0cd24d8b612a br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0           
       0        0 ACCEPT     all  --  enp2s0 enp1s0  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination         

Chain DOCKER (2 references)
    pkts      bytes target     prot opt in     out     source               destination         
  940022 48881116 ACCEPT     tcp  --  !br-0cd24d8b612a br-0cd24d8b612a  0.0.0.0/0            172.24.0.2           tcp dpt:10051
       0        0 ACCEPT     tcp  --  !docker0 docker0  0.0.0.0/0            172.17.0.2           tcp dpt:9000
       0        0 ACCEPT     tcp  --  !br-0cd24d8b612a br-0cd24d8b612a  0.0.0.0/0            172.24.0.3           tcp dpt:10050

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
    pkts      bytes target     prot opt in     out     source               destination         
       0        0 DOCKER-ISOLATION-STAGE-2  all  --  docker0 !docker0  0.0.0.0/0            0.0.0.0/0           
17785385 1456762374 DOCKER-ISOLATION-STAGE-2  all  --  br-0cd24d8b612a !br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0           
38982315 4047003331 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain DOCKER-ISOLATION-STAGE-2 (2 references)
    pkts      bytes target     prot opt in     out     source               destination         
       0        0 DROP       all  --  *      docker0  0.0.0.0/0            0.0.0.0/0           
       0        0 DROP       all  --  *      br-0cd24d8b612a  0.0.0.0/0            0.0.0.0/0           
17785385 1456762374 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain DOCKER-USER (1 references)
    pkts      bytes target     prot opt in     out     source               destination         
38982310 4047003079 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0
sudo iptables -t nat -nvxL POSTROUTING

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination         
       0        0 SNAT       tcp  --  *      *       0.0.0.0/0            192.168.0.50         tcp spt:80 to:172.16.0.5:80
       0        0 MASQUERADE  all  --  *      !docker0  172.17.0.0/16        0.0.0.0/0           
 3317958 216888284 MASQUERADE  all  --  *      !br-0cd24d8b612a  172.24.0.0/16        0.0.0.0/0           
       0        0 MASQUERADE  tcp  --  *      *       172.24.0.2           172.24.0.2           tcp dpt:10051
       0        0 MASQUERADE  tcp  --  *      *       172.17.0.2           172.17.0.2           tcp dpt:9000
       0        0 MASQUERADE  tcp  --  *      *       172.24.0.3           172.24.0.3           tcp dpt:10050
sudo iptables -t nat -nvxL PREROUTING

Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination         
       0        0 DNAT       tcp  --  *      *       0.0.0.0/0            172.16.0.5           tcp dpt:80 to:192.168.0.50:80
 3435283 185379301 DOCKER     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL
Sanbar
() автор топика
Ответ на: комментарий от Sanbar
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination         
       0        0 DNAT       tcp  --  *      *       0.0.0.0/0            172.16.0.5           tcp dpt:80 to:192.168.0.50:80

Правило ни разу не сработало. Наверно нужно задуматься почему так происходит...

И зачем ты SNAT в postrouting добавил?

vel ★★★★★
()
Последнее исправление: vel (всего исправлений: 1)
18 апреля 2023 г.