Search code examples
linuxiptablesnat

Iptables NAT one-to-one


I use linux serve Fedora 4.14.33-51.37.amzn1.x86_64. I want use NAT 1-to-1. For example Is it same problem My scheme is: My server has two network interfaces.

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc mq state UP group default qlen 1000
    link/ether 0a:8a:59:b9:2d:b8 brd ff:ff:ff:ff:ff:ff
    inet 172.10.1.72/25 brd 172.10.1.127 scope global eth0
       valid_lft forever preferred_lft forever
    inet 172.10.1.32/25 brd 172.10.1.127 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet 172.10.1.39/25 brd 172.10.1.127 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet 172.10.1.101/25 brd 172.10.1.127 scope global secondary eth0

eth1:
 inet 172.10.1.246/28 brd 172.10.1.255 scope global eth1

net.ipv4.ip_forward = 1

How can work NAT

(eth0)172.10.1.101 - (server1)192.168.1.10
(eth0)172.10.1.32 - (server2)192.168.1.11

and etc ... route table on NAT server

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.10.1.1      0.0.0.0         UG    0      0        0 eth0
0.0.0.0         172.10.1.241    0.0.0.0         UG    10001  0        0 eth1
10.0.0.0        172.10.1.1      255.0.0.0       UG    0      0        0 eth0
169.254.169.254 0.0.0.0         255.255.255.255 UH    0      0        0 eth0
172.10.1.0      0.0.0.0         255.255.255.128 U     0      0        0 eth0
172.10.1.240    0.0.0.0         255.255.255.240 U     0      0        0 eth1
192.168.1.0     172.10.1.241    255.255.255.240 UG    0      0        0 eth1

My currently iptables settings:

iptables -nvL

    Chain INPUT (policy ACCEPT 1726 packets, 115K bytes)
     pkts bytes target     prot opt in     out     source               destination
     1827  121K LOG        all  --  *      *       0.0.0.0/0            0.0.0.0/0            LOG flags 0 level 4

    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination
      664 55128 ACCEPT     all  --  eth0   eth1    0.0.0.0/0            0.0.0.0/0
        0     0 ACCEPT     all  --  eth1   eth0    0.0.0.0/0            0.0.0.0/0
        0     0 LOG        all  --  *      *       0.0.0.0/0            0.0.0.0/0            LOG flags 0 level 4

    Chain OUTPUT (policy ACCEPT 2123 packets, 668K bytes)
     pkts bytes target     prot opt in     out     source               destination
     2123  668K LOG        all  --  *      *       0.0.0.0/0            0.0.0.0/0            LOG flags 0 level 4

and

iptables -nvL -t nat
Chain PREROUTING (policy ACCEPT 36 packets, 2476 bytes)
 pkts bytes target     prot opt in     out     source               destination
    8   528 DNAT       all  --  eth0   *       0.0.0.0/0            172.10.1.101         to:192.168.1.10

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

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

Chain POSTROUTING (policy ACCEPT 202 packets, 14788 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 SNAT       all  --  *      eth0    192.168.1.10         0.0.0.0/0            to:172.10.1.101

When i try check my NAT server as telnet 172.10.1.101 4016 I have error

telnet: connect to address 172.10.1.101: Connection timed out

My server 192.168.1.10 listened port 4016.

On my NAT server I don't have logs.

But I try connect to another ip on my eth0 interface and saw in log
Jun 18 15:04:39 ip-172-10-1-72 kernel: [ 1245.059113] IN= OUT=eth0 SRC=172.10.1.39 DST=10.68.72.90 LEN=40 TOS=0x10 PREC=0x00 TTL=255 ID=57691 DF PROTO=TCP SPT=4016 DPT=47952 WINDOW=0 RES=0x00 ACK RST URGP=0 

10.68.72.90 it is my server before NAT. After added roles in iptables I don't able to ping my ip 172.10.1.101.


Solution

  • I had right rules.

    iptables -t nat -A POSTROUTING -s 192.168.1.11 -o eth0 -j SNAT --to-source 172.10.1.32
    iptables -t nat -A PREROUTING -d 172.10.1.32 -i eth0 -j DNAT --to-destination 192.168.1.11
    

    The problem was in AWS. I should turn of for NAT server "Check source" in interface menu. And all static routs must be in Route Table for subnet, where is my server located, not be on the servers 192.168.1.10 and 192.168.1.11.