一、Netfilter 简介

(1) Netfilter 是 Linux 内置的一种防火墙机制,我们一般也称之为数据包过滤机制,而 iptables 只是操作 netfilter 的一个命令行工具
(2) Netfilter 是 Linux CentOS 6 内置的防火墙机制,Firewall 是 Linux CentOS 7 内置的防火墙机制,如果想在 CentOS 7 中使用 netfilter 而不是 firewall,操作如下

[root@MongoDB ~]# systemctl stop firewalld.service  // 停止firewalld
[root@MongoDB ~]# systemctl disable firewalld.service  // 禁止firewall开机启动
[root@MongoDB ~]# yum install iptables-services -y
[root@MongoDB ~]# systemctl start iptables.service  //启动防火墙
[root@MongoDB ~]# systemctl enable iptables.service  // 设置防火墙开机启动

centos7 iptables服务

1.查看iptables服务状态

[root@MongoDB ~]#systemctl status iptables

systemctl start iptables  // 启动iptables服务
systemctl restart iptables  // 重启iptables服务
systemctl stop iptables    // 关闭iptables服务

centos6 iptables 服务

service iptables start  // 启动 iptables服务
service iptables restart  // 重启iptables服务
service iptables stop    // 关闭iptables服务
service iptables status  // 查看iptables服务状态

/etc/init.d/iptables stop // 关闭iptables服务
/etc/init.d/iptables status  // 查看iptables状态

2.查看规则,默认查看filter表的规则
iptables -nvL              针对INPUT链 默认规则ACCEPT通过
iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)  // 针对INPUT链 默认规则
// INPUT链 规则pkts bytes target     prot opt in     out     source               destination         7589  858K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           42  2520 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           2   104 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:220     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:27017
30806 3205K REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited// FORWARD链 默认规则
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination         0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited// OUTPUT链 默认规则
Chain OUTPUT (policy ACCEPT 6732 packets, 950K bytes)pkts bytes target     prot opt in     out     source               destination 

二、iptables 常见用法:

iptables -F                # 清空规则,默认清空filter表的规则
iptables -X                # 删除用户自定义规则
iptables -Z                # 清空链的计数器
service iptables save      # 保存规则,会把当前规则保存到/etc/sysconfig/iptables
iptables-save > my.ipt     # 备份规则,这里指定备份到my.ipt文件
iptables-restore < my.ipt  # 恢复规则,这里指定使用my.ipt文件进行恢复  

清空所有的规则,只留下默认规则

iptables -F  清空规则,默认清空filter表的规则

只留下默认规则 默认规则都是ACCEPT动作

[root@MongoDB ~]# iptables -F
[root@MongoDB ~]# iptables -nvL
Chain INPUT (policy ACCEPT 6 packets, 480 bytes)pkts bytes target     prot opt in     out     source               destination         Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination         Chain OUTPUT (policy ACCEPT 6 packets, 528 bytes)pkts bytes target     prot opt in     out     source               destination 

iptables -X  删除用户定义规则

iptables -Z 清空链的计数器

iptables -A INPUT -p tcp --dport 80 -j ACCEPT                                                # 放通80端口
iptables -A INPUT -p tcp --dport 22 -j DROP                                                  # 禁用22端口
iptables -A INPUT -p tcp -m multiport --dport 80,843,443 -j ACCEPT                          # 放通多个端口
iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT                              # 只允许某个IP访问22端口
iptables -A INPUT -s 192.168.1.1/32 -p tcp -m multiport --dport 3873,4507,3306 -j ACCEPT    # 允许某个IP访问多个端口

添加一条规则 到filter表 允许8080端口

[root@MongoDB ~]# iptables -t filter -A INPUT -p tcp --dport 8080 -j ACCEPT

删除一条规则

先执行 iptables -nvL --line-numbers 查看规则的序列号
[root@MongoDB ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 93 packets, 7775 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8080Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         Chain OUTPUT (policy ACCEPT 59 packets, 6900 bytes)
num   pkts bytes target     prot opt in     out     source               destination 

-D 删除规则

然后执行 iptables -D INPUT <number> 删除规则
[root@MongoDB ~]# iptables -D INPUT 1
[root@MongoDB ~]#
[root@MongoDB ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 14 packets, 1020 bytes)
num   pkts bytes target     prot opt in     out     source               destination         Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         Chain OUTPUT (policy ACCEPT 6 packets, 728 bytes)
num   pkts bytes target     prot opt in     out     source               destination

-A       # 添加规则,默认添加到最后一条规则
-I       # 插入规则,默认插入到第一条规则
-D       # 删除规则,先执行 iptables -nvL --line-numbers 查看规则的序列号,然后执行 iptables -D INPUT <number> 删除规则
-n       # 数字
-s       # 用于指定源IP地址,用法如:iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT
-d       # 用于指定目标IP地址,用法如:iptables -A INPUT -d 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT
-p       # 用于指定检查哪个协议,用法如:iptables -A INPUT -p tcp --dport 80 -j ACCEPT
-i       # 用于指定入口网卡,用法如:iptables -A INPUT -i eth0 -j ACCEPT
-o       # 用于指定出口网卡,用法如:iptables -A FORWARD -o eth0 -j ACCEPT
-j       # 用于指定要进行的处理动作,ACCEPT表示接受数据包进来 DROP直接丢弃数据包 用法如:iptables -A INPUT -p tcp --dport 80 -j ACCEPT
-P       # 用于设置默认规则,用法如:iptables -P INPUT DROP
-t       # 用于指定操作哪张表,用法如:iptables -t nat -nvL , iptables -t filter ,如果没有指定默认是filter表
-Z       # 用于清空计数器,用法如:iptables -Z
-L       # 用于列出规则链中的所有规则
--sport  # 用于指定源端口,用法如:iptables -A INPUT -p tcp --sport 1234 -j ACCEPT
--dport  # 用于指定目标端口,用法如:iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT
!        # 取反 

 

转载于:https://www.cnblogs.com/mingerlcm/p/10685450.html

Linux 防火墙:Netfilter iptables相关推荐

  1. linux 防火墙 -netfilter

    2019独角兽企业重金招聘Python工程师标准>>> 关于iptables 什么是iptables? 常见于linx系统下的应用层防火墙工具 firewalld 和netfilte ...

  2. Linux防火墙与iptables命令

    Linux防火墙与iptables命令 防火墙概念 一.Firewalld与iptables简介 1.1Firewalld 1.2Firewalld.iptables 二.iptables 2.1四表 ...

  3. linux防火墙Netfilter(iptables)

    Linux的防火墙由Netfilter实现: Netfilter的过滤功能在内核中实现,不依靠daemon. 工作于OSI 7层模型的第2, 3, 4层. 只检测报文头. iptables是Netfi ...

  4. 【Q】之Linux中的防火墙netfilter iptables

    防火墙 一.Linux防火墙基础 1.1 Linux包过滤防火墙概述 1.2 数据包控制的匹配流程 控制的匹配流程

  5. Linux防火墙之iptables

    文章目录 一.什么是防火墙 二.防火墙分类 1.硬件防火墙 2.软件防火墙 三.iptables详解 1.netfilter和iptables(命令) 2.四表五链 3.iptables匹配过程 4. ...

  6. Linux 防火墙之iptables

    文章目录 引言 一.iptables 概述 1.1 netfilter/iptables关系 1.2 iptables 的四表五链 二.iptables 配置 2.1 iptables 安装 2.2 ...

  7. linux防火墙reject,Iptables 扩展动作 Reject Mark

    防火墙在做信息滤决定时,有一套遵循和组成的规则,这些规则存储在专用的信息滤表中,而这些表集成在Linux内核中.在信息滤表中,规则被分组放在我们所谓的链(Chain)中.Iptables组件是一种工具 ...

  8. linux 防火墙加固,Iptables 加固服务器安全

    防火墙在做信息滤决定时,有一套遵循和组成的规则,这些规则存储在专用的信息滤表中,而这些表集成在Linux内核中.在信息滤表中,规则被分组放在我们所谓的链(Chain)中.Iptables组件是一种工具 ...

  9. Linux防火墙:iptables禁IP与解封IP常用命令

    转载:https://www.baidu.com/link?url=SJWsJbXb_P8s9tK4BGIwdAJD5aH1c5_eq5IHIiiqW6kUBBmH4zmnx4BNqXdmuxWz1T ...

  10. 【Linux防火墙】iptables基础用法及高级用法

    文章目录 iptables命令简介 1.语法 2.iptables命令选项输入顺序 3.选项讲解 4.基础用法示例 1.清除规则相关操作 2.查看相关规则及序列号 3.通过查看后的序列号删除规则 4. ...

最新文章

  1. [Swust OJ 566]--开N方数(牛顿切线法解高次方程)
  2. Atitit.js图表控件总结
  3. c语言定义数组变量初始化为0,c语言数组初始化——int a[N] = {0};
  4. js reduce实现中间件_js数组高阶方法reduce经典用法代码分享
  5. html教程padding,HTML CSS——margin和padding的学习
  6. oracle查询慢怎么优化,Oracle查询优化-怎样建立索引优化下面的查询语句啊
  7. numpy的基本使用 附python代码详细讲解(numpy函数,创建数组,常用属性,索引切片,数组的复制,修改维度,数组的分割、拼接、转置)
  8. 采用分治法求一个整数序列中的最大值和最小值
  9. bat命令调用rar进行文件夹压缩
  10. C# 登陆验证码工具类VerifyCode
  11. 测试结果可视化翻译_流行测验:此民意调查结果可视化有什么问题?
  12. 不定期备考小tips[常微][2] #20210528
  13. 有密码的压缩包怎么解密
  14. 如何应对项目中的范围蔓延?
  15. 根据Box-Muller变换原理利用均匀分布的随机变量生成二维柯西分布
  16. 热烈欢迎Ubuntu志愿者!
  17. 洛必达法则的一种极简证明
  18. 大学生需要云服务器吗?
  19. HEVC解码器HM源码阅读(四)解析参数集
  20. 网络变压器在以太网中的作用

热门文章

  1. 2019牛客第八场A All-one Matrices(单调栈)
  2. oracle分区和锁的难,oracle使用三(锁和表分区)
  3. 并发请求数_nginx如何限制并发连接和请求数?
  4. PAT_B_1080_C++(25分)
  5. linux退出大于符号,每天一个linux命令--退出符号
  6. c++ 删除二叉树的子树_平衡二叉树
  7. nodejs正则提取html,Nodejs正则表达式函数之match、test、exec、search、split、replace
  8. 文档过期打不开怎么办_4 个超实用金山文档小技巧,很强大也很简单,赶紧收藏!...
  9. io流不关闭会怎么样_幸福树一个月不浇水会怎么样
  10. 2019.9.18最小生成树知识点总结