为什么要设置禁PING
在生产环境中,一般会把服务器的IP地址设为禁PING的,即禁止服务器对PING这个客户端发出响应。因为若PING请求非常多的话,服务器要花费大量资源去响应PING请求。因此生产环境中有一种PING攻击,即什么都不做,只是PING你的服务器,那我们的服务器资源全部用来响应PING请求的话,就没有办法对其他服务进行正常的响应。
C:\Users\qwd>ping 192.168.172.130
正在 Ping 192.168.172.130 具有 32 字节的数据:
来自 192.168.172.130 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.172.130 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.172.130 的回复: 字节=32 时间<1ms TTL=64
192.168.172.130 的 Ping 统计信息:
数据包: 已发送 = 3,已接收 = 3,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 1ms,平均 = 0ms
Control-C
^C
C:\Users\qwd>
表示可以PING通。
在服务器中,是否对PING请求作出回应是在内核参数中设定的
sysctl -a命令可用来查看所有的内核参数
通用方法:
sysctl -a | grep icmp可用来过滤其中包含icmp的命令(因为ping命令是遵循icmp协议的)
[root@qwb ~]# sysctl -a | grep icmp
sysctl: net.ipv4.icmp_echo_ignore_all = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_errors_use_inbound_ifaddr = 0
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.icmp_msgs_burst = 50
net.ipv4.icmp_msgs_per_sec = 1000
net.ipv4.icmp_ratelimit = 1000
net.ipv4.icmp_ratemask = 6168
reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.ens33.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
net.ipv6.icmp.ratelimit = 1000
net.netfilter.nf_conntrack_icmp_timeout = 30
net.netfilter.nf_conntrack_icmpv6_timeout = 30
其中,sysctl: net.ipv4.icmp_echo_ignore_all这一参数是用来控制是否回应PING命令的。将其设为0即对PING命令作出响应,设为1即不做出响应。
将该参数设为1的步骤
将sysctl: net.ipv4.icmp_echo_ignore_all = 1写入/etc/sysctl.conf即可。
但是配置文件不能即时生效,需要在系统重启之后才能读取,加载完这一参数后才能生效
sysctl -p命令可使sysctl.conf文件中的变量立刻加载到内存中,从而使其立刻生效
[root@qwb ~]# sysctl -p
net.ipv4.icmp_echo_ignore_all = 1
[root@rebekk ~]# sysctl -a |grep icmp
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
net.ipv4.icmp_echo_ignore_all = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_errors_use_inbound_ifaddr = 0
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.icmp_msgs_burst = 50
net.ipv4.icmp_msgs_per_sec = 1000
net.ipv4.icmp_ratelimit = 1000
net.ipv4.icmp_ratemask = 6168
sysctl: reading key "net.ipv6.conf.ens33.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
net.ipv6.icmp.ratelimit = 1000
net.netfilter.nf_conntrack_icmp_timeout = 30
net.netfilter.nf_conntrack_icmpv6_timeout = 30
[root@qwb ~]#
现在来试一下效果
C:\Users\qwd>ping 192.168.172.130
正在 Ping 192.168.172.130 具有 32 字节的数据:
请求超时。
请求超时。
192.168.172.130 的 Ping 统计信息:
数据包: 已发送 = 2,已接收 = 0,丢失 = 2 (100% 丢失),
Control-C
^C
C:\Users\qwd>
另一种更加简便的即时方法
[root@qwb ~]# cat /proc/sys/net/ipv4/icmp_echo_ignore_all
1
[root@qwb ~]# echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all
[root@qwb ~]# cat /proc/sys/net/ipv4/icmp_echo_ignore_all
0
[root@qwb ~]#