Article Title: iptables usage on red-flag Linux (2 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
WriteIptablesRule script
In the previous section, we learned aboutLinuxNetwork packets are stored inIptablesThe flow direction in the data link. This section will briefly introduce how to writeIptablesRules. The so-called rules refer to blocking different connections and data packets or allowing their whereabouts on a chain. All rules must be inserted to a specific chain to produce results. The syntax of the writing rule is as follows:
Iptables[-TTable] Command [match] [target | jump]
-TSpecifies the table name. Generally, you do not need to specify the table to be used becauseIptablesDefault usageFilterTable to execute all the commands.
CommandThe operation that the rule requires. For example, insert or delete a rule on a chain in the table.
MatchThis item describes the characteristics of the data packet that matches the rule. For example, specify the data packet sourceIPAnd network interfaces.
TargetIs the destination of the final packet.MatchByTargetProcessing.
The following describes several simpleIptablesRules:
Iptables? I input 1-I eth0? P all? J ACCEPT
The role of this script is in the defaultFilterTableINPUTInsert a filter rule at the first position of the chain-accept the rules that pass through the Ethernet Card0 (eth0)All protocol data packets. You can see from the linked list that a data packet passed into the local machine isFiterTableINPUTThe application can receive this packet.
Iptables? A input? I eth0? P icmp? Icmp-type 8? J DROP
InFilterTableINPUTThe last append rule of the chain-discardedEth0The Input Serial number is8OfIcmpProtocol packet, that isPing requestIn this way, other hosts will failPingConnect to the local machine.
ExploitationIptablesYou can also easily set up a gateway. IfEth0The external network is connected, whileEth1Connect to a LAN (192.168.0.0/24), Assuming that on the external network, the localIPIs202.113.13.91We only need one command to allow all hosts in the LAN to access the external network through the local machine.
Iptables? T nat? A postrouting? O eth0? S 192.168.0.0/24? J SNAT -- to 202.113.13.91
This script is actually directedNatTablePOSTROUTINGAddedSNATRules to disguise the source address of all packets to be forwarded from the LANIPTo external networks. In this way, the LAN gateway function is implemented.
Remember, before thatEcho 1>/proc/sys/net/ipv4/ip_forwardTo enableIPThe forwarding function.
SimilarlyIptablesYou can also easily implement port ing. For example, we need the external network to be able to access the LAN through the Local Machine192.168.0.110HostFTPThe script is as follows:
Iptables-t nat? A prerouting? I eth0? P tcp? D 202.113.13.91 -- dport 21? J DNAT? To 192.168.0.110
The script describes the rule that sets the external network to the Local Machine21Sent by port no.TCPAll data packetsDNATTo a host inside the LAN (of course you need to ensure that this host is enabled)FTPService), so that the external network can202.113.13.91ThisIPAccessFTPService.
We can see thatIptablesThere are a wide variety of parameters and a variety of Rule combinations, as long as you understand the flow of data packets, as well as the role of each table and chain, master them, you can easily build a network firewall with rich functions.
IptablesNetwork Control Function and Its Powerful. This article only introducesIptablesA small part of, hope to play a role, and want to learn more can be found on the official homepageIptablesYou can also find the Chinese translation of this guide at the URL below.
Http://man.lupaworld.com/content/network/iptables-tutorial-cn- 1.1.19 . Html
(E129)