Iptables port forwarding (1)

Source: Internet
Author: User

Iptables is a useful system tool.Iptables port forwarding.

First, run the following script:

# Filename gw. sh

PATH = $ PATH:/usr/sbin:/sbin

Echo "1">/proc/sys/net/ipv4/ip_forward

Modprobe ip_tables

Modprobe ip_nat_ftp

Modprobe ip_conntrack_ftp

Iptables-F INPUT

Iptables-F FORWARD

Iptables-f postrouting-t nat

Iptables-f prerouting-t nat

Iptables-P FORWARD DROP

Iptables-a forward-s 10.0.0.0/24-j ACCEPT

Iptables-a forward-I eth0-m state -- state ESTABLISHED, RELATED-j ACCEPT

Iptables-t nat-a postrouting-o eth0-s 10.0.0.0/24-j MASQUERADE

Iptables-t nat-a prerouting-p tcp-d 192.168.1.201 -- dport 80-j DNAT -- to 10.0.0.2: 80

Iptables-a forward-p tcp-d 192.168.1.201 -- dport 80-j ACCEPT

Iptables-a forward-p tcp-d 10.0.0.2 -- dport 80-j ACCEPT

Then access the service from the outside. No problem.

Then I changed the script:

# Filename gw. sh

PATH = $ PATH:/usr/sbin:/sbin

Echo "1">/proc/sys/net/ipv4/ip_forward

Modprobe ip_tables

Modprobe ip_nat_ftp

Modprobe ip_conntrack_ftp

Iptables-F INPUT

Iptables-F FORWARD

Iptables-f postrouting-t nat

Iptables-f prerouting-t nat

Iptables-P FORWARD DROP

Iptables-a forward-s 10.0.0.0/24-j ACCEPT

Iptables-a forward-I eth0-m state -- state ESTABLISHED, RELATED-j ACCEPT

Iptables-t nat-a postrouting-o eth0-s 10.0.0.0/24-j MASQUERADE

Iptables-t nat-a prerouting-p tcp-d 192.168.1.201 -- dport 8000-j DNAT -- to 10.0.0.2: 80

Iptables-a forward-p tcp-d 192.168.1.201 -- dport 8000-j ACCEPT

Iptables-a forward-p tcp-d 10.0.0.2 -- dport 80-j ACCEPT

#! /Bin/sh

PATH = $ PATH:/usr/sbin:/sbin

Echo "1">/proc/sys/net/ipv4/ip_forward

Modprobe ip_tables

Modprobe ip_nat_ftp

Modprobe ip_conntrack_ftp

Iptables-F INPUT

Iptables-F FORWARD

Iptables-f postrouting-t nat

Iptables-f prerouting-t nat

Iptables-P FORWARD DROP

Iptables-t nat-P PREROUTING DROP

Iptables-a forward-s 10.0.0.0/24-j ACCEPT

Iptables-a forward-I eth0-m state -- state ESTABLISHED, RELATED-j ACCEPT

Iptables-t nat-a postrouting-o eth0-s 10.0.0.0/24-j MASQUERADE

Iptables-t nat-a prerouting-p tcp-d 192.168.1.201 -- dport 81-j DNAT -- to 10.0.

0.2: 80

Iptables-a forward-p tcp-d 10.0.0.2 -- dport 80-j ACCEPT

Iptables-t nat-a prerouting-p tcp-d 192.168.1.201 -- dport 21-j DNAT -- to 10.0.

0.2: 21

Iptables-a forward-p tcp-d 10.0.0.2 -- dport 21-j ACCEPT


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.