CentOS配置iptables防火墙

这几天在virpus上买了个VPS练练手 先从基础的iptables配置折腾起吧

[root@yip ~]# service iptables status
Table: filter
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 
4    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 8 
5    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state ESTABLISHED 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination   

这是配置好的防火墙... 具体配置如下:

首先先确认iptables是安装的:

[root@yip ~]# yum install -y iptables
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.unl.edu
 * extras: centos.mirror.facebook.net
 * updates: mirrors.kernel.org
Setting up Install Process
Package iptables-1.4.7-11.el6.x86_64 already installed and latest version
Nothing to do

如果提示Nothing to do就证明已经安装好了 接下来使用 iptables -L -n命令可以获取到目前防火墙设置的规则,就是最上面的代码。如果你想清楚规则,可以使用以下命令:

#首先在清除前要将policy INPUT改成ACCEPT,表示接受一切请求。
#这个一定要先做,不然清空后可能会悲剧
iptables -P INPUT ACCEPT
 
#清空默认所有规则
iptables -F
 
#清空自定义的所有规则
iptables -X
 
#计数器置0
iptables -Z

规则的具体配置如下:

#允许来自于lo接口的数据包
#如果没有此规则,你将不能通过127.0.0.1访问本地服务,例如ping 127.0.0.1
iptables -A INPUT -i lo -j ACCEPT
 
#ssh端口22
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
 
#FTP端口21
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
 
#web服务端口80
iptables -A INPUT -p tcp --dport 80 -j ACCEP
 
#tomcat
iptables -A INPUT -p tcp --dport xxxx -j ACCEP
 
#mysql
iptables -A INPUT -p tcp --dport xxxx -j ACCEP
 
#允许icmp包通过,也就是允许ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
 
#允许所有对外请求的返回包
#本机对外请求相当于OUTPUT,对于返回数据包必须接收啊,这相当于INPUT了
iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
 
#如果要添加内网ip信任(接受其所有TCP请求)
iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
 
#过滤所有非以上规则的请求
iptables -P INPUT DROP

最后保存规则,系统会在/etc/sysconfig/iptables下生成文件:

[root@yip ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

然后设置开机自动启动吧:

chkconfig iptables on

设置参考的是这位仁兄的写的文章:http://www.woxplife.com/articles/404.html 觉得写得蛮清晰的,我比较懒,哈哈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值