PDA

View Full Version : How to prevent removing/deleting/erasing whole filesystem using safe-rm



Fli
08-16-2016, 12:34 PM
On the redhat or debian based Linux system, one can accidentally delete operating system files and make system un-usable.

Solution can be using "safe-rm" instead of the "rm"

When using safe-rm one can blacklist system directories and they will not be accidentally deleted by "rm" command. So the safe-rm should be rm replacement.

Safe-RM homepage: https://launchpad.net/safe-rm

How to install safe-rm on the redhat/debian based Linux (ie CentOS, Ubuntu)?

Debian based first try: apt-get install safe-rm
(i have not tested apt-get way)

Else try this: (I failed to make it working. Failed at step 3, no matter in which PATH dir i coppied safe-rm and renamed to rm, it always deleted blacklisted folder while blacklisted should be skipped)

1. Go to https://launchpad.net/safe-rm and copy URL of the .tar.gz and paste it into following wget command and execute. It will download & extract safe-rm archive

wget https://launchpad.net/safe-rm/trunk/0.12/+download/safe-rm-0.12.tar.gz -q -O -|tar xz

2. Find path to "rm" binary:

which rm

In my case it was /bin/rm

3. Show PATH and move safe-rm binary to the first directory of the PATH

echo $PATH

For example, if your path contains:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/bin

Then, putting safe-rm in /usr/local/sbin and renaming it to "rm" will ensure that
all users on your system use safe-rm instead of the real rm.

So i did:

mv safe-rm*/safe-rm /usr/local/sbin/rm

Removed downloaded directory of the safe-rm:

rm -rf ./safe-rm*

4. Add blacklisted paths that will never be deleted

The system-wide blacklist lives in /etc/safe-rm.conf and you should probably add
paths like these:

/
/etc
/usr
/usr/lib
/var

The user-specific blacklist lives in ~/.config/safe-rm and could include things like:

/home/username/documents
/home/username/documents/*
/home/username/.mozilla

I used following command to add all directories in / to the blacklist:

for i in $(ls -A1 /|grep -vE "\.|-");do echo /$i >> /etc/safe-rm.conf;done;echo -e "These directories will not be deleted:";cat /etc/safe-rm.conf