Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've faced times when I accidentally ran dangerous commands and lost all my data. How do I set Linux to ask for current user password or root user password when running potentially dangerous commands like rm -rf . or rm -rf / and many other?

share|improve this question
    
Actually, neither of those commands will actually run. Both are blocked already. See here. –  terdon Mar 16 at 18:47

2 Answers 2

There isn't really anything you can do beyond making use of sudo so that you're required to provide your password to run commands that have elevated privileges such as root.

The other tip you'll often make use of, is wrapping destructive commands such as rm so that you're prompted with an "are you sure?" or with rm you can make use of the -i switch which will cause rm to be interactive in nature, asking for confirmation before deleting each file.

Be warned though, this typically gets extremely annoying and you'll likely revert to running with it disabled.

share|improve this answer
    
Adding the -f will cause the -i to not prompt, so it doesn't really help in the rm -rf example. –  jordanm Mar 16 at 21:47

The main problem might be to reliably identify a "dangerous command". But let's assume you have a list of dangerous commands.

You can "overwrite" each binary with a function (similar to a shell alias but more powerful):

rm () {
  if is_dangerous rm "$@"; then
    echo "This is a dangerous command!"
    echo -n "Think well and type uppercase YES to really execute it: "
    read answer
    if [ YES = "$answer" ]; then
      executing rm "$@"
      command rm "$@"
    else
      echo "not executed"
    fi
}

is_dangerous would be a script or function which checks the passed command line against a list.

share|improve this answer
    
i dont think that this will prompt for username and pasword –  klerk Mar 16 at 18:22
2  
@vladeli The question is not about prompting for the username. And though I indeed read the question too fast downvoting such an answer is so ridiculous that I am not going to improve my answer. Let's see what you can offer. –  Hauke Laging Mar 16 at 18:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.