I will explain iptables's detailed Chinese manual in one sentence

Source: Internet
Author: User

ArticleReproduced to: http://www.opsers.org/2010_01_21.html

Overview
Use iptables-ADC to specify the chain rules.-A Add-D Delete-C modify

Iptables-[ri] chain rule num rule-specification [Option]
Use iptables-Ri to specify the sequence of rules

Iptables-D chain rule num [Option]
Deletes a specified rule.
Iptables-[lfz] [Chain] [Option]
Use iptables-lfz chain name [Option]

Iptables-[NX] Chain
Use-NX to specify a chain

Iptables-P chain target [Options]
Default target of the specified chain

 

Iptables-e old-chain-name New-chain-name
-E old chain name New Chain name
Replace old chain names with new chain names
Description
Iptalbes is used to set, maintain, and check the IP packet filtering rules of the Linux kernel.
Different tables can be defined. Each table contains several internal chains and user-defined chains. Each chain is a rule list that matches the corresponding package: each rule specifies how the matching package should be processed. This is called a 'target' (target) and can also jump to a user-defined chain in the same table.

Targets
The firewall rules specify the features and targets of the checked packets. If the package does not match, it is sent to the next rule check in the chain. If yes, the next rule is determined by the target value. the target value can be a user-defined chain name or a specific value, such as accept [pass], drop [delete], queue [queue], or return [Return].
Accept indicates that the package passes. Drop indicates dropping this package. Queue indicates to pass this package to the user space. Return indicates that the matching of the chain is stopped and the rule of the previous chain starts again. If a built-in chain is reached, or the rule of the built-in chain is return, the fate of the package will be determined by the target specified by the chain criterion.

Tables
There are currently three tables (which table is the current table depends on the Kernel configuration option and the current module ).
-T table
This option specifies the table of matching packages to be operated by the command. If the kernel is configured to automatically load modules, if the modules are not loaded, the system will try to load the appropriate modules (for this table. These tables are as follows: filter, which is the default table and contains the built-in chain input (the packet to be processed) and forword (the packet to be processed) and output (processing locally generated packages ). Nat. When this table is queried, it indicates that a new connection packet is generated, which consists of three built-in chains: prerouting), output (the local package before the route is modified), and postrouting (the package to be modified ). The mangle table is used to modify the specified package. It has two built-in rules: prerouting (the package before the route is modified) and output (the local package before the route is modified ).
Options
These options that can be recognized by iptables can be different types.

