Common iptable settings in CentOS

Source: Internet
Author: User

Basic format

1. iptable [-t table] command option [name] matching condition [-j Action]
2. Common command options are as follows:
-A [append] adds A rule at the end of the specified connection
-D [delete] deletes a rule in a specified connection. It can be matched by rule number or rule content.
-I [insert] inserts a new rule, which is at the beginning by default.
-R [replace] replaces a rule.
-L [list]: list all rules.
-F [flush] clears all rules
-N [new] Custom rule connection
-X [-- delete-chain] deletes a custom rule connection.
-P [policy]: set the default policy
-N [numeric] is displayed in numbers, such as displaying ip addresses but not hostnames.
-V [verbose] displays details
-V [version]: view the version of iptable.
-- Line-number: view the rule connection. The list number is displayed.
-Z: Clear the counter value


Example

# Iptable-t filter-F [clear all rules in the filter table]
# Iptable-t filter-Z [clear the counter value in the filter table]
# Iptable-t filter-X [clear custom connections in the filter table]
# Iptable-t filter-p input drop [set the default INPUT connection policy to DROP]
# Iptable-t filter-P OUTPUT DROP
# Iptable-t filter-P FORWROD DROP
# Iptable-t filter-a input-p tcp-j ACCEPT [add A rule that allows the entry of tcp packets at the end of the INPUT connection]
# Iptable-t filter-r input 1-p tcp-j DROP [replace the 1st rules of INPUT connection to reject tcp packet entry]
# Iptable-t nat-vnL -- line-number [list all rules in the nat table in a detailed and numeric format]
# Iptable-t nat-d postrouting 1 [delete 1st rules in the nat table POSTROUTING connection]


Condition match

1. Protocol matching: the protocol used to check the data packet is allowed if it complies with the rules. Otherwise, it is rejected. The allowed protocol names are in the/etc/protocols file.
Common protocols include tcp, udp, icmp, ip, and all. -P protocol name]
# Iptable-I INPUT-p icmp-j REJECT [REJECT icmp packets entering the firewall itself]
# Iptable-a forward-p udp-j ACCEPT [allow forwarding of all udp packets]
2. Address match: used to check whether the IP address of the data packet complies with the rules, including the source and destination addresses. -S source address,-d destination address]
# Iptable-a forward-s 10.0.0.0/8-j DROP [Reject packet forwarding from 10.0.0.0/8 network segment]

# Iptable-a forward-d 80.0.0.0/8-j DROP [reject forwarding packets destined for 80.0.0.0/8 network segments]

3. Port match: used to check the tcp or udp ports of the data packet. You must use the-sport source port and-dport destination port together with the-p protocol type]
# Iptables-a forward-s 10.0.0.0/8-p tcp -- dport 80-j ACCEPT [allow forwarding of packets from 10.0.0.0/8 CIDR blocks and the destination port is 80]
# Iptables-I FORWARD-s 10.0.0.0/8-p tcp -- sport 21-j ACCEPT [allow forwarding packets from 10.0.0.0/8 CIDR blocks and the source port is 21]
4. Interface match: used to check whether data packets are allowed to enter or exit from the interface of the firewall.
# Iptables-a forward-I eth0-s 10.0.0.0/8-p tcp -- dport 80-j ACCEPT
Allow forwarding from eth0, from 10.0.0.0/8 network segment, use tcp protocol, the destination port is 80 packets]
# Iptables-a input-I eth0-s 80.0.0.0/8-j DORP [deny access from eth0, packets from 80.0.0.0/8]
5. SNAT conversion: generally used when linux acts as a gateway server
SNAT can only be used for the POSTROUTING connection of the nat table to convert the source address. It should be used in combination with --.
# Iptables-t nat-a postrouting-s 10.0.0.0/8-j SNAT -- to 202.106.1.1
[Convert the source address of all data packets from 10.0.0.0/8 to 202.106.1.1]
# Iptables-t nat-a postrouting-I eth0-s 80.0.0.0/8-p tcp -- dport 25-j SNAT -- to 202.106.1.1

6. DNAT conversion: it can only be used for PREROUTING connections in the nat table to convert the destination address or port.
# Iptables-t nat-a prerouting-I eth1-d 202.106.1.1-p tcp -- dport 80-j DNAT -- to 10.0.0.10
[Enter from eth1, the destination address is 202.106.1.1, and the destination address of the data packet whose destination port is 80 is changed to 10.0.0.1 using the tcp protocol]
7. MASQUERADE: camouflage, a special case of SNAT.
# Iptables-t nat-a postrouting-s 10.0.0.0/8-o eth1-j MASQUERADE
[Disguise the source address of the packet from the 10.0.0.0/8 segment and from eth1 as the eth1 interface address]

Expansion Module

1. Match by source MAC address
# Iptables-t filter-a forward-m -- mac-source 00: 02: b2: 03: a5: f6-j DROP
Reject packet forwarding from this MAC address]
2. Match by multiple ports or continuous ports
20: all ports after 20
Port 20 to port 100
: 20 indicates all ports earlier than 20
-M multiport [-- prots, -- sports, -- dports]
# Iptables-a input-p tcp-m multiport -- dports, 80-j ACCEPT [multi-port matching]
# Iptables-a input-p tcp-dport 20:-j ACCEPT
# Iptables-a input-p tcp-sport 20: 80-j ACCEPT
# Iptables-a input-p tcp-sport: 80-j ACCEPT
3. You can also match data packets by data packet rate and status.
-M limit -- limit matching rate, for example,-m limit -- limit 50/s-j ACCEPT
-M state -- state such as-m state -- state INVALID, RELATED-j ACCEPT


Other common operations

Iptables-F/* clear all rules */
Iptables-a input-p tcp -- dport 22-j ACCEPT/* allow packets to enter from port 22 */
Iptables-a output-p tcp -- sport 22-m state -- state ESTABLISHED-j ACCEPT/* packets allowed to enter port 22 are returned */
Iptables-a output-p udp -- dport 53-j ACCEPT/* domain name resolution port, generally not open */
Iptables-a input-p udp -- sport 53-j ACCEPT/* domain name resolution port, generally not open */
Iptables-a input-s 127.0.0.1-d 127.0.0.1-j ACCEPT/* allow local access to the local machine */
Iptables-a output-s 127.0.0.1-d 127.0.0.1-j ACCEPT
Iptables-a input-p tcp-s 0/0 -- dport 80-j ACCEPT/* allow all IP addresses to access port 80 */
Iptables-a output-p tcp -- sport 80-m state -- state ESTABLISHED-j ACCEPT
Iptables-save>/etc/sysconfig/iptables/* save configuration */
Iptables-L/* Display the iptables list */

Vi/etc/sysconfig/iptables

Add:
-A RH-Firewall-1-INPUT-m state -- state NEW-m tcp-p tcp -- dport 80-j ACCEPT
-A RH-Firewall-1-INPUT-m state -- state NEW-m tcp-p tcp -- dport 2222-j ACCEPT

Or

Enable ports 80, 22, and 3306 on the CentOS firewall

/Sbin/iptables-I INPUT-p tcp-dport 80-j ACCEPT
/Sbin/iptables-I INPUT-p tcp-dport 22-j ACCEPT
/Sbin/iptables-I INPUT-p tcp-dport 3306-j ACCEPT
Then save:
/Etc/rc. d/init. d/iptables save

View opened ports:

/Etc/init. d/iptables status

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.