Tag Archives: nat

IPTABLES: how to do nat?

Network Address Translation, very useful for me.

You should have at leat two network interfaces.

This is an example using MASQUERADE (SNAT) dump type.

The kernel IP forwarding should be enabled before begins to setup NAT rules.

To enable on debian’s based system

# echo 1 > /proc/sys/net/ipv4/ip_forward ;(temporarily)

or add to the /etc/sysctl.conf file :

net.ipv4.ip_forward = 1 ;(for permanent)

Accepting connections through FORWARD table:

# iptables -P FORWARD ACCEPT

so the rule:

# iptables -t nat -A POSTROUTING -o (interface that has the internet connection) -j MASQUERADE

Note: is very important to have two different network ranges like and know what would be the gateway on the clients.
eth0 > dhcp from ISP
eth1 > 172.20.0.0 (NAT range)

For permanent effect:

add to /etc/rc.local script this line:

/sbin/iptables -t nat -A POSTROUTING -o (interface with internet) -j MASQUERADE

MANUAL CLIENTS CONFIGURATION

note: you can set up a DHCP server and make the job easier.

ip > 172.20.0.10 (into NAT range) ; this address could be different
Subnet Mask > 255.255.255.0 ; this address could be different
Gateway > 172.20.0.1 ; Linux nat interface (in this case eth1 address)

DNS servers > you can set public DNS address like openDNS or internal ones. On the Linux system machine with NAT rules type:

# cat /etc/resolv.conf

and put the Linux’s system DNS. (may be a router with another NAT range with a DNS server configured)

Tip! You can make restrinctions to any service on the network introducing INPUT rules like into FORWARD table with the prefix:

# iptables -A FORWARD ...

DOne.