ubuntu系统:
apt-get install iptables #如果默认没有安装,请运行此命令安装防火墙
# whereis iptables #查看系统是否安装防火墙可以看到:
iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz #表示已经安装iptables
apt-get install iptables #如果默认没有安装,请运行此命令安装防火墙
# iptables -L #查看防火墙配置信息,显示如下:
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
# vi /etc/iptables.rules
添加以下内容(备注:80是指web服务器端口,3306是指MySQL数据库链接端口,22是指SSH远程管理端口.)
*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:syn-flood - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
-A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
-A syn-flood -j REJECT --reject-with icmp-port-unreachable
COMMIT
# iptables-restore < /etc/iptables.rules #使防火墙规则生效
# vi /etc/network/if-pre-up.d/iptables #创建文件,添加以下内容,使防火墙开机启动
#!/bin/bash
iptables-restore < /etc/iptables.rules
# chmod +x /etc/network/if-pre-up.d/iptables #添加执行权限
# iptables -L -n查看规则是否生效.
eg.
# Generated by iptables-save v1.4.21 on Sat Aug 11 23:44:51 2018 *filter :INPUT DROP [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [3122:409937] :syn-flood - [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT -A INPUT -p tcp -m tcp --dport 20 -j ACCEPT #-A INPUT -s 10.42.1.103 -p tcp -m tcp --dport 22 -j REJECT -A INPUT -s 10.42.1.0/24 -p tcp -m tcp --dport 22 -j ACCEPT #-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT -A INPUT -s 10.42.1.0/24 -p tcp -m tcp --dport 3306 -j ACCEPT #-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood -A INPUT -j REJECT --reject-with icmp-host-prohibited -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN -A syn-flood -j REJECT --reject-with icmp-port-unreachable COMMIT # Completed on Sat Aug 11 23:44:51 2018 # Generated by iptables-save v1.4.21 on Sat Aug 11 23:44:51 2018 *nat :PREROUTING ACCEPT [2177:722593] :INPUT ACCEPT [464:157009] :OUTPUT ACCEPT [1877:120700] :POSTROUTING ACCEPT [1877:120700] COMMIT # Completed on Sat Aug 11 23:44:51 2018
Chain INPUT (policy ACCEPT)
target prot opt source destination
target prot opt source destination
target prot opt source destination
[root@tp ~]# iptables -X 清除预设表filter中使用者自定链中的规则
查看防火墙规则列表: # iptables -L -n
设定预设规则:
#iptables -P INPUT DROP
注:如果你是远程SSH登陆的话,当你输入上述命令回车的时候就应该掉了.因为你没有设置任何规则.
怎么办,去本机操作呗!
# iptables -P OUTPUT ACCEPT
# iptables -P FORWARD DROP
上面的意思是,当超出了IPTABLES里filter表里的两个链规则(INPUT,FORWARD)时,不在这两个规则里的数据包怎么处理呢,那就是DROP(放弃).应该说这样配置是很安全的.我们要控制流入数据包
如果做了邮件服务器,开启25,110端口.
如果做了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 -t nat -A PREROUTING -d 211.101.46.253 -j DROP
[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
允许所有已经建立的和相关的连接(必须配置否则httpd无法连接)
[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