Commands
These options specify to execute a specific action: If there is no other rule under the command line, this row can only specify one option. for long-Format Commands and option names, you only need to ensure that the iptables command can be distinguished from other options.
-A-append
Add one or more rules at the end of the selected chain. When the source (Address) or/and destination (Address) are converted to multiple addresses, this rule is added to all possible addresses (combinations.

-D-delete
Delete one or more rules from the selected chain. This command can be used to specify the deleted rule as the serial number in the chain (the first serial number is 1) or as the rule to be matched.

-R-replace
Replaces a rule from the selected chain. If the source (Address) or/and destination (Address) are converted to multiple addresses, this command fails. The rule sequence number starts from 1.

-I-insert
Insert one or more rules to the selected Chain Based on the given rule sequence number. Therefore, if the rule number is 1, the rule will be inserted into the chain header. This is the default method when no rule serial number is specified.

-L-list
Displays all the rules of the selected chain. If no link is selected, all links are displayed. It can also be used with the Z option, and the chain will be automatically listed and zeroed. Precise output is affected by other parameters.

-F-flush
Clear the selected chain. This means that all rules are deleted one by one.

-- Z-zero
Clears the packets and byte counters of all links. It can be used with-L to view the counter before clearing. See the previous article.

-N-New-chain
Create a new user-defined Chain Based on the given name. This must ensure that no chain with the same name exists.

-X-delete-chain
Deletes a specified user-defined chain. This chain must not be referenced. If it is referenced, you must delete or replace the relevant rules before deleting it. If no parameter is provided, this command will try to delete each non-built chain.
-P-Policy
Set the target rule of the chain.

-E-Rename-chain
Rename the specified Chain Based on the name given by the user. This is only a modifier and does not affect the structure of the entire table. The targets parameter provides a valid target. Rules can be used only for non-user-defined chains, and both built-in and user-defined chains cannot be the target of rules.

-H help.
Help. The syntax of the current command is very short.

Parameters
Parameters
The following parameters constitute detailed rules, such as the add, delete, replace, append, and check commands.

-P-protocal [!] Protocol
Protocol for rule or package check (package to be checked. The specified protocol can be either one or all of TCP, UDP, or ICMP, or a numerical value, representing one of these protocols. You can also use the Protocol name defined in/etc/protocols. Add "! "Indicates the opposite rule. The number 0 is equivalent to all. Protocol all matches all protocols, and this is a time-saving option. When combined with the check command, all can be disabled.
-S-source [!] Address [/mask]
Specifies the source address, which can be the host name, network name, and clear IP address. The mask can be a network mask or a clear number. specify the number of "1" on the left of the network mask. Therefore, the value of the mask is 24 or 255.255.255.0. Add "! "Indicates that the opposite address segment is specified. Flag -- SRC is short for this option.

-D -- destination [!] Address [/mask]
Specify the target address. For more information, see the description of the-s flag. The flag-DST is short for this option.

-J -- jump target
-J target jump
Specify the target of the rule, that is, what to do if the package matches. The target can be a user-defined chain (not where this rule is located), a private built-in target that will immediately determine the fate of the package, or an extension (see extensions below ). If the rule option is ignored, the matching process will not affect the package, but the rule counter will increase.

-I-in-interface [!] [Name]
I-access (network) interface [!] [Name]
This is the optional entry name received by the package through this interface, and the package is received through this interface (the package entered in the chain input, forword, and prerouting ). Before the Interface Name, use "! "After description, it refers to the opposite name. If the interface name is followed by "+", all interfaces starting with this interface name will be matched. If this option is ignored, it is assumed to be "+", then any interface will be matched.

-O -- Out-interface [!] [Name]
-O -- output interface [name]
This is the optional exit name sent by the package through this interface, and the package is output through this port (the package sent in the chain forward, output, and postrouting ). Before the Interface Name, use "! "After description, it refers to the opposite name. If the interface name is followed by "+", all interfaces starting with this interface name will be matched. If this option is ignored, it is assumed as "+", then all arbitrary interfaces will be matched.

[!] -F, -- Fragment
[!] -F -- multipart
This means that in the fragmented package, the rule only asks for the second and later parts. Since then, because the source port or target port (or ICMP type) of the packet cannot be determined, such packets cannot match any rules specified for matching them. If "! "The description is used before the"-F "sign to indicate the opposite.

Other options
Other options
You can also specify the following additional options:

-V -- verbose
-V -- details
Detailed output. This option allows the LIST command to display the interface address, rule option (if any), and TOS (type of service) mask. The package and byte counters will also be displayed. K, M, and g (prefix) are used to indicate 1000, 1,000,000, and 1,000,000,000 times respectively (but refer to the-x flag to change it). For addition, insert, delete, and replace commands. This prints detailed information about one or more rules.

-N -- numeric
-N -- number
Digital output. The IP address and port are printed in numbers. By default,ProgramTry to display the host name, network name, or service (as long as it is available ).

-X-exact
-X-precision
Extended number. Display the exact value of the package and byte counter, instead of the approximate number expressed in K, M, G. This option can only be used for the-l command.

-- Line-Numbers
When a rule is displayed in the list, add a row number before each rule to match the rule's position in the chain.

Match extensions
Corresponding extension
Iptables can use some extension packages that match the module. The following are the extension packages included in the basic package, and most of them can be added in front! To indicate the opposite.

TCP
When -- protocol TCP is specified and other matching extensions are not specified, these extensions are loaded. It provides the following options:

-- Source-Port [!] [Port [: Port]
Specifies the source port or port range. This can be the service name or port number. Format port: You can specify the port range. If the first port number is ignored, the default value is "0". If the end port number is ignored, the default value is "65535". If the second port number is greater than the first port number, it is switched. You can use the -- Sport alias for this option.

-- Destionation-Port [!] [Port: [port]
Specify the target port or port range. This option can be replaced by the -- dport alias.

-- TCP-flags [!] Mask comp
Matches the specified TCP tag. The first parameter is the tag we want to check, a list separated by commas, and the second parameter is a tag table separated by commas, which must be set. Mark as follows: syn ack fin rst urg psh all none. Therefore, this command: iptables-a forward-p tcp -- TCP-flags SYN, ack, FIN, rst syn only matches the packets whose SYN flag is set but the ACK, fin, and RST tags are not set.

[!] -- SYN
Only TCP packets whose SYN bit is set and ACK and fin bit are cleared. These packages are used to send requests during TCP connection initialization. For example, a large number of such packages will block the TCP connection when an interface is blocked, and the outgoing TCP connection will not be affected. This is equal to -- TCP-flags SYN, RST, and ACK syn. If "-- SYN" is preceded "! "Mark, indicating the opposite.

-- TCP-Option [!] Number
Match the TCP option.

UDP
When Protocol UDP is specified and other matching extensions are not specified, these extensions are loaded and provide the following options:

-- Source-Port [!] [Port: [port]
Specifies the source port or port range. For details, see the description of the TCP extended -- source-port option.

-- Destination-Port [!] [Port: [port]
Specify the target port or port range. For details, see the -- destination-port option of TCP extension.

ICMP
When Protocol ICMP is specified and other matching extensions are not specified, the extension is loaded. It provides the following options:
-- ICMP-type [!] Typename
This option allows you to specify the ICMP type, which can be a numeric ICMP type, or an ICMP type name displayed by the command iptables-p icmp-H.

Mac
-- Mac-source [!] Address
Match the physical address. The format must be XX: XX. Note that it is only valid for packets from the Ethernet device that enter the prerouting, forword, and input chains.

Limit
This module matching flag matches with a tag bucket filter at a certain speed. It is used with log targets to provide a limited number of logins. when the limit value is reached, the rules that use this extension package will match. (Unless "! "Mark)

-- Limit rate
Maximum average matching rate: The value can be assigned to units such as '/second','/minute ','/hour', or '/Day'. The default value is 3/hour.

-- Limit-burst number
Maximum initial number of packages to be matched: if the limit specified earlier has not reached this value, the total number is increased by 1. The default value is 5.

Multiport
This module matches a group of source or target ports. You can specify up to 15 ports. It can only be used with-p tcp or-p udp.

-- Source-Port [port [, port]
If the source port is one of the given ports

-- Destination-Port [port [, port]
If the target port is one of the given ports, it matches

-- Port [port [, port]
If the source port and destination port are the same and are the same as a given port, they match.
Mark
This module matches the netfilter tag field (you can set mark as below ).

-- Mark value [/mask]
Match those unsigned tag values (if the mask is specified, a logical tag is added to the mask before comparison ).

Owner
This module generates a local package to match different features of the package creator. It can only be used for output chains, and even some packets (such as ICMP ping responses) may not have owners, so they will never match.

-- UID-owner userid
If a valid user ID is provided, it matches the package generated by the process.

-- GID-owner groupid
If a valid group ID is provided, it matches the package generated by the process.

-- Sid-owner seessionid
Match the packets generated by the process according to the given session group.

State
This module allows the connection trace status of the access package when used in conjunction with the connection trace.

-- State
The State is a comma-separated list of matched connection statuses. The possible status is: Invalid indicates that the package is an unknown connection, established indicates a two-way transmission connection, and new indicates that the package is a new connection. Otherwise, the package is not transmitted in two directions, related indicates that the package starts from a new connection, but is connected with an existing connection, such as FTP data transmission or an ICMP error.

Unclean
This module has no options, but it tries to match those strange and uncommon packages. In the lab.

ToS
This module matches the eight-bit ToS (service type) field (that is, included in the priority) of the IP package header ).

-- TOS
This parameter can be a standard name (view the list with iptables-m tos-h) or a value.

Target extensions
Iptables can be used to expand the target module: The following are included in the Standard Edition.

Log
Enable the kernel record for the matching package. After this option is set in the rule, the Linux kernel prints some information about all matching packages (such as the IP header field) through printk ).
-- Log-level
Record level (numeric or see syslog. conf (5 )).
-- Log-Prefix prefix
Add a specific prefix before the record information: a maximum of 14 letters are used to distinguish it from other information in the record.

-- Log-TCP-Sequence
Record the TCP serial number. If records can be read by users, this poses a security risk.

-- Log-TCP-Options
Record the options from the TCP header.
-- Log-IP-Options
Record the options from the IP packet header.

Mark
Set the netfilter flag value of the package. Only applicable to mangle tables.

-- Set-mark

Reject
As a response to the matched package, an error package is returned: the package is the same as the drop in other cases.

This target applies only to input, forward, and output chains, and user-defined chains that call these chains. These options control the returned error Package features:

-- Reject-with Type
The type can be ICMP-net-unreachable, ICMP-host-unreachable, ICMP-Port-nreachable, ICMP-proto-unreachable, ICMP-net-prohibited, or ICMP-host-prohibited, this type will return the corresponding ICMP error message (default: Port-unreachable ). The echo-reply option is also allowed. It can only be used to generate a ping response in the rule that specifies the ICMP ping packet. Finally, the TCP-reset option can be used in the input chain or in the Rules called by the self-input chain. Only the TCP protocol is matched: a tcp rst packet will be returned.
ToS
Set the first eight-bit TOS of the IP package. It can only be used for mangle tables.

-- Set-tos TOS
You can use a numeric TOS value or iptables-j tos-h to view the list of valid TOS names.
Mirror
This is a test demonstration target. It can be used to convert the source address and target address in the IP address header field, and then transfer the package. It is only applicable to input, forward, and output chains, and user-defined chains that only call them.

SNAT
This target only applies to the postrouting chain of the NAT table. It specifies to modify the source address of the package (all packages will be affected after this connection) and stop checking the rule. It includes the following options:

-- To-source [-] [: Port-port]
You can specify a single new IP address, a range of IP addresses, or a port range (only in rules specifying-p tcp or-p udp ). If no port range is specified, ports lower than 512 in the source port will be placed as other ports lower than 512; ports between 512 and 1024 will be placed below 1024, other ports are placed as 1024 or above. If possible, the port is not modified.

-- To-destiontion [-] [: Port-port]
You can specify a single new IP address, a range of IP addresses, or a port range (only in rules specifying-p tcp or-p udp ). If no port range is specified, the target port is not modified.

Masquerade
Only used for the postrouting chain of the NAT table. It can only be used to dynamically obtain IP (dial-up) connections: If you have a static IP address, you must use SNAT. Disguise is equivalent to setting an image for the IP address of the interface through which the packet is sent. When the interface is closed, the connection will be terminated. This is because the current dial-up may not be the same interface address (all established connections will be closed later ). It has an option:

-- To-ports [-port>]
Specify the source port range to be used, and overwrite the default SNAT source address selection (see the above ). This option applies only to rules with-p tcp or-p udp specified.

Redirect
Only applicable to the prerouting and output chains of NAT tables, and user-defined chains that only call them. It modifies the target IP address of the package to send the package to the machine itself (the locally generated package is placed at 127.0.0.1 ). It contains an option:

-- To-ports []
Specified destination port or port range: If this parameter is not specified, the target port is not modified. Only rules with-p tcp or-p udp can be specified.

Diagnostics
Diagnosis
Different error messages are printed as standard errors: ExitCode0 indicates that it is correct. If the command line parameter is incorrect or the command line parameter is abused, error code 2 is returned, and other error codes are returned as 1.

Bugs
Bugs
Check is not implemented (yet ).
Check is not completed yet.

Compatibility with ipchains
Compatibility with ipchains
Iptables is very similar to Rusty Russell's ipchains. The main difference is that the input chain is only used to enter the package of the local host, and the output is only used to generate the package from the local host. Therefore, each packet passes through only one of the three chains. The previously forwarded packet passes through all three chains. The other major difference is that-I references the incoming interface;-O references the output interface, both of which are applicable to the packets that enter the forward chain. When the default filter table is used with the optional extension module, iptables is a pure package filter. This can greatly reduce the previous obfuscation of IP camouflage and packet filtering, so the following options are different:
-J masq
-M-S
-M-l
There are several different links in iptables.

See also
See
Iptables-howto provides detailed iptables usage and describes netfilter-hacking-howto in essence.

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.