小五六资源网,小五六博客

iptables 禁止ping

发布:小五六资源网 2020年12月24日 1:41 星期四分类: Linux

示例1:

两台虚拟 ,通过iptables 控制,让k8s-01-102无法pingk8s-01-10。  服务器IP如下。

k8s-01-102:   192.168.2.102

k8s-01-103:   192.168.2.103

1、节点k8s-01-103,初始状态iptables 状态:


  1. [root@k8s-node-2 ~]# iptables -L INPUT --line-numbers
  2. Chain INPUT (policy ACCEPT)
  3. num target prot opt source destination

2、节点k8s-01-102,初始ping 状态,可以ping通:


  1. [root@k8s-node-1 ~]# ping 192.168.2.103
  2. PING 192.168.2.103 (192.168.2.103) 56(84) bytes of data.
  3. 64 bytes from 192.168.2.103: icmp_seq=16 ttl=64 time=0.357 ms
  4. 64 bytes from 192.168.2.103: icmp_seq=17 ttl=64 time=0.307 ms
  5. 64 bytes from 192.168.2.103: icmp_seq=18 ttl=64 time=0.284 ms
  6. 64 bytes from 192.168.2.103: icmp_seq=19 ttl=64 time=0.280 ms
  7. 64 bytes from 192.168.2.103: icmp_seq=20 ttl=64 time=0.313 ms
  8. 64 bytes from 192.168.2.103: icmp_seq=21 ttl=64 time=0.287 ms
  9. 64 bytes from 192.168.2.103: icmp_seq=22 ttl=64 time=0.270 ms

3、节点k8s-01-103,添加INPUT链,添加后查看INPUT链状态:

[root@k8s-node-2 ~]# iptables -I INPUT -p icmp -j DROP

  1. [root@k8s-node-2 ~]# iptables -L INPUT --line-numbers
  2. Chain INPUT (policy ACCEPT)
  3. num target prot opt source destination
  4. 1 DROP icmp -- anywhere anywhere

4、节点k8s-01-102,再次ping k8s-01-103节点:


  1. [root@k8s-node-1 ~]# ping 192.168.2.103
  2. PING 192.168.2.103 (192.168.2.103) 56(84) bytes of data.
  3. ^C
  4. --- 192.168.2.103 ping statistics ---
  5. 15 packets transmitted, 0 received, 100% packet loss, time 14001ms
  6. [root@k8s-node-1 ~]#

5、删除k8s-01-103中新添加的链,注意删除链用的是行号,因此查看链时都加了 --line-numbers 参数:


  1. [root@k8s-node-2 ~]# iptables -D INPUT 1
  2. [root@k8s-node-2 ~]# iptables -L INPUT --line-numbers
  3. Chain INPUT (policy ACCEPT)
  4. num target prot opt source destination
  5. [root@k8s-node-2 ~]#

6、节点k8s-01-102,再次ping,可以ping通:


  1. [root@k8s-node-1 ~]# ping 192.168.2.103
  2. PING 192.168.2.103 (192.168.2.103) 56(84) bytes of data.
  3. 64 bytes from 192.168.2.103: icmp_seq=16 ttl=64 time=0.357 ms
  4. 64 bytes from 192.168.2.103: icmp_seq=17 ttl=64 time=0.307 ms
  5. 64 bytes from 192.168.2.103: icmp_seq=18 ttl=64 time=0.284 ms
  6. 64 bytes from 192.168.2.103: icmp_seq=19 ttl=64 time=0.280 ms
  7. 64 bytes from 192.168.2.103: icmp_seq=20 ttl=64 time=0.313 ms
  8. 64 bytes from 192.168.2.103: icmp_seq=21 ttl=64 time=0.287 ms
  9. 64 bytes from 192.168.2.103: icmp_seq=22 ttl=64 time=0.270 ms

7、原理解释:

ping 实际是采用了icmp 协议的一个工具。

在服务器k8s-01-103中,通过iptables 中在于内置INPUT链的最上边添加了icmp的drop规则。

收到的packet直接丢弃掉,不会有响应。也就是模拟了远端ping,但是不会收到相应包的情况。

温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!

发表评论: