service iptables status
若提示为iptables:unrecognized service,则需要安装。
VPS.GL提供的Linux系统都已经安装了iptables防火墙。同时,为了适应广大使用Linux朋友的需要,这里就告诉大家安装步骤。
二、快速安装iptables
apt-get install iptables //Debian,Ubuntu使用此命令
yum install iptables //Linux,CentOS使用此命令
二、配置iptables规则
以下就在Linux系统下,配置iptables。使用Ubuntu的朋友参照此教程即可。为了避免本身的规则会对我们的配置造成干扰,我们先清除默认规则。
iptables –F
好了我们现在开始配置我们的规则。这里仅仅是指导大家配置一些常用的规则,并不是每一条规则都适合每一台VPS。因此,在配置的同时,应选择适合自己系统的iptables规则。(iptables配置文件在/etc/sysconfig/iptables)
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
#允许本地回环接口
iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
# 允许已建立的或相关连的通行
iptables -A OUTPUT -j ACCEPT
#允许所有本机向外的访问
iptables -A INPUT -p tcp –dport 22 -j ACCEPT
# 允许访问22端口(这是ssh端口,建议开启)
iptables -A INPUT -p tcp –dport 80 -j ACCEPT
#允许访问80端口(架设WEB必须开启)
iptables -A INPUT -p tcp –dport 1723 -j ACCEPT
#允许访问1723端口(架设VPN必须开启)
iptables -A INPUT -p tcp –dport 21 -j ACCEPT
iptables -A INPUT -p tcp –dport 20 -j ACCEPT
#允许FTP服务的21和20端口
注:因为根据防火墙的安全规则,禁用所有没有定义的端口。所以,如果大家还需要开启更多的端口,请按照上面的配置开启。
iptables -A INPUT -j REJECT
iptables -A FORWARD -j REJECT
#禁止其他未允许的规则访问
#拒绝某个ip访问:
-A INPUT -s ip -j DROP
service iptables save
或者/etc/rc.d/init.d/iptables save
#保存防火墙规则
service iptables restart
#重启防火墙才能使规则生效
三、查询修改及删除
iptables -L –n
#查询正在使用的规则
iptables -L -n –line-numbers
#将规则按数字序号显示方便删除
iptables -D INPUT 4
#删除第四条规则
四、设定开机启动,这一步很重要推荐设置。
chkconfig iptables on
linux下IPTABLES配置详解
Chain INPUT (policy ACCEPT)
target prot opt source destination
target prot opt source destination
target prot opt source destination
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 255
ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0
ACCEPT udp -- 0.0.0.0/0 224.0.0.251 udp dpt:5353
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:631
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:25
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
可以看出我在安装linux时,选择了有防火墙,并且开放了22,80,25端口.
Chain INPUT (policy ACCEPT)
target prot opt source destination
target prot opt source destination
target prot opt source destination
[root@tp ~]# iptables -X 清除预设表filter中使用者自定链中的规则
Chain INPUT (policy ACCEPT)
target prot opt source destination
target prot opt source destination
target prot opt source destination
上面的意思是,当超出了IPTABLES里filter表里的两个链规则(INPUT,FORWARD)时,不在这两个规则里的数据包怎么处理呢,那就是DROP(放弃).应该说这样配置是很安全的.我们要控制流入数据包
如果做了邮件服务器,开启25,110端口.
[root@tp ~]# iptables -A INPUT -p tcp --dport 25 -j ACCEPT
如果做了FTP服务器,开启21端口
IPTABLES -A OUTPUT -o lo -p all -j ACCEPT(如果是OUTPUT DROP)
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
target prot opt source destination
SNAT all -- 192.168.0.0/24 anywhere to:211.101.46.235
target prot opt source destination
[root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 172.16.0.0/12 -j DROP
[root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 192.168.0.0/16 -j DROP
如果我们想,比如阻止MSN,QQ,BT等的话,需要找到它们所用的端口或者IP,(个人认为没有太大必要)
[root@tp ~]# iptables -A INPUT -m state --state INVALID -j DROP
[root@tp ~]# iptables -A OUTPUT -m state --state INVALID -j DROP
[root@tp ~]# iptables-A FORWARD -m state --state INVALID -j DROP
允许所有已经建立的和相关的连接
[root@tp ~]# iptables-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@tp ~]# iptables-A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@tp ~]# /etc/rc.d/init.d/iptables save
这样就可以写到/etc/sysconfig/iptables文件里了.写入后记得把防火墙重起一下,才能起作用.
[root@tp ~]# service iptables restart
别忘了保存,不行就写一部保存一次.你可以一边保存,一边做实验,看看是否达到你的要求,