Common commands and parameters of iptables

Source: Internet
Author: User
Iptables command: command to maintain the rule table: 1. (-N) create a new rule table 2. (-X) delete an empty Rule Table 3. (-P) change the default policy of the built-in rule table. 4. (-L) list rules in the rule table 5. (-F) clear the rules in the rule table. 6. (-Z) clear the rule table counters. rules in the management rule table: 1. (-A) add

IptablesCommand:

Command for maintaining the rule table:

1. (-N) create a new rule table

2. (-X) delete an empty rule table

3. (-P) change the default policy of the built-in rule table.

4. (-L) list rules in the rule table

5. (-F) clear the rules in the rule table

6. (-Z) clear the rule table counter

Manage rules in the rule table:

1. (-A) add the new rule to the rule table.

2. (-I) insert the new rule to a location in the rule table.

3. (-R) replace rules in the rule table

4. (-D) delete a rule from the rule table

When debugging iptables rules, you may need to modify your scripts repeatedly to implement some specific functions. we recommend that you add these lines to your script to prevent repeated rules:

# Clear all rules

Iptables-F-t filter

Iptables-X-t filter

Iptables-Z-t filter

Iptables-F-t nat

Iptables-X-t nat

Iptables-Z-t nat

# Set the default policy for the built-in rule table

Iptables-P INPUT ACCEPT

Iptables-P OUTPUT ACCEPT

Iptables-P FORWARD ACCEPT

Iptables-t nat-P PREROUTING ACCEPT

Iptables-t nat-P POSTROUTING ACCEPT

Iptables-t nat-P OUTPUT ACCEPT

The "-t" option is short for "-- table", which specifies the type of rule tables you want to operate on. The default value is filter.

The following example shows how to use some iptables options:

Specify the source address and target address

Specifies the source address of the rule operation-s or -- source or-src

Specify the target address of the rule operation-d or -- destination or-dst

Allow packets from 192.168.100.0/24 to pass through

Iptables-a input-s 192.168.100.0/24-d 0.0.0.0/0-j ACCEPT

The "-j" option is short for "-- jump". it specifies the specific processing method of data packets matching the rule, which may be ACCEPT, DROP, etc.

Inverse: add "!" before the parameter. No.

Accept all packets except 192.168.100.0/24

Iptables-a input-s! 192.168.100.0/24-d 0.0.0.0/0-jACCEPT

Protocol (TCP, UDP, or ICMP)

-P or -- protocol

Disable icmp protocol

Iptables-a input-p icmp-j DROP

Specified interface

Interface-I or -- in-interface

Interface-o or -- out-interface

Only the-I interface can be specified in the INPUT rule table, and only the-o interface can be specified in the OUTPUT rule table. the FORWARD table can specify these two interfaces.

Packets allowed to enter from eth1

Iptables-a input-I eth1-j ACCEPT

Packets that can be sent from the eth0 interface

Iptables-a output-o eth0-j ACCEPT

Forward the data packet that enters from eth1 and eth0 sends

Iptables-a forward-I eth1-o eth0-j ACCEPT

TCP/UDP extension

Specify the source port -- sport or -- source-port

Specify the destination port -- dport or -- destination-port

Allow access to tcp packets destined for port 21 from eth0

Iptables-a input-I eth0-p tcp -- dport 21-j ACCEPT

Allow access to udp packets destined for port 21 from eth0

Iptables-a input-I eth0-p udp -- dport 21-j ACCEPT

Status Match

-- State, state...

As we used in the preceding script, state is a list separated by commas (,), indicating the connection status to be matched. Valid status options include INVAILD, indicating that the connection to the data packet is unknown; ESTABLISHED, indicating that the connection to the data packet has been transmitted in two-way, that is, the connection has been ESTABLISHED; NEW, indicates that the packet request initiates a connection; RELATED indicates that the packet initiates a new connection, but this connection is RELATED to an existing connection, for example: there is a RELATED relationship between the FTP data transmission connection and the control connection.

Disable the NEW status from eth0, that is, prohibit NEW access requests from eth0

Iptables-a input-I eth0-m state -- state NEW-j DROP

"-M" is short for "-- match. -The m option introduces the iptables state extension module, such as the mac extension module, which implements permission control rules based on the MAC address of the host Nic:

Iptables-a input-p tcp-m mac -- mac-source 01: 02: 03: 04: 05: 06-j ACCEPT

The extension module is a feature of the iptables system. it makes iptables a scalable security architecture. You can learn from here

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.