Iptables usage summary

Source: Internet
Author: User
Recently, the iptables command was used in a job, mainly for Port Ing. I checked a lot of information on the internet. although there are many examples, I still had to understand it for several days. Here we will summarize the learned knowledge. The following are my personal opinions. (I. basic knowledge (1) Introduction to iptables recently used in a job IptablesCommand, mainly for Port Ing, a lot of information on the Internet, although there are many examples, but still a few days to understand. Here we will summarize the learned knowledge. The following are my personal opinions. (Some of them are summarized from the network and are constantly improving ...)
I. basic knowledge
(1) Introduction to iptables
Iptables is an excellent nat + firewall tool in Linux. iptables operates more than 2.4 of the netfilter kernel, so it requires more than 2.4 of the linux kernel. Its functionality and security are far more powerful than those of its predecessors ipforward and ipchains. iptables generally works on layer 2, Layer 3, and layer 4 of OSI.
First, let's briefly introduce the general workflow of netfilter, that is, how to process a packet (or group, packet) when it reaches the network interface (NIC) of linux. Then we will introduce how to use iptables to change or control this packet.
Does netfilter have three internal tables: filter, nat, and mangle. Each table has a Chain ).
In the filter table, the firewall function table defines three chains: INPUT, FORWARD, and OUTPUT (three chains defining packet entry, forwarding, and OUTPUT ), the operation on this filter table is an important means to implement the firewall function.
Nat table (Network AddressTranslation Network address translation) defines three links: PREROUTING, POSTROUTING, and OUTPUT, which are mainly used for address translation and port forwarding.
The mangle table is a custom table, including the filters and various chains in the nat table above. it allows us to perform some custom operations. The chains in the mangle table exist. netfilter has a priority in the packet processing process (we do not know much about this table. generally, we cannot use this mangle table. we will not introduce it here ).
The following figure clearly depicts the netfilter packet processing process (from the internet, I do not know who the author is, I would like to pay close attention to it !).

Note: For more information, see the following article:
Http://blog.csdn.net/xingliyuan22/article/details/9047767

(2) Basic operations
#/Etc/init. d/iptables start
# Service iptables start
#/Etc/init. d/iptables stop
#/Etc/init. d/iptables restart

# Service iptables restart
#/Etc/init. d/iptables save

II. Application
(1) Port ing
Premise: A Linux dual-Nic computer IP address: 192.168.1.2, 202.201.1.2, and the intranet has a Web server 192.168.1.6
Requirement: enter 202.201.1.2: 8000 in the internet browser to access port 80 of 192.168.1.6.
Detailed configuration:
Note: Before that, enable the port forwarding service.
Net. ipv4.ip _ forward = 1 in the/etc/sysctl. conf configuration file is 0 by default.

You can get the root permission modification, or use the following command:
Echo "1">/etc/sysctl. conf/net. ipv4.ip _ forward
① Clear original rules
# Iptables-F-t nat
# Iptables-F-t filter
#/Etc/init. d/iptables save
② Configure ing rules
# Iptables-t nat-a prerouting-d 202.201.1.2-t tcp -- dport 8000-t tcp-j DNAT-to -- destination 192.168.1.6: 80
# Iptables-t nat-a postrouting-d 192.168.1.6-p tcp -- dport 80-j SNAT -- to 192.168.1.2
# Iptables-a forward-o eth0-d 192.168.1.6-p tcp -- dport 80-jACCEPT
# Iptables-a forward-I eth0-s192.168.1.6-p tcp -- dport 80-jACCEPT
# Iptables-a forward-m state -- state ESTABLISHED, RELATED-jACCEPT
③ Save and start the service
#/Etc/init. d/iptables save
# Service iptables restart note: There is a package flow in the/proc/net/ip_conntrack file
(2) IP ing
Note: In Linux, append an IP address to a network card. in Linux, a network card can have multiple IP addresses.
LINUX dual-Nic, with the IP addresses eth0: 192.168.1.2/24 and eth1: 192.168.2.2/24 respectively
Requirement: map IP: 192.168.2.5 to 192.168.1.2 on LINUX
① Append a linux eth0 URL
# Ip address add 1.1.1.1/30 brd + dev eth0
② Iptables configuration
# Iptables-a forward-s 192.168.12.95-j ACCEPT
# Iptables-a forward-d 192.168.12.95-j ACCEPT
# Iptables-t nat-a prerouting-d 202.201.1.2-j DNAT-to -- destination 192.168.1.6

# Iptables-t nat-a postrouting-d 192.168.1.6-j SNAT -- to-source 192.168.1.2
#/Etc/init. d/iptables save
# Service iptables restart

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.