2019独角兽企业重金招聘Python工程师标准>>>

iptables常用知识回顾点

  • iptables -I/-A/-D 后紧跟 链 ,可以是INPUT,OUTPUT,FORWARD
  • iptables -P 用来指定 链的默认策略 ——>最好不要直接操作,否则会造成远程的终端断开

iptables小案例

  • 需求:

    • 把80,22,21端口放行,但22端口指定一个IP段,只允许这个IP段的IP访问的时候,才可访问到,其他段的一概拒绝
  • 实现:(用一个脚本来实现)
    • RELATED状态,这是一个边缘的一个状态

      • 比如:客户端和服务端相互了通信,建立完连接之后,还会有一些额外的链接出来,这时候状态就变成了RELATED(若紧紧只有ESTABLISHED,而没有RELATED,很有可能导致其他的通信被禁掉,因为默认策略是INPUT DROP)
    • ESTABLISHED状态, 保持连接
    • 有时,在没有增加-m --state这条规则,导致增加了80或21端口,但是不能正常通信,加这条规则的目的是为了让通信更加顺畅
[root@hanfeng-001 ~]# vim /usr/local/sbin/iptables.sh添加以下内容#! /bin/bash
ipt="/usr/sbin/iptables    //这里ipt是定义个一个变量(写脚本的时候,写全局的路径,就是绝对路径,就是后面再加载它,用变量去代替,看着更加简单)
$ipt -F    //清空之前的规则——>在没有 -t 指定表的时候,默认的就是filter表
$ipt -P INPUT DROP    //把IPPUT的策略给扔掉
$ipt -P OUTPUT ACCEPT    //把OUTPUT放行
$ipt -P FORWARD ACCEPT    //把FORWARD放行
$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT    //增加规则,-m  --state 指定了状态,并针对这些状态放行(-m  --state这种用法并不多见,但是这条规则必须写进来,目的是让相关的数据包放行)
$ipt -A INPUT -s 192.168.202.130/24 -p tcp --dport 22 -j ACCEPT    //把该网段的22端口数据包放行——>这里的IP段根据自己的IP段来做实验
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT    //把80端口数据包放行
$ipt -A INPUT -p tcp --dport 21 -j ACCEPT      //把21端口数据包放行然后保存退出:wq
[root@hanfeng ~]# sh /usr/local/sbin/iptables.sh        //执行脚本
[root@hanfeng ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination         30  2148 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED0     0 ACCEPT     tcp  --  *      *       192.168.202.0/24     0.0.0.0/0            tcp dpt:220     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:800     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:21Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination         Chain OUTPUT (policy ACCEPT 18 packets, 1816 bytes)pkts bytes target     prot opt in     out     source               destination
[root@hanfeng ~]# 
  • 为什么要写脚本?
  • 因为这里有INPUT DROP,肯定要把你的远程连接给断开,所以需要执行脚本把你的命令批量执行

icmp示例

  1. iptables -I INPUT -p icmp --icmp-type 8 -j DROP 这个规则会产生一个效果,让你ping外面的机器还可以ping通,但是ping本机的时候,就不会通了
[root@hanfeng-001 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP    //会发现可ping通外面的网络,但自己的虚拟机和物理机则无法连接
[root@hanfeng-001 ~]# ping www.qq.com
PING www.qq.com (180.96.86.192) 56(84) bytes of data.
64 bytes from 180.96.86.192: icmp_seq=1 ttl=128 time=7.38 ms
64 bytes from 180.96.86.192: icmp_seq=2 ttl=128 time=6.16 ms
64 bytes from 180.96.86.192: icmp_seq=3 ttl=128 time=7.73 ms
^C
--- www.qq.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 6.166/7.092/7.731/0.677 ms
[root@hanfeng-001 ~]# 
  1. 这时用自己的物理机去ping虚拟机,会发现无法连接

  1. 这时,再来删除这条命令
  • iptables -D INPUT -p icmp --icmp-type 8 -j DROP
[root@hanfeng ~]# iptables -D INPUT -p icmp --icmp-type 8 -j DROP
iptables: Bad rule (does a matching rule exist in that chain?).
[root@hanfeng ~]#
  1. service iptables restart 重启iptables服务
[root@hanfeng ~]# service iptables restart    //重启iptables服务
Redirecting to /bin/systemctl restart  iptables.service
[root@hanfeng ~]# iptables -nvL    //这里会看到还没禁掉之前的规则
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination         81  6996 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            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            Chain OUTPUT (policy ACCEPT 61 packets, 6060 bytes)pkts bytes target     prot opt in     out     source               destination
[root@hanfeng ~]# 
  1. 这里物理机去ping虚拟机IP,会发现再次ping通

  • 这里的物理机和虚拟机不通,并不指不能连接,这里仅仅是做了一个禁ping而已(是可以ping通外网的,但别人无法ping通你)

转载于:https://my.oschina.net/u/3707314/blog/1581927

10.15 iptables filter表案例相关推荐

  1. 10.15 iptables filter表小案例10.16/10.17/10.18 iptables nat表应用

    2019独角兽企业重金招聘Python工程师标准>>> 10.15 iptables filter表小案例 iptables 命令.语法总结 iptables-nvL //查看ipt ...

  2. 31次课(iptables filter表案例、iptables nat表应用)

    10.15 iptables filter表案例 iptables小案例,需求需要把80端口22端口还有21端口放行.但是22端口我需要指定一个ip段,只有这个ip段的ip访问的时候才可以访问,其他段 ...

  3. iptables filter表案例/iptables nat表应用

    iptables filter表案例 iptables filter 表案例 创建一个iptables.sh脚本 [root@Ask-02 ~]# vim /usr/local/sbin/iptabl ...

  4. iptables nat表含义_十(4)iptables语法、iptables filter表小案例、iptables nat表应用

    iptables语法 filter表: INPUT链:作用于进入本机的包 OUTPUT链:作用于送出本机的包 FORWARD链:作用于和本机无关的包 nat表: PREROUTING链:作用是包在刚刚 ...

  5. 【CentOS 7笔记43】,防火墙和iptables filter表#

    2019独角兽企业重金招聘Python工程师标准>>> shallow丿ove firewalld和netfilter setenforce 0 #临时关闭selinux /etc/ ...

  6. Linux防火墙-netfilter filter表案列与nat表应用

    iptables filter表案例 脚本代码和注释 [root@localhost ~]# vim /usr/local/sbin/iptables.sh ## 文档内容 #!/bin/bash # ...

  7. 06.15 iptables防火墙

    第一章 什么是防火墙 1. 软件防火墙 iptables免费 是开源. 2. 硬件防火墙 华为 深信服 思科 H3C Juniper 天融信 飞塔 网康 绿盟科技 金盾 3. 防火墙工作流程 4. i ...

  8. 七周三次课(11月29日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法...

    2019独角兽企业重金招聘Python工程师标准>>> 10.11 Linux网络相关 ifconfig 查看网卡ip (yum install net-tools) 安装 -a   ...

  9. 七周三次课(1月24日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法...

    七周三次课(1月24日) 10.11 Linux网络相关 10.12 firewalld和netfilter 10.13 netfilter5表5链介绍 10.14 iptables语法 ====== ...

最新文章

  1. 机器学习算法与理论用到的数学知识
  2. [ACM] hdu 1285 确定比赛名次 (拓扑排序)
  3. SDOI2016 生成魔咒
  4. python标签使用教程_怎样用Python做标签云
  5. iOS定时器-- NSTimer 和CADisplaylink
  6. Redis入门指南(第2版) Redis设计思路学习与总结
  7. centos写mysql光标移到上一行_mysql:一条SQL更新语句(update)是如何执行的
  8. 算法图解:如何用两个栈实现一个队列?
  9. vs2008 及.netframework3.5 安装问题
  10. XX银行 机器学习平台使用情况访谈总结
  11. 河南oracle客户端,解决Oracle监听服务报错
  12. Julia: Array的确很强大
  13. 正态分布下贝叶斯决策的特例(二)
  14. Spark机器学习实例
  15. 【JY】橡胶支座的简述和其力学性能计算
  16. Gym - 100502G Outing (强连通缩点+树形依赖背包)
  17. 【真北直播预报】让你的对话更有力,使十维宇宙不坠落
  18. 高清视音监控系统的实现
  19. 运算放大器的16个基础知识点
  20. 完整dm368打印信息

热门文章

  1. swoole实现数据库连接池
  2. ASP.NET--Menu控件
  3. 20个经典要诀学好英语
  4. win7利用remote连接服务器,显示发生身份验证错误 要求的函数不受支持
  5. python写一个通讯录step by step V3.0
  6. OpenStack如何实现高可用集群介绍
  7. linux proc
  8. 金融行业安全漏洞分析报告
  9. Android预安装可卸载程序
  10. Android 获取标题栏的高度