centos6.6防火墙配置shell

在安装完LAMP之后,因为没有开启防火墙,www的请求会被防火墙档掉,所以来配置下防火墙
这里是配置的防火墙的sheel,摘自鸟哥的linux私房菜
我注释掉了一些

#!/bin/bash
# 请先输入您的相关参数,不要输入错误了!
EXTIF='eth0' # 这个是可以连上 Public IP 的网络接口
INIF='' # 内部 LAN 的连接接口;若无则写成 INIF=""
INNET='' # 若无内部网域接口,请填写成 INNET=""
export EXTIF INIF INNET
# 第一部份,针对本机的防火墙设定!##########################################
# 1. 先设定好核心的网络功能:
#阻挡ddos攻击
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
#>>ping broadcast 地址时才取消 ping 的回应
#echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
#rp_filter丢弃不合理的封包,log_martians记录不合法的ip
for i in /proc/sys/net/ipv4/conf/*/{rp_filter,log_martians}
do
    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. 清除规则、设定默认政策及开放 lo 与相关的设定值
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH
#清除filter table规则
iptables -F
iptables -X
iptables -Z
#设置filter表的链的默认的规则
iptables -P INPUT   DROP
iptables -P OUTPUT  ACCEPT
iptables -P FORWARD ACCEPT
#设置信任装置
iptables -A INPUT -i lo -j ACCEPT
# ESTABLISHED:已经联机成功的联机状态;RELATED    :这个最常用!表示这个封包是与我们主机发送出去的封包有关
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

# 3. 启动额外的防火墙 script 模块
if [ -f /usr/local/virus/iptables/iptables.deny ]; then
      sh /usr/local/virus/iptables/iptables.deny
fi
if [ -f /usr/local/virus/iptables/iptables.allow ]; then
      sh /usr/local/virus/iptables/iptables.allow
fi
if [ -f /usr/local/virus/httpd-err/iptables.http ]; then
      sh /usr/local/virus/httpd-err/iptables.http
fi
#记录到指定端口的连接
iptables -A INPUT -p tcp --dport 22306 -i $EXTIF -m state --state NEW -m recent --set --name sshcount
#检测如果在30秒内的连接数达到3次则拒绝连接
iptables -A INPUT -p tcp --dport 22306 -i $EXTIF -m state --state NEW -m recent --update --seconds 30 --hitcount 4  --name sshcount -j DROP
#记录日志,前缀SSHOPEN:
iptables -A INPUT -p icmp --icmp-type 8 -m length --length 78 -m recent --set --name sshopen --rsource -j ACCEPT
#指定数据包78字节,包含IP头部20字节,ICMP头部8字节。
iptables -A INPUT -p tcp --dport 22306 --syn -m recent --rcheck --seconds 15000 --name sshopen --rsource -j ACCEPT
# 5. 允许某些服务的进入,请依照你自己的环境开启
# iptables -A INPUT -p TCP -i $EXTIF --dport  21 --sport 1024:65534 -j ACCEPT # FTP
#iptables -A INPUT -p TCP -i $EXTIF --dport  22306 --sport 1024:65534 -j ACCEPT # SSH
# iptables -A INPUT -p TCP -i $EXTIF --dport  25 --sport 1024:65534 -j ACCEPT # SMTP
# iptables -A INPUT -p UDP -i $EXTIF --dport  53 --sport 1024:65534 -j ACCEPT # DNS
# iptables -A INPUT -p TCP -i $EXTIF --dport  53 --sport 1024:65534 -j ACCEPT # DNS
 iptables -A INPUT -p TCP -i $EXTIF --dport  80 --sport 1024: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


# 第二部份,针对后端主机的防火墙设定!###############################
# 1. 先加载一些有用的模块
modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack 
ip_conntrack_ftp ip_conntrack_irc"
for mod in $modules
do
    testmod=`lsmod | grep "^${mod} " | awk '{print $1}'`
    if [ "$testmod" == "" ]; then
          modprobe $mod
    fi
done

# 2. 清除 NAT table 的规则吧!,没有用到,就先注释了
#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

# 3. 若有内部接口的存在 (双网卡) 开放成为路由器,且为 IP 分享器!
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 -j MASQUERADE
      done
  fi
fi
# 如果你的 MSN 一直无法联机,或者是某些网站 OK 某些网站不 OK,
# 可能是 MTU 的问题,那你可以将底下这一行给他取消批注来启动 MTU 限制范围
# iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss \
#          --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu

# 4. NAT 服务器后端的 LAN 内对外之服务器设定
# iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 \
#          -j DNAT --to-destination 192.168.1.210:80 # WWW

# 5. 特殊的功能,包括 Windows 远程桌面所产生的规则,假设桌面主机为 1.2.3.4
# iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4  --dport 6000 \
#          -j DNAT --to-destination 192.168.100.10
# iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4  --sport 3389 \
#          -j DNAT --to-destination 192.168.100.20

# 6. 最终将这些功能储存下来吧!
/etc/init.d/iptables save

另外,我是从windows下上传这个文件到linux上去执行的,各种报错,开始以为是真的语法有错误,后来才知道是文件格式的问题,用dos2unix转换一下就好了...

标签: linux, 系统服务, centos6, iptable

添加新评论