CentOS7安装和配置iptables防火墙

  • A+
所属分类:LINUX运维 网站首页

这篇文章是为《用fail2ban 实现动态防火墙防暴力破解》服务的。链接地址:

从CentOS7(RHEL7)开始,官方的标准防火墙设置软件从iptables变更为firewalld。 因此,为了使Fail2ban与iptables联动,需禁用自带的firewalld服务,同时安装iptables服务。

1.查看防火墙状态

  1. [root@kvmtest ~]# systemctl status firewalld  
  2. ● firewalld.service - firewalld - dynamic firewall daemon  
  3.    Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)  
  4.    Active: active (running) since 六 2020-12-26 09:26:03 CST; 8s ago  
  5.      Docs: man:firewalld(1)  
  6.  Main PID: 3611 (firewalld)  
  7.     Tasks: 2  
  8.    CGroup: /system.slice/firewalld.service  
  9.            └─3611 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid  
  10.   
  11. 12月 26 09:26:03 kvmtest firewalld[3611]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter --delete FORWARD --destination 192.168.122.0/24 --out-interface vir...hat chain?).  
  12. 12月 26 09:26:03 kvmtest firewalld[3611]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter --delete FORWARD --source 192.168.122.0/24 --in-interface virbr0 --...hat chain?).  
  13. 12月 26 09:26:03 kvmtest firewalld[3611]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter --delete FORWARD --in-interface virbr0 --out-interface virbr0 --jum...hat chain?).  
  14. 12月 26 09:26:03 kvmtest firewalld[3611]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter --delete FORWARD --out-interface virbr0 --jump REJECT' failed: ipta...y that name.  
  15. 12月 26 09:26:03 kvmtest firewalld[3611]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter --delete FORWARD --in-interface virbr0 --jump REJECT' failed: iptab...y that name.  
  16. 12月 26 09:26:03 kvmtest firewalld[3611]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter --delete INPUT --in-interface virbr0 --protocol udp --destination-p...hat chain?).  
  17. 12月 26 09:26:03 kvmtest firewalld[3611]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter --delete INPUT --in-interface virbr0 --protocol tcp --destination-p...hat chain?).  
  18. 12月 26 09:26:03 kvmtest firewalld[3611]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter --delete OUTPUT --out-interface virbr0 --protocol udp --destination...hat chain?).  
  19. 12月 26 09:26:04 kvmtest firewalld[3611]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter --delete INPUT --in-interface virbr0 --protocol udp --destination-p...hat chain?).  
  20. 12月 26 09:26:04 kvmtest firewalld[3611]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter --delete INPUT --in-interface virbr0 --protocol tcp --destination-p...hat chain?).  
  21. Hint: Some lines were ellipsized, use -l to show in full.  
  22. [root@kvmtest ~]#   

2.关闭防火墙禁用服务

  1. [root@kvmtest ~]# systemctl stop firewalld  
  2. [root@kvmtest ~]# systemctl mask firewalld  
  3. Created symlink from /etc/systemd/system/firewalld.service to /dev/null.  
  4. [root@kvmtest ~]#   

3.安装iptables

  1. [root@kvmtest ~]# yum install -y iptables  
  2. [root@kvmtest ~]# yum update iptables  
  3. [root@kvmtest ~]# yum install iptables-services  

4.修改/etc/rsyslog.conf文件

为了记录iptables防火墙丢弃的数据包到日志文件,还需修改/etc/rsyslog.conf文件。

增加kern.* /var/log/iptables.log

  1. # Log all kernel messages to the console.  
  2. # Logging much else clutters up the screen.  
  3. #kern.*                                                 /dev/console  
  4. kern.* /var/log/iptables.log  

然后重启rsyslog服务

  1. [root@kvmtest ~]# systemctl restart rsyslog  
  2. [root@kvmtest ~]#   

