7

When using bash with or without sudo there are many traps. For example when logged in as root between

rm -rf ~/bin

and

rm -rf /bin 

there is just one character but this difference can make you quite unhappy.

To protect myself a little bit of such disasters I use this in my /etc/bash.bashrc (systemwide .bashrc):

if [ $UID -ne 0 ]; then
    # ask me before deleting
    alias rm='rm -i'
else
    # do not delete / or prompt if deleting more than 3 files at a time
    alias rm='rm -I --preserve-root'
fi

With this, I at least have to confirm deleting before running into disaster. Maybe there are even more dangerous commands as rm...

What are the most dangerous bash-commands and to protect myself from day-to-day disasters?

10
  • You can also add -i to cp or mv to protect against dangerous overwrites.
    – Tom Hunt
    Sep 8, 2015 at 15:20
  • 8
    Have you tried not logging in as root?
    – ceejayoz
    Sep 8, 2015 at 15:22
  • 1
    You never know when you are going to type --no-preserve-root accidentally.
    – kasperd
    Sep 8, 2015 at 15:26
  • 1
    @Aaron I find it a lot easier to remember to check my commands more carefully when using sudo. Easy to forget you're in as root.
    – ceejayoz
    Sep 8, 2015 at 16:23
  • 1
    @Aaron, "Linux has not yet evolved..." actually, it has: SELinux. I find it easier to just not have to login to machines at all, and just use a configuration management tool to do everything. Also, anyone still using sudo su - needs to be given a copy of the sudo(8) manpage and a written test.
    – womble
    Sep 8, 2015 at 19:33

1 Answer 1

11

First off, never use root to execute day-to-day commands.

That's the best way to actually expose yourself to disasters.

With that in mind, if you use sudo, you can actually limit commands AND the command options that a user can execute with sudo.

For example, in your sudoers file, you can limit using rm like so:

myuser ALL=(root)   NOPASSWD: rm -r

This would mean that myuser can only use sudo as root and can only execute rm with the -r option.

The sudoers file also support regex so you can really customize what can be executed while using sudo.

A good starting point...

4
  • 7
    Upvoted for "never use root to execute day-to-day commands."
    – Jenny D
    Sep 8, 2015 at 15:32
  • I know that folks will disagree with me on this, but folks have been conditioned to believe sudo prevents such mistakes and it simply isn't true. In fact, it makes super-user level mistakes much easier and more common, in my experience of recovering systems that folks have broken. There is a psychological aspect to this topic however and I know that this will continue.
    – Aaron
    Sep 8, 2015 at 15:47
  • @aaron, I have the exact opposite experience. Since we enforced strict sudo, auditd, sshd and pam settings, the amount of mistakes that are going through has decreased drastically. On top of it it makes it so much easier to know who did what. In turn making it easier to do a rollback in case of problems.
    – Alex
    Sep 8, 2015 at 17:23
  • 1
    Note: If you specify any arguments to a command in sudoers, but did not add a wildcard, then you'll only be able to use the command as you gave it, without additional arguments. The example given will allow sudo rm -r, but not sudo rm -r foo, or sudo rm -ri foo.
    – muru
    Sep 8, 2015 at 18:04

Not the answer you're looking for? Browse other questions tagged .