您的位置:首页 > 运维架构 > Linux

阿里云CentOS6上配置iptables

2015-08-02 14:03 357 查看
参考:http://blog.abv.cn/?p=50

阿里云CentOS6默认没有启动iptables

1、检查iptables状态

[root@iZ94jj63a3sZ ~]# service iptables status
iptables: Firewall is not running.
[root@iZ94jj63a3sZ ~]#


  说明iptables没有启动。

  如果没有安装,则使用如下命令安装

[root@iZ94jj63a3sZ ~]# yum install -y iptables


  启动iptables

[root@iZ94jj63a3sZ ~]# service iptables start


 查看当前iptables的配置情况

[root@iZ94jj63a3sZ ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination


2、清楚默认的防火墙规则

#首先在清除前要将policy INPUT改成ACCEPT,表示接受一切请求。
#这个一定要先做,不然清空后可能会悲剧
[root@iZ94jj63a3sZ ~]# iptables -P INPUT ACCEPT

#清空默认所有规则
[root@iZ94jj63a3sZ ~]# iptables -F

#清空自定义的所有规则
[root@iZ94jj63a3sZ ~]# iptables -X

#计数器置0
[root@iZ94jj63a3sZ ~]# iptables -Z


3、配置规则

#允许来自于lo接口的数据包
#如果没有此规则,你将不能通过127.0.0.1访问本地服务,例如ping 127.0.0.1
[root@iZ94jj63a3sZ ~]# iptables -A INPUT -i lo -j ACCEPT

#ssh端口22
[root@iZ94jj63a3sZ ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT

#web服务端口80
[root@iZ94jj63a3sZ ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT

#允许icmp包通过,也就是允许ping
[root@iZ94jj63a3sZ ~]# iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

#允许所有对外请求的返回包
#本机对外请求相当于OUTPUT,对于返回数据包必须接收啊,这相当于INPUT了
[root@iZ94jj63a3sZ ~]# iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT

#过滤所有非以上规则的请求
[root@iZ94jj63a3sZ ~]# iptables -P INPUT DROP


4、保存

  首先使用 iptables -L -n 检查一下配置是否正确

[root@iZ94jj63a3sZ ~]# iptables -L -n


  确认无误后保存

[root@iZ94jj63a3sZ ~]# service iptables save


  添加到开机自启动

[root@iZ94jj63a3sZ ~]# chkconfig iptables on
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: