Implement packet forwarding through iptables

Source: Internet
Author: User
Tags virtual environment

Lab environment:

System: centos6.2 linux2.6

VMware Virtual Environment Test

Three machines are available: A, B, and C.

A has a NIC: eth1: 1923168.66.225

B: One NIC: eth0: 192.168.66.10

C: One NIC: eth0: 192.168.66.11

For some reason, I want B to send data packets to C but not directly, and a can exchange data packets with B and C. At this time, I want, B sends the data packet to a, and then a forwards the data packet to C. At the same time, a changes the destination address of the forwarded data packet to B, when C receives the packet forwarded by A, it is like B directly sends it to C.

You can use netflter/iptables

Configure:

$ Iptables-T filter-a forward-O eth1-p tcp-J accept # Forward a data packet $ iptables-T filter-a forward-I eth1-P TCP-J accept # change the destination IP address of the data before the route entry to the IP address of C, you can use the 8080 $ iptables-T Nat-A prerouting-p tcp-m tcp -- dport 8080-j dnat -- to-destination 192.168.66.10: 8080 # change the source IP address of the data packet to a's IP address $ iptables-T Nat-A postrouting-D 192.168.66.10-p tcp-m tcp -- dport 8080-j snat -- to-Source 192.168.66.11

There is another one. Don't forget to enable the forwarding function of the Linux dual-nic.

To enable the packet forwarding function to take effect automatically after the system starts, you need to modify the/etc/sysctl. conf file and add a line:

# Controls IP packet forwarding
Net. ipv4.ip _ forward = 1

Then, you can write an application for testing and use tcpdump to capture packets on the C host.

Lab reference:

How to enable the forwarding function for Linux dual-nic

Http://blog.chinaunix.net/uid-24172143-id-2049790.html

Iptables port forwarding

Http://meridian.dyndns.org/any.thing/d04.html

Forward and Nat rules

Http://man.chinaunix.net/linux/redhat/rhel-sg-zh_cn-4/s1-firewall-ipt-fwd.html

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.