2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「rm -rf /」を使ってみる

Last updated at Posted at 2024-03-24

検証用のEC2を停止・削除するので、その前にせっかくだから知識としてしか知らない滅びの呪文、rm -rf /を試してみる。
(ディストリビューションはAmazon Linux 2023)

まずはオプションなしで実行する。

$ sudo rm -rf /
rm: it is dangerous to operate recursively on '/'
rm: use --no-preserve-root to override this failsafe
$ 

そう、「危険なのでどうしてもやるなら--no-preserve-rootを使え」と警告が出る。
現在の大抵のディストリビューションでは警告が出ると聞いてたが、その通り。

では--no-preserve-rootを付けてみると。

$ sudo rm -rf --no-preserve-root /

中略

rm: cannot remove '/sys/module/fat/sections/.rodata.str1.1': Operation not permitted
rm: cannot remove '/sys/module/fat/sections/.text.unlikely': Operation not permitted
rm: cannot remove '/sys/module/fat/sections/.note.gnu.property': Operation not permitted
rm: cannot remove '/sys/module/fat/sections/.retpoline_sites': Operation not permitted
rm: cannot remove '/sys/module/fat/sections/__ksymtab_strings': Operation not permitted
rm: cannot remove '/sys/module/fat/sections/__ksymtab_gpl': Operation not permitted
rm: cannot remove '/sys/module/fat/sections/.rodata.str1.8': Operation not permitted
rm: cannot remove '/tmp': Device or resource busy
rm: cannot remove '/var/lib/nfs/rpc_pipefs/gssd/clntXX/gssd': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/gssd/clntXX/info': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/nfsd': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/cache': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/nfsd4_cb': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/statd': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/portmap': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/nfs': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/mount': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/lockd': Operation not permitted
$ ll
-bash: /usr/bin/ls: No such file or directory
$ ls /
-bash: /usr/bin/ls: No such file or directory
$ 

カーネル関連のファイルなど、それでも削除できない一部のファイルを除き綺麗さっぱり消された模様。
lsコマンドも使えなくなっている。

$ cd /
$ pwd
/
$ 

組み込みコマンドのcdpwdは使えた。

$ type cd
cd is a shell builtin
$ type pwd
pwd is a shell builtin
$ 

ちなみにrm -rf /だから警告が出たが、rm -rf /*だと出ないとも聞いたことがある。
別の機会に試してみようと思う。


2024/4/7追記

rm -rf /*を試してみた。
(今回もディストリビューションはAmazon Linux 2023)

$ sudo rm -rf /*

中略

rm: cannot remove '/sys/module/fat/sections/__ksymtab_gpl': Operation not permitted
rm: cannot remove '/sys/module/fat/sections/__ex_table': Operation not permitted
rm: cannot remove '/sys/module/fat/sections/.rodata.str1.8': Operation not permitted
rm: cannot remove '/tmp': Device or resource busy
rm: cannot remove '/var/lib/nfs/rpc_pipefs/gssd/clntXX/gssd': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/gssd/clntXX/info': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/nfsd': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/cache': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/nfsd4_cb': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/statd': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/portmap': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/nfs': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/mount': Operation not permitted
rm: cannot remove '/var/lib/nfs/rpc_pipefs/lockd': Operation not permitted
$ ll
-bash: ls: command not found
$ ls
-bash: ls: command not found
$ cd /
$ pwd
/
$ 

確かにオプション付けなくても警告出ずに処理が進むことを確認。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?