CentOS 6.x 配置iptables

来源 https://www.cnblogs.com/chillax1314/p/7976067.html

iptables -P INPUT DROP---设置INPUT链默认规则为丢弃所有流量
iptbales有四表五链,raw,mangle,nat,filter(默认)表;五链:INPUT,OUTPUT,FORWARD,PREROUTING,POSTROUTING

系统默认的iptables规则

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p icmp -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited
iptables -A FORWARD -j REJECT --reject-with icmp-host-prohibited

系统默认的ip6tables规则

ip6tables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
ip6tables -A INPUT -p ipv6-icmp -j ACCEPT
ip6tables -A INPUT -i lo -j ACCEPT
ip6tables -A INPUT -m state --state NEW -m udp -p udp --dport 546 -d fe80::/64 -j ACCEPT
ip6tables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
ip6tables -A INPUT -j REJECT --reject-with icmp6-adm-prohibited
ip6tables -A FORWARD -j REJECT --reject-with icmp6-adm-prohibited

vim iptables.sh 拷贝一下内容,具体按实际生产需求修改,此脚本默认丢弃INPUT流量,放行OUTPUT链流量,丢弃FORWARD流量;

#!/bin/bash

#清空iptables 规则
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X

#允许本机访问本机
iptables -A INPUT -i lo -j ACCEPT

#添加黑名单方法

#iptables -I INPUT -s 192.168.0.1 -j DROP #屏蔽单个IP的命令

#iptables -I INPUT -s 192.168.1.0/24 -j DROP #封IP段即从192.168.1.0到192.168.1.254的命令

#允许172.16.20.0 192.168.2.0/24 ssh访问进来
iptables -A INPUT -s 172.16.20.0/24 -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -s 192.168.2.0/24 -p tcp --dport 22 -j ACCEPT

#允许https通行
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

#允许http访问服务器,已经存在的链接不断开
iptables -A INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT

#允许已经建立的链接继续通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#允许ping

#iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT

#设置INPUT链默认丢弃
iptables -P INPUT DROP

#设置OUTPUT链默认允许
iptables -P OUTPUT ACCEPT

#设置默认丢弃FORWARD链
iptables -P FORWARD DROP

#防止各种攻击
iptables -A FORWARD -p icmp -icmp-type echo-request -m limit -limit 1/s -j ACCEPT
iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT

iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --syn -m limit --limit 12/s --limit-burst 24 -j ACCEPT

iptables -A INPUT -p tcp --dport 80 -m recent --name BAD_HTTP_ACCESS --update --seconds 60 --hitcount 20 -j REJECT 
iptables -A INPUT -p tcp --dport 80 -m recent --name BAD_HTTP_ACCESS --set -j ACCEPT

#允许本机访问192.168.7.2的ftp服务器

iptables -A INPUT -s 192.168.7.2 -p tcp -m multiport --dports 20,21 -m state --state NEW -j ACCEPT
iptables -A INPUT -s 192.168.7.2 -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
iptables -A INPUT -s 192.168.7.2 -p tcp -m tcp --dport 30001:31000 -j ACCEPT

#保存iptables
service iptables save 或者用iptables-save > /etc/sysconfig/iptables
chkconfig iptables on ——添加开机启动
service iptables restart——重启iptables服务,其实不需要重启立即生效

注意:如果在生产上ssh端口不是默认的22,会导致执行了此脚本后无法连接服务器,为了防止此类事情发生,可以定期执行一个脚本取消iptables运行
crontab -e
/10 * /etc/init.d/iptables stop——每十分钟执行停止iptables服务

查看iptables记录信息

日志信息查看
more /proc/net/nf_conntrack

=============== End