同时,要对日志文件每隔一段时间(一周)进行切割,所以需要编辑/etc/logrotate.d/syslog文件。

  1. [root@kvmtest ~]# vim /etc/logrotate.d/syslog  
  2.   
  3. /var/log/cron  
  4. /var/log/maillog  
  5. /var/log/messages  
  6. /var/log/secure  
  7. /var/log/spooler  
  8. /var/log/iptables.log  
  9. {  
  10.     missingok  
  11.     sharedscripts  
  12.     postrotate  
  13.         /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true  
  14.     endscript  
  15. }  

5.规则设定

以下是一份完整的iptables脚本

  1. iptables -P INPUT ACCEPT  
  2. iptables -F  
  3. iptables -X  
  4. iptables -Z  
  5. iptables -A INPUT -i lo -j ACCEPT  
  6. iptables -A INPUT -p tcp --dport 22 -j ACCEPT  
  7. iptables -A INPUT -p tcp --dport 80 -j ACCEPT  
  8. iptables -A INPUT -p tcp --dport 443 -j ACCEPT  
  9. iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT  
  10. iptables -A INPUT  -p tcp -j LOG --log-prefix "iptables denied"  
  11. iptables -P INPUT   DROP  
  12. iptables -P OUTPUT  ACCEPT  
  13. iptables -P FORWARD ACCEPT  

6.重启防火墙

  1. [root@kvmtest ~]# service iptables save  
  2. iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]  
  3. [root@kvmtest ~]# systemctl restart iptables  
  4. [root@kvmtest ~]#   

7.查看iptables规则

  1. [root@kvmtest ~]# iptables -L -n  
  2. Chain INPUT (policy DROP)  
  3. target     prot opt source               destination           
  4. ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0             
  5. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22  
  6. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80  
  7. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443  
  8. ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED  
  9. LOG        tcp  --  0.0.0.0/0            0.0.0.0/0            LOG flags 0 level 4 prefix "iptables denied"  
  10.   
  11. Chain FORWARD (policy ACCEPT)  
  12. target     prot opt source               destination           
  13.   
  14. Chain OUTPUT (policy ACCEPT)  
  15. target     prot opt source               destination           
  16. [root@kvmtest ~]#   

8.补充

关于上面的解释

iptables -P INPUT ACCEPT

表示先允许所有的输入通过防火墙,以防远程连接断开。

iptables -F

表示清空所有默认规则。

iptables -X

表示清空所有自定义规则。

iptables -Z

表示将所有计数器归0。

iptables -A INPUT -i lo -j ACCEPT

表示允许来自于lo接口(本地访问)的数据包

iptables -A INPUT -p tcp --dport 22 -j ACCEPT

表示开放22端口(SSH)。

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

表示开放80端口(HTTP)

iptables -A INPUT -p tcp --dport 443 -j ACCEPT

表示开放443端口(HTTPS)

iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT

表示允许接受本机请求之后的返回数据。

iptables -A INPUT  -p tcp -j LOG --log-prefix "iptables denied"

表示所有被丢弃的包都会被记录到/var/log/iptables.log文件中,且每条记录会以”iptables denied”作为前缀。

iptables -P INPUT DROP

表示其他入站一律丢弃

iptables -P OUTPUT ACCEPT

表示所有出站一律通过

iptables -P FORWARD DROP

表示所有转发一律通过

9.设定其他规则

如果要添加可信任网段:192.168.0.1/24,接受其所有请求。

iptables -A INPUT -s 192.168.0.1/24 -j ACCEPT

如果要添加可信任ip:192.168.0.1,接受其所有TCP请求。

iptables -A INPUT -p tcp -s 192.168.0.1 -j ACCEPT

如果要添加可信任ip:192.168.0.1,接受其对某个端口:3306的所有TCP请求。

iptables -I INPUT -p tcp -s 192.168.0.1 --dport 3306 -j ACCEPT

如果要封停一个IP:192.168.0.1。

iptables -I INPUT -s 192.168.0.1 -j DROP
moonrong
  • 版权声明:本站原创文章,于2020年12月26日13:50:42,由 发表,共 6123 字。
  • 版权声明: 本文由于2020年12月26日13:50:42 发表在 好派笔记,共 6123 字。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: