Linux禁用ping

禁止ping会有诸多不便,但是其带来的好处有时也令人不忍放弃,比如我,我有一台VPS之前因为搭了个VPN,结果没过多久后就遭到了洪水攻击,你懂得。。。后来把ping禁止掉后,换了下ssh的端口,立马清静了~

 

关于禁止ping的方法看了下网上很多人的帖子,其方式无外乎两类。一类通过修改内存加载的控制文件直接禁掉所有ICMP协议,另一类就是通过配置防火墙iptables规则来实现了。这两类方法各有优缺点,以下分别进行阐述。

 

第一类:修改内存加载的控制文件/proc/sys/net/ipv4/icmp_echo_ignore_all

这个方法也有很多种,最简单的就是直接修改文件:

关闭ping

echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all

开启ping

echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all

另外也可以通过控制命令sysctl来控制

sysctl -w net.ipv4.icmp_echo_ignore_all=1
sysctl -p

注:sysctl设置和显示在/proc/sys目录中的内核参数.能用sysctl来设置或重新设置连网功能,如IP转发、IP碎片去除及源路由检查等。用户只需要编辑/etc/sysctl.conf文件,即可手工或自动执行由sysctl控制的功能。

sysctl [-n] [-e] -w variable=value
sysctl [-n] [-e] -p (default /etc/sysctl.conf)
sysctl [-n] [-e] -a

常用参数的意义:
-w  临时改动某个指定参数的值,如:sysctl -w net.ipv4.ip_forward=1
-a   显示所有的系统参数
-p  从指定的文件加载系统参数,如不指定即从/etc/sysctl.conf中加载
如果仅仅是想临时改动某个系统参数的值,能用两种方法来实现,例如想启用IP路由转发功能:
1) #echo 1 > /proc/sys/net/ipv4/ip_forward
2) #sysctl -w net.ipv4.ip_forward=1
以上两种方法都可能即时开启路由功能,但如果系统重启,或执行了
# service network restart
命令,所设置的值即会丢失,如果想永久保留设置,能修改/etc/sysctl.conf文件
将 net.ipv4.ip_forward=0改为net.ipv4.ip_forward=1

可见,该方法的优点在于其简单,但缺点确在于无法保持配置,而且直接修改配置文件禁用的是整个icmp协议,在很多情况下确实有其不便之处。

第二类:配置iptables防火墙规则

本人对iptables也只是个初学者,没法进行详细讲解,这里就贴几个网上找到的例子。

iptables普通配置文件 :

vim /etc/firewall.sh

iptables -F
iptables -N FIREWALL
iptables -F FIREWALL
iptables -A INPUT -j FIREWALL
iptables -A FORWARD -j FIREWALL
iptables -A FIREWALL -p tcp -m tcp –dport 110 –syn -j ACCEPT
# nginx web server
iptables -A FIREWALL -p tcp -m tcp –dport 80 –syn -j ACCEPT
# apache web server
iptables -A FIREWALL -p tcp -m tcp –dport 81 –syn -j ACCEPT
#ssh
iptables -A FIREWALL -p tcp -m tcp –dport 22 –syn -j ACCEPT
iptables -A FIREWALL -i lo -j ACCEPT
# dns
iptables -A FIREWALL -p udp -m udp –sport 53 -j ACCEPT
iptables -A FIREWALL -p tcp -m tcp –syn -j REJECT
iptables -A FIREWALL -p udp -m udp -j REJECT
#禁ping
iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP
#开启ping
#iptables -D INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP

生成firewall.sh文件

权限
chmod 755 /etc/firewall.sh

修改配置文件

vim /etc/init.d/rc.local

在最后一行加上
sh /etc/firewall.sh

iptables屏蔽IP :
#如果只是想屏蔽IP的话“3、开放指定的端口”可以直接跳过。
#屏蔽单个IP的命令是
iptables -I INPUT -s 123.45.6.7 -j DROP
#封整个段即从123.0.0.1到123.255.255.254的命令
iptables -I INPUT -s 123.0.0.0/8 -j DROP
#封IP段即从123.45.0.1到123.45.255.254的命令
iptables -I INPUT -s 124.45.0.0/16 -j DROP
#封IP段即从123.45.6.1到123.45.6.254的命令是
iptables -I INPUT -s 123.45.6.0/24 -j DROP

查看已配置的规则,使用如下命令

iptables -L

会输出以下内容

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

上面的规则说明,允许任何人从任何地方访问。

首先来创建一个Iptables的文件

nano /etc/iptables.test.rules

输入以下规则

*filter

# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn’t use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

# Accepts all established inbound connections
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT

# Allows all outbound traffic
# You could modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
-A INPUT -p tcp –dport 80 -j ACCEPT
-A INPUT -p tcp –dport 443 -j ACCEPT

# Allows SSH connections for script kiddies
# THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
-A INPUT -p tcp -m state –state NEW –dport 30000 -j ACCEPT

# Now you should read up on iptables rules and consider whether ssh access
# for everyone is really desired. Most likely you will only allow access from certain IPs.

# Allow ping
-A INPUT -p icmp -m icmp –icmp-type 8 -j ACCEPT

# log iptables denied calls (access via ‘dmesg’ command)
-A INPUT -m limit –limit 5/min -j LOG –log-prefix “iptables denied: ” –log-level 7

# Reject all other inbound – default deny unless explicitly allowed policy:
-A INPUT -j REJECT
-A FORWARD -j REJECT

这看到比较复杂,但细看每一部分,你会发现,它只是关闭除了我们允许的所有端口,在这种情况下是80和443端口(标准的网络浏览器的端口)和前面定义的SSH端口。

激活这些新的规则

iptables-restore < /etc/iptables.test.rules

再来看看有什么不同

iptables -L

我们看到,只有上面定义的端口是关闭的,其余都是关闭的。如果是这样的,将保存Iptables文件

iptables-save > /etc/iptables.up.rules

为了确保iptables规则开始重新启动,我们将创建一个新的文件

nano /etc/network/if-pre-up.d/iptables

输入下面内容

#!/bin/bash
/sbin/iptables-restore < /etc/iptables.up.rules

更改一下权限

chmod +x /etc/network/if-pre-up.d/iptables

#添加屏蔽IP
#禁止此IP访问服务器
iptables -I INPUT -s 1.2.3.4 -j DROP

iptables -A INPUT -s 1.2.3.4 -j DROP
#禁止服务器访问此IP
iptables -A OUTPUT -d 1.2.3.4 -j DROP
如果要封某个网段:
iptables -I INPUT -s 1.2.3.0/24 -j DROP

#清空屏蔽IP
iptables -t filter -D INPUT -s 1.2.3.4 -j DROP
iptables -t filter -D OUTPUT -d 1.2.3.4 -j DROP

#一键清空所有规则
iptables -F

#查看

iptables -L INPUT

iptables -L

iptables-save(此命令将保存规则,下次开机自动执行)

#处理IP碎片数量,防止攻击,允许每秒100个
iptables -A FORWARD -f -m limit –limit 100/s –limit-burst 100 -j ACCEPT
#设置ICMP包过滤,允许每秒1个包,限制触发条件是10个包
iptables -A FORWARD -p icmp -m limit –limit 1/s –limit-burst 10 -j ACCEPT

 

 

 

 

3 thoughts on “Linux禁用ping

  1. You really make it seem really easy along with your presentation but I in finding this topic to be actually something that I think I might by no means understand. It seems too complicated and very large for me. I am looking ahead on your subsequent publish, I will try to get the cling of it!

  2. Woah! I’m really digging the template/theme of this blog. It’s simple, yet effective.
    A lot of times it’s very difficult to get that “perfect balance”
    between superb usability and visual appeal. I must say that you’ve
    done a great job with this. In addition, the blog loads very quick for me on Chrome.
    Excellent Blog!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据