Essential Safety Checklist for Dangerous Linux Commands
This guide outlines critical precautions and best‑practice tips for executing risky Linux commands—such as rm, chmod, cat, dd, tar, and MySQL—by verifying environments, backing up data, using safe aliases, and avoiding common pitfalls that can cause catastrophic data loss.
1. Preparation
Before executing dangerous commands, take a deep breath and verify the target server using
ifconfigor
ip addr, then confirm the working directory with
pwd.
<code>$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:16:3e:34:e9:a9 brd ff:ff:ff:ff:ff:ff
inet 172.19.26.39/20 brd 172.19.31.255 scope global dynamic noprefixroute eth0
inet6 fe80::216:3eff:fe34:e9a9/64 scope link
</code> <code>$ pwd
/etc/nginx
</code>2. rm -rf
The
-rfoption recursively deletes files; a missing space or an unintended
/can wipe entire systems. Examples:
<code>rm -rf ./* => rm -rf /
rm -rf abc/ => rm -rf abc /
</code>When scripting, always check variables before using
rm, e.g., avoid
rm -rf ${p}/*which becomes
rm -rf /if
${p}is empty.
3. chmod
Changing permissions carelessly can be as destructive as
rm. Backup permissions with
getfacl -R / > chmod.txtand restore using
setfacl --restore=chmod.txt.
4. cat
Using redirection incorrectly (e.g.,
cat >> filemissing a
>) can erase file contents. The same risk applies to
echoand other redirection operators.
5. dd
The
dd if=/dev/zero of=/dev/sda bs=512 count=1command formats a disk; accidental execution will destroy data.
6. cp
Enable interactive mode with
alias cp='cp -i'to prompt before overwriting files; the same applies to
mv.
7. tar
Extracting with
tar -xfcan overwrite existing files in the current directory, leading to data loss.
8. vim
Opening large files in
vimmay trigger the OOM killer. Use read‑only mode (
view) or safer tools like
lessor
more.
9. mkfs.*
Commands such as
mkfs.ext4format disks and should never be run on production systems.
10. MySQL
Use
mysql -U(or
--safe-updates) to prevent UPDATE/DELETE without a WHERE clause, set an alias, wrap critical operations in transactions, and prefer
inplacefor DDL to reduce locking.
Online environments are priceless; proceed with caution, prioritize stability over speed, and remember that thorough approvals often take days—saving a few seconds is not worth a disaster.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.