Iptables firewall settings in CentOS

Source: Internet
Author: User
This article mainly refers to the chapter on firewall and NAT settings in the private kitchen-server of laruence. 1. by default, iptables has at least three table filters, nat, and mangle. Filter: mainly related to packets entering the local machine. There are three chains: (1) INPUT: related to packets entering the local machine; (2) OUTPUT: for more information about packets sent from the local machine, refer to the firewall and NAT settings chapter in "private kitchen of laruence-Server.
1. when preset, IptablesThere are at least three table filters, nat, and mangle.
Filter: mainly related to packets entering the local machine. There are three chains:
(1) INPUT: related to packets entering the local machine;
(2) OUTPUT: related to packets sent from the local machine;
(3) FORWARD: it is not close to the local host and is mainly used to transmit packets to other hosts.
Nat: address translation. There are three chains:
(1) PREROUTING: rules performed before route determination;
(2) POSTROUTING: the rule to be performed after route determination;
(3) OUTPUT: related to the sent packets.
Mangle: it is related to the routing signs of special packets, and also has PREROUTING, OUTPUT, INPUT, and FORWARD.
2. iptables [-t tables] [-L] [-nv]
-T: followed by a table, such as nat or filter. the default filter
-L: list the current table rules.
-N: IP address and HOSTNAME are not checked.
-V: lists multiple types of information.
3. iptables-save and iptables save
Iptables-save outputs the defined rules
Iptables save writes the defined rules to/etc/sysconfig/iptables.
The two must be different, so don't confuse them. I used iptables-save as iptablessave and did not write it into the configuration file. Therefore, the firewall settings have never taken effect.
4. I think laruence has a good idea. he writes firewall rules into script files and writes the rules that allow and block them in two different files for maintenance.
Iptables. rule: set basic rules, including clearing firewall rules, loading modules, and setting services;
Iptables. deny: set to resist the entry of some malicious hosts;
Iptables. allow: Allows custom backdoor source hosts. Iptables. rule:
EXTIF = "eth0" # connect to the Internet network interface (192.168.1.0/24)
INIF = "eth1" # internal LAN connection interface
INNET = "192.168.100.0/24" # Internal Network
Export EXTIF INIF INNET
# Part 1: Firewall settings for the local machine
#1. set core network functions first
Echo "1">/proc/sys/net/ipv4/tcp_syncookies
Echo "1">/proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
For I in/proc/sys/net/ipv4/conf/*/{rp_filter, log_martians };
Echo "1"> $ I
Done
For I in/proc/sys/net/ipv4/conf/*/{accept_source_route, accept_redirects, send_redirects}; do
Echo "0"> $ I
Done
#2. clear rules, set preset policies, and enable lo and related settings
PATH =/bin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin :~ /Bin
Export PATH
# Clear custom firewall rules:
Iptables-F # clear all defined rules
Iptables-X # kill all user-defined chains
Iptables-Z # clear the count and traffic statistics of all chains
Iptables-p inputdrop # define the INPUT preset rule as DROP
Iptables-p output accept # define the default OUTPUT rule as ACCEPT
Iptables-p forwardaccept # define the FORWARD preset rule as ACCEPT
Iptables-a input-I lo-jACCEPT # sets lo as A trusted device, that is, the packets that access lo are accepted.
Iptables-a input-m state -- state RELATED, ESTABLISHED-j ACCEPT # It is passed as long as it is already ESTABLISHED or RELATED packets
Iptables-a input-m state -- state INVALID-jDROP # discard an INVALID packet
If [-f/usr/local/iptables. deny]; then
Sh/usr/local/iptables. deny # write firewall open rules
Fi
If [-f/usr/local/iptables. allow]; then
Sh/usr/local/iptables. allow # write firewall blocking rules
Fi
# Allow some types of ICMP packets to enter
Icmp_type = "0 3 3/4 11 12 14 16 18"
For typeicmp in $ icmp_type
Do
Iptables-a input-I $ EXTIF-p icmp -- icmp-type $ typeicmp-j ACCEPT
Done
# Allow certain services to enter, according to your environment settings
# Iptables-a input-p TCP-I $ EXTIF -- dport 21 -- sport1024: 65534-jACCEPT # FTP
# Iptables-a input-p TCP-I $ EXTIF -- dport 22 -- sport1024: 65534-jACCEPT # SSH
# Iptables-a input-p TCP-I $ EXTIF -- dport 25 -- sport1024: 65534-jACCEPT # SMTP
# Iptables-a input-p UDP-I $ EXTIF -- dport 53 -- sport1024: 65534-jACCEPT # DNS
# Iptables-a input-p TCP-I $ EXTIF -- dport 53 -- sport1024: 65534-j ACCEPT # DNS
Iptables-a input-p TCP-I $ EXTIF -- dport 80 -- sport1024: 65534-j ACCEPT # WWW
# Iptables-a input-p TCP-I $ EXTIF -- dport 110 -- sport 1024: 65534-j ACCEPT # POP3
# Iptables-a input-p TCP-I $ EXTIF -- dport 443 -- sport 1024: 65534-j ACCEPT # HTTPS
# Part 2: backend host settings
# Load some useful modules first
Modules = "ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrackip_conntrack_ftp ip_conntrack_irc"
For mod in $ modules
Do
Testmod = 'lsmod | grep "^ $ {mod}" | awk '{print $1 }''
If ["$ testmod" = ""]; then
Modprobe $ mod
Fi
Done
# Clear NAT table rules
Iptables-F-t nat
Iptables-X-t nat
Iptables-Z-t nat
Iptables-t nat-P PREROUTING ACCEPT
Iptables-t nat-P POSTROUTING ACCEPT
Iptables-t nat-P OUTPUT ACCEPT
# Set a bit IP address sharer for the backend host
If ["$ INIF "! = ""]; Then
Iptables-a input-I $ INIF-j ACCEPT
Echo "1">/proc/sys/net/ipv4/ip_forward
If ["$ INNET "! = ""]; Then
For innet in $ INNET
Do
Iptables-t nat-a postrouting-s $ INNET-o $ EXTIF-jMASQUERADE
Done
Fi
Fi
# If your MSN has been? O method ??, Or something? Station OK? Site not OK,
# It may be MTU ???, So you can? ⒌ @ A row? He canceled? Solution ??? MTU limit ??? Br/> # iptables-a forward-p tcp-m tcp -- tcp-flags SYN, rst syn-mtcpmss -- mss 1400: 1536-j TCPMSS -- clamp-mss-to-pmtu
# What is the backend LAN of the NAT server? Partner? Too many tasks? Br/> # iptables-t nat-a prerouting-p tcp-I $ EXTIF -- dport 80-j DNAT -- to-destination 192.168.100.200: 80
# Special features, including Windows? End desktop? Born ?? T, false? Desktop master? C? 192.168.1.100
# Iptables-t nat-a prerouting-p tcp-s 192.168.1.100 -- dport 6000-j DNAT -- to-destination 192.168.100.200
# Iptables-t nat-a prerouting-p tcp-s 192.168.1.100 -- sport 3389-j DNAT -- to-destination 192.168.100.200
# Save the rule
/Etc/init. d/iptables save
Iptables. allow:
#! /Bin/bash
Iptables-a input-I $ EXTIF-s 192.168.1.100-jACCEPT # set according to your own situation
Iptables. deny:
#! /Bin/bash
Iptables-a input-I $ EXTIF-s 192.168.1.103-jDROP # set according to your own situation
Finally, change all three files to 700 and the owner is root.
Here we only talk about some basic settings in the simple circumstances of the firewall. the larger the scale, the more problems to consider, and the more complicated the firewall configuration, this still needs to be learned.

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.