CentOS 6.x 配置iptables的更多相关文章

  1. CentOS 7 服务器配置--配置iptables防火墙

    #检查服务器是否安装了iptables systemctl status iptables.service #安装iptables yum install -y iptables #更新iptable ...

  2. centos 7 配置iptables

    环境:阿里云ECS.centos 7 一.防火墙配置 不知道为什么,云主机没有开启firewall 或iptables,记录一下配置iptables防火墙的步骤 1.检测并关闭firewall sys ...

  3. CentOS 配置 iptables 配合 ss

    转自:http://www.jianshu.com/p/28b8536a6c8a 环境: CentOS 6 shadowsocks iptables 在安装了ss-bash后,ss-bash每添加一次 ...

  4. CentOS下配置iptables防火墙 linux NAT(iptables)配置

    CentOS下配置防火墙 配置nat转发服务CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/syscon ...

  5. CentOS配置iptables规则并使其永久生效

    1. 目的 最近为了使用nginx,配置远程连接的使用需要使用iptable是设置允许外部访问80端口,但是设置完成后重启总是失效.因此百度了一下如何设置永久生效,并记录.  2. 设置 2.1 添加 ...

  6. CentOS 6.3配置PPTP VPN的方法

    1.验证ppp 用cat命令检查是否开启ppp,一般服务器都是开启的,除了特殊的VPS主机之外. [root@localhost1 /]# cat /dev/ppp cat: /dev/ppp: No ...

  7. Centos下安装配置LAMP(Linux+Apache+MySQL+PHP)

    Centos下安装配置LAMP(Linux+Apache+MySQL+PHP)   关于LAMP的各种知识,还请大家自行百度谷歌,在这里就不详细的介绍了,今天主要是介绍一下在Centos下安装,搭建一 ...

  8. CentOS 6.5配置nfs服务

    CentOS 6.5配置nfs服务 网络文件系统(Network File System,NFS),一种使用于分散式文件系统的协议,由升阳公司开发,于1984年向外公布.功能是通过网络让不同的机器.不 ...

  9. CentOS安装与配置LNMP

    本文PDF文档下载:http://www.coderblog.cn/doc/Install_and_config_LNMP_under_CentOS.pdf 本文EPUB文档下载:http://www ...

随机推荐

  1. ffmpeg编译错误,提示找不到相应的shared libraries :libavdevice.so.53

    解决方法:需要配置响应的环境变量,以便能找到响应的lib库 vi   /etc/ld.so.conf 加入   /usr/local/lib 执行  sudo  ldconfig

  2. Spark累加器(Accumulator)

    一.累加器简介 在Spark中如果想在Task计算的时候统计某些事件的数量,使用filter/reduce也可以,但是使用累加器是一种更方便的方式,累加器一个比较经典的应用场景是用来在Spark St ...

  3. gfs下载文件较大,可以分区域分变量下载

       一.下载 所有字段的GFS预报(大致有325个字段),1度的文件有1G多,0.5度的3.5G左右. 若每天下载0.6.12.18四个发布点的数据,那是很费时费力的.而且经常会被IDS/IPS设备 ...

  4. python声明类时继承不继承object类的区别

    不加的叫做经典类或旧式类,但是现在python3的类都默认是继承了object的,所以可写可不写 下面举个例子说明: 首先说明下__class__功能与用法: __class__功能和type()函数 ...

  5. 泡泡一分钟:Visual Odometry Using a Homography Formulation with Decoupled Rotation and Translation Estimation Using Minimal Solutions

    张宁 Visual Odometry Using a Homography Formulation with Decoupled Rotation and Translation Estimation ...

  6. python for 无限循环

    class Infinit: def __iter__(self): return self def __next__(self): return None for i in Infinit(): p ...

  7. Qt编写自定义控件64-垂直时间轴

    一.前言 垂直时间轴控件,主要用来描述企业发展历程大事件,或者软件版本迭代历史等,通过时间节点和事件描述来直观的展示发展的过程,一般在web网页或者app中经常看到此类控件,尤其是公司的官网关于公司部 ...

  8. 【445】Markdown Syntax

    ref: Markdown基本语法 ref: Markdown Guide ref: Markdown Cheatsheet ref: Markdown Tutorial Lists Basic Sy ...

  9. 123457123456#2#----com.MC.DishuGame368----前拼后广--儿童打地鼠Game-mc2222222

    com.MC.DishuGame368----前拼后广--儿童打地鼠Game-mc

  10. clientHeight,offsetHeight,scrollHeight迷一样的三个值

    https://blog.csdn.net/qq_39083004/article/details/78498178 https://www.imooc.com/article/17571  推荐 o ...