CentOS之——CentOS7安装iptables防火墙

来源:互联网 发布:网络电视直播软件 最新 编辑:程序博客网 时间:2024/06/04 20:01
 

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小蜜丸吃不下去怎么办 铜钱的字不认识怎么办 古钱币出手好烦怎么办 安装目录不可写怎么办 手机不支持exfat格式怎么办 windows7图标变大了怎么办 igs格式烂曲面怎么办 手机桌面文件夹打不开怎么办 苹果下载不了150怎么办 iphone6速度变慢怎么办 苹果手机微信打不开pdf怎么办 苹果手机打不开pdf怎么办 pdf文件超过了怎么办 pdf电脑删不了怎么办 联想笔记本摄像头横屏调竖屏怎么办 pdf文件打开失败怎么办 pdf复制文字乱码怎么办 电子发票乱码了怎么办 超星尔雅挂了怎么办 电脑应用双击打不开怎么办 电脑控制面板打不开怎么办 转换器无法打开文件怎么办 电脑文件无法打开怎么办 手机上jpg打不开怎么办 脸上全是黄褐斑怎么办 容易发胖的体质怎么办 感冒后一直咳嗽怎么办 感冒咳嗽怎么办小窍门 到了减肥平台期怎么办 减肥遇见平台期怎么办 脚冻伤了痒怎么办 冬天脚后跟冻了怎么办 夏天脚冻了怎么办 导航软件删了怎么办 婆婆爱打孩子怎么办 乙肝婆婆带孩子怎么办 婆婆不会教孩子怎么办 婆婆不会带孩子怎么办 公婆枪带孩子怎么办 婆婆要带孩子怎么办 婆婆太惯孩子怎么办