前几天相信做运维的同学都知道的一个新闻,那就是让你心惊胆战的”rm -fr /”命令。没错,那个新闻就是国外一哥们据说是Ansible的bug,导致执行”rm -fr $foo/$bar”时,$foo/$bar变量并没有获取到值,所以你懂的,命令就变成了”rm -fr /”,但是”rm -fr /”真的会删除整个系统吗?是的,真的会删除,但那是老一点系统版本才会有这么一个天大的bug,比如CentOS5,在CentOS6/7早已不存在这个问题了,系统已经做了安全控制,确切地说,rm -fr /不会被执行,当然你也可以强制执行此命令,一切后果自行承担。当然这些也是我在看了一篇文件后实验得出的结论,下面给出验证过错。
那篇文章是这么说的,根据最新的POSIX.1-2008标准,”rm -fr /”命令是不会被执行的,而是应该打印错误信息。但是老的POSIX.1-2004标准则无此定义,那么这个有什么意义呢?在Linux中,我们所使用的rm、touch、mkdir、cp、mv等命令都是由coreutils这个核心工具提供的,coreutils 5.2稳定版于2004年2月19日发布,而现在coreutils工具在CentOS6上都已经是8.22版本了(CentOS7是8.4版本,Debian8是8.23版本)。只有你的coreutils的版本足够高(5.2版本以上),才可以”安全地”使用rm -fr /这个指令,老版本的rm还是存在此问题的。
下面来验证看看
内核版本和coreutils版本(CentOS6)
[root@localhost ~]# uname -r
2.6.32-431.el6.x86_64
1
2
[root@localhost~]# uname -r
2.6.32-431.el6.x86_64
[root@localhost ~]# rpm -qi coreutils
Name : coreutils
Version : 8.22
Release : 15.el7
.............
Description :
These are the GNU core utilities. This package is the combination of
the old GNU fileutils, sh-utils, and textutils packages.
1
2
3
4
5
6
7
8
[root@localhost~]# rpm -qi coreutils
Name:coreutils
Version:8.22
Release:15.el7
.............
Description:
ThesearetheGNUcoreutilities.Thispackageisthecombinationof
theoldGNUfileutils,sh-utils,andtextutilspackages.
内核版本和coreutils版本(CentOS7)
[root@localhost ~]# uname -r
3.10.0-327.el7.x86_64
1
2
[root@localhost~]# uname -r
3.10.0-327.el7.x86_64
[root@localhost ~]# rpm -qi coreutils
Name : coreutils Relocations: (not relocatable)
Version : 8.4 Vendor: CentOS
1
2
3
[root@localhost~]# rpm -qi coreutils
Name:coreutilsRelocations:(notrelocatable)
Version:8.4Vendor:CentOS
内核版本和coreutils版本(Debian8)
root@localhost:~ # uname -r
3.16.0-4-amd64
1
2
root@localhost:~# uname -r
3.16.0-4-amd64
root@localhost:~ # dpkg -s coreutils
Package: coreutils
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 14249
Maintainer: Michael Stone
Architecture: amd64
Multi-Arch: foreign
Version: 8.23-4
1
2
3
4
5
6
7
8
9
10
11
root@localhost:~# dpkg -s coreutils
Package:coreutils
Essential:yes
Status:installokinstalled
Priority:required
Section:utils
Installed-Size:14249
Maintainer:MichaelStone
Architecture:amd64
Multi-Arch:foreign
Version:8.23-4
开始执行”rm -fr /”,所以以上系统执行此命令都会报以下错误信息(都验证过)
[root@localhost ~]# rm -fr /
rm: it is dangerous to operate recursively on `/'
rm: use --no-preserve-root to override this failsafe
1
2
3
[root@localhost~]# rm -fr /
rm:itisdangeroustooperaterecursivelyon`/'
rm:use--no-preserve-roottooverridethisfailsafe
[root@localhost ~]# LANG="zh_CN.UTF-8"
[root@localhost ~]# rm -fr /
rm: 在"/" 进行递归操作十分危险
rm: 使用 --no-preserve-root 选项跳过安全模式
1
2
3
4
[root@localhost~]# LANG="zh_CN.UTF-8"
[root@localhost~]# rm -fr /
rm:在"/"进行递归操作十分危险
rm:使用--no-preserve-root选项跳过安全模式
可以看出执行”rm -fr /”命令会被不允许执行,说执行rm /十分危险,然后就拒绝了。另外,除了直接使用根目录作为参数,如果这个参数是在经过计算、替换之后、最后和根目录等价;也会提示错误的。下面来看看。
[root@localhost ~]# foo="/tmp"
[root@localhost ~]# far=""
[root@localhost ~]# rm -fr $foo/$far
[root@localhost ~]# foo=""
[root@localhost ~]# rm -fr $foo/$far
rm: it is dangerous to operate recursively on `/'
rm: use --no-preserve-root to override this failsafe
1
2
3
4
5
6
7
[root@localhost~]# foo="/tmp"
[root@localhost~]# far=""
[root@localhost~]# rm -fr $foo/$far
[root@localhost~]# foo=""
[root@localhost~]# rm -fr $foo/$far
rm:itisdangeroustooperaterecursivelyon`/'
rm:use--no-preserve-roottooverridethisfailsafe
同样可以检测出来,然后拒绝执行此操作,但是下面也说了,可以使用–no-preserve-root选项,强制执行”rm -fr / –no-preserve-root”,如果这样的话,整个系统就会被删除的哦!谨慎操作,我是在虚拟机上面试的删除操作哦。系统马上就会死掉了,马上系统就会报一大堆”command not found”,然后重启系统就会发现如下报错:
[root@localhost ~]# rm -fr / --no-preserve-root
1
[root@localhost~]# rm -fr / --no-preserve-root
然后重启系统就挂掉了。
如果您觉得本站对你有帮助,那么可以支付宝扫码捐助以帮助本站更好地发展,在此谢过。