Iptables and NAT settings

Source: Internet
Author: User
1. for example, to change the source IP address of all packets from 192.168.1.0/24 to 1.2.3.4: iptables-tnat-APOSTROUTING-s192.168.1.0/24-oeth0-jSNAT -- to1.2.3.4 note that, the system does not perform SNAT until the data packet is sent. 1. Source NAT (SNAT)

For example, change the source IP address of all data packets from 192.168.1.0/24 to 1.2.3.4:

  Iptables-T nat-a postrouting-s 192.168.1.0/24-oeth0-j SNAT -- to 1.2.3.4

It should be noted that the system does not perform SNAT until the packets are sent out during routing and overconsideration.

There is a special case of SNAT which ip spoofing is called Masquerading. we recommend that you use it when using dial-up Internet access,

Or use it when the valid IP address is not fixed. For example:

 # Iptables-t nat-a postrouting-s 192.168.1.0/24-oppp0-j MASQUERADE

We can see that there is no need to explicitly specify the source IP address and other information at this time.

2. destination SNAT (DNAT)

For example, change the destination IP address of all data packets from 192.168.1.0/24 to 1.2.3.4:

 Iptables-t nat-a prerouting-s 192.168.1.0/24-ieth1-j DNAT -- to 1.2.3.4

Note that the system performs DNAT before routing and excessive consideration.

There is a special situation of DNAT, that is, the so-called Redirection, which is equivalent to changing the destination IP address of the qualified data packet to the IP address of the network interface when the data packet enters the system. It is usually used to form a transparent proxy with the squid Configuration. if the squid listening port is 3128, we can use the following statement to import data from 192.168.1.0/24, the packet destined for port 80 is redirected to the squid listener.

  Modify port 80 to 3128:

Iptables-t nat-a prerouting-I eth1-p tcp-s192.168.1.0/24 -- dport 80-j REDIRECT -- to-port 3128

  Port ing: (Remote Desktop)

Iptables-t nat-APREROUTING-p tcp -- dport 3389-I eth0-j DNAT -- to-destination 192.168.0.1: 3389

  Example

1. use dial-up with LAN to access the Internet

Small enterprises and Internet cafes use dial-up networks to access the Internet. Usually, proxies are used. However, considering costs and protocol support, we recommend that you use ip spoofing to enable Internet access in the area network.

After the kernel is successfully upgraded, install iptables and execute the following script:

# Load related modules

Modprobe ip_tables

Modprobe ip_nat_ftp

# Mask ip addresses

Iptables-t nat-a postrouting-s 192.168.1.0/24-oppp0-j MASQUERADE

2. ip ing

Assume that an ISP provides Internet access to the campus. to facilitate management, the IP addresses assigned by the ISP to the campus users are all pseudo IP addresses, but some users require that their WWW servers be set up to publish information. We can bind multiple valid ip addresses to the external network card of the firewall, and then forward the packets sent to one of the ip addresses to the internal WWW server of a user through ip ING, then, the response packet of the internal WWW server is disguised as the package sent by the legal IP address.

Let's assume the following scenarios:

The ip address assigned by this ISP to A www server is:

Pseudo ip: 192.168.1.100

Real ip: 202.110.123.100

The ip address assigned by this ISP to B's www server is:

Pseudo ip: 192.168.1.200

Real ip: 202.110.123.200

The IP addresses of the linux firewall are:

Intranet interface eth1: 192.168.1.1

Internet interface eth0: 202.110.123.1

Then, we bind the real ip addresses allocated to units A and B to the firewall's internet interface and run the following command as root:

Ifconfig eth0 add 202.110.123.100 netmask 255.255.255.0

Ifconfig eth0 add 202.110.123.200 netmask 255.255.255.0

After the kernel is successfully upgraded, install iptables and execute the following script:

# Load related modules

Modprobe ip_tables

Modprobe ip_nat_ftp

First, perform the destination NAT (DNAT) on all packets received by the firewall with the destination ip address 202.110.123.100 and 202.110.123.200 ):

Iptables-a prerouting-I eth0-d 202.110.123.100-j DNAT -- to192.168.1.100

Iptables-a prerouting-I eth0-d 202.110.123.200-j DNAT -- to192.168.1.200

Next, perform Source NAT (SNAT) on the packets received by the firewall with the source IP addresses 192.168.1.100 and 192.168.1.200 ):

Iptables-a postrouting-o eth0-s 192.168.1.100-j SNAT -- to 202.110.123.100

Iptables-a postrouting-o eth0-s 192.168.1.200-j SNAT -- to 202.110.123.200

In this way, all data packets whose destination ip addresses are 202.110.123.100 and 202.110.123.200 will be forwarded to 192.168.1.100 and 192.168.1.200 respectively, and all data packets from 192.168.1.100 and 192.168.1.200 will be disguised as 202.110.123.100 and expires, respectively, thus, ip ING is realized.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.