linux-ubuntu关闭防火墙


SYNOPSIS
       iptables [-t table] {-A|-C|-D} chain rule-specification
       ip6tables [-t table] {-A|-C|-D} chain rule-specification
       iptables [-t table] -I chain [rulenum] rule-specification
       iptables [-t table] -R chain rulenum rule-specification
       iptables [-t table] -D chain rulenum
iptables 是linux下一款强大的防火墙,在不考虑效率的情况下,功能强大到足可以替代大多数硬件防火墙,但是强大的防火墙如果应用不当,可能挡住的可不光是那些潜在的攻击,还有可能是你自己哦。这个带来的危害对于普通的个人PC来说可能无关紧要,但是想象一下,如果这是一台服务器,一旦发生这样的情况,不光是影院正常的服务,还需要到现场去恢复,这会给你带来多少损失呢?

所以我想说的是,当你敲入每一个iptables 相关命令的时候都要万分小心。

1.应用每一个规则到DROP target时,都要仔细检查规则,应用之前要考虑他给你带来的影响。

2.在redhat中我们可以使用service iptables stop来关闭防火墙,但是在有些版本如ubuntu中这个命令却不起作用,大家可能在网上搜索到不少文章告诉你用iptables -F这个命令来关闭防火墙,但是使用这个命令前,千万记得用iptables -L查看一下你的系统中所有链的默认target,iptables -F这个命令只是清除所有规则,只不会真正关闭iptables.想象一下,如果你的链默认target是DROP,本来你有规则来允许一些特定的端口,但一旦应用iptables -L ,清除了所有规则以后,默认的target就会阻止任何访问,当然包括远程ssh管理服务器的你。

以我建议的关闭防火墙命令是

    iptables -P INPUT ACCEPT
    iptables -P FORWARD ACCEPT
    iptables -P OUTPUT ACCEPT
    iptables -F

总之,当你要在你的服务器上做任何变更时,最好有一个测试环境做过充分的测试再应用到你的服务器。除此之外,要用好iptables,那就要理解iptables的运行原理,知道对于每一个数据包iptables是怎么样来处理的。这样才能准确地书写规则,避免带来不必要的麻烦。

       iptables [-t table] -S [chain [rulenum]]
       iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]
       iptables [-t table] -N chain
       iptables [-t table] -X [chain]
       iptables [-t table] -P chain target
       iptables [-t table] -E old-chain-name new-chain-name
       rule-specification = [matches...] [target]
       match = -m matchname [per-match-options]
       target = -j targetname [per-target-options]


屏蔽ICMP ping请求

我们可以通过允许下面的命令屏蔽ping请求:

  1. # iptables -A INPUT -p icmp --icmp-type echo-request -j DROP # iptables -A INPUT -i eth1 -p icmp --icmp-type echo-request -j DROP

也可以按照特定的网段和主机限制ping请求:

  1. # iptables -A INPUT -s 192.168.1.0/24 -p icmp --icmp-type echo-request -j ACCEPT

以下命令只接受受限制的ping请求:

  1. #假定默认INPUT策略为丢弃数据包 # iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
  2. # iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT # iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
  3. #所有的服务器都对ping请求作出应答 # iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT 

屏蔽或开启常见端口

屏蔽或开启常用的TCP、UDP端口:

  1. #可以使用DROP替换ACCEPT,实现端口屏蔽。 #打开22端口(SSH)
  2. # iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 22 -j ACCEPT
  3. #打开TCP/UDP631端口(打印服务) # iptables -A INPUT -s 192.168.1.0/24 -p udp -m udp --dport 631 -j ACCEPT
  4. # iptables -A INPUT -s 192.168.1.0/24 -p tcp -m tcp --dport 631 -j ACCEPT # 打开123端口,允许局域网用户进行NTP时间同步
  5. # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p udp --dport 123 -j ACCEPT #打开25端口(SMTP)
  6. # iptables -A INPUT -m state --state NEW -p tcp --dport 25 -j ACCEPT # 打开DNS端口
  7. # iptables -A INPUT -m state --state NEW -p udp --dport 53 -j ACCEPT # iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
  8. #打开http/https端口 # iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
  9. # iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT #打开TCP110端口(POP3)
  10. # iptables -A INPUT -m state --state NEW -p tcp --dport 110 -j ACCEPT #打开TCP143端口
  11. # iptables -A INPUT -m state --state NEW -p tcp --dport 143 -j ACCEPT #为局域网用户开启Samba访问
  12. # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 137 -j ACCEPT # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 138 -j ACCEPT
  13. # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 139 -j ACCEPT # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 445 -j ACCEPT
  14. #为局域网用户开启代理服务器访问 # iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 3128 -j ACCEPT
  15. #为局域网用户开启MySQL访问 # iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

相关内容