CentOS之——CentOS7安装iptables防火墙

 

CentOS之——CentOS7安装iptables防火墙

载请注明出处:http://blog.csdn.net/l1028386804/article/details/50779761

CentOS7默认的防火墙不是iptables,而是firewalle.

安装iptable iptable-service

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #先检查是否安装了iptables  
  2. service iptables status  
  3. #安装iptables  
  4. yum install -y iptables  
  5. #升级iptables  
  6. yum update iptables   
  7. #安装iptables-services  
  8. yum install iptables-services  
禁用/停止自带的firewalld服务
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #停止firewalld服务  
  2. systemctl stop firewalld  
  3. #禁用firewalld服务  
  4. systemctl mask firewalld  
设置现有规则
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #查看iptables现有规则  
  2. iptables -L -n  
  3. #先允许所有,不然有可能会杯具  
  4. iptables -P INPUT ACCEPT  
  5. #清空所有默认规则  
  6. iptables -F  
  7. #清空所有自定义规则  
  8. iptables -X  
  9. #所有计数器归0  
  10. iptables -Z  
  11. #允许来自于lo接口的数据包(本地访问)  
  12. iptables -A INPUT -i lo -j ACCEPT  
  13. #开放22端口  
  14. iptables -A INPUT -p tcp --dport 22 -j ACCEPT  
  15. #开放21端口(FTP)  
  16. iptables -A INPUT -p tcp --dport 21 -j ACCEPT  
  17. #开放80端口(HTTP)  
  18. iptables -A INPUT -p tcp --dport 80 -j ACCEPT  
  19. #开放443端口(HTTPS)  
  20. iptables -A INPUT -p tcp --dport 443 -j ACCEPT  
  21. #允许ping  
  22. iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT  
  23. #允许接受本机请求之后的返回数据 RELATED,是为FTP设置的  
  24. iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT  
  25. #其他入站一律丢弃  
  26. iptables -P INPUT DROP  
  27. #所有出站一律绿灯  
  28. iptables -P OUTPUT ACCEPT  
  29. #所有转发一律丢弃  
  30. iptables -P FORWARD DROP  
其他规则设定
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #如果要添加内网ip信任(接受其所有TCP请求)  
  2. iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT  
  3. #过滤所有非以上规则的请求  
  4. iptables -P INPUT DROP  
  5. #要封停一个IP,使用下面这条命令:  
  6. iptables -I INPUT -s ***.***.***.*** -j DROP  
  7. #要解封一个IP,使用下面这条命令:  
  8. iptables -D INPUT -s ***.***.***.*** -j DROP  
保存规则设定
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #保存上述规则  
  2. service iptables save  
开启iptables服务
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #注册iptables服务  
  2. #相当于以前的chkconfig iptables on  
  3. systemctl enable iptables.service  
  4. #开启服务  
  5. systemctl start iptables.service  
  6. #查看状态  
  7. systemctl status iptables.service  

解决vsftpd在iptables开启后,无法使用被动模式的问题

1.首先在/etc/sysconfig/iptables-config中修改或者添加以下内容

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #添加以下内容,注意顺序不能调换  
  2. IPTABLES_MODULES="ip_conntrack_ftp"  
  3. IPTABLES_MODULES="ip_nat_ftp"  
2.重新设置iptables设置
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT  
以下为完整设置脚本
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #!/bin/sh  
  2. iptables -P INPUT ACCEPT  
  3. iptables -F  
  4. iptables -X  
  5. iptables -Z  
  6. iptables -A INPUT -i lo -j ACCEPT  
  7. iptables -A INPUT -p tcp --dport 22 -j ACCEPT  
  8. iptables -A INPUT -p tcp --dport 21 -j ACCEPT  
  9. iptables -A INPUT -p tcp --dport 80 -j ACCEPT  
  10. iptables -A INPUT -p tcp --dport 443 -j ACCEPT  
  11. iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT  
  12. iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT  
  13. iptables -P INPUT DROP  
  14. iptables -P OUTPUT ACCEPT  
  15. iptables -P FORWARD DROP  
  16. service iptables save  
  17. systemctl restart iptables.service  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值