Is there a way to force the sudo
command to ask for a password each time when I'm using sudo rm /path/file
?
I want to protect my self from stupid mistakes, committed due to low attention when I'm running some commands from the history.
Is there a way to force the sudo
command to ask for a password each time when I'm using sudo rm /path/file
?
I want to protect my self from stupid mistakes, committed due to low attention when I'm running some commands from the history.
As noted in other answers,
sudo
can set a timeout on the cached credential timestamp. This can be done specifically for a given command usingDefaults
insudoers
:Always remember to edit
sudoers
withvisudo
. I recommend a drop-in file in/etc/sudoers.d
instead of editing/etc/sudoers
directly. For example:From the manpage for
sudoers
:Note that it does say you can affect "commands being run as a specific user", but doesn't mention commands being run by a specific user, so it might not be possible to constrain it only for your user. The examples in the manpage don't include anything for that.
You can use
k
option withsudo
to reset the timestamp.If
sudo -k
is used as a command, it would expire/invalidate the cached credentials immediately.But if
sudo -k
is used with some command, for example,sudo -k rm /some/file
, the shell would ask for password even if some credentials are already cached. In this case, the new credentials won't be cached. That means if another command withsudo
is executed after that, it won't ask for password (if credentials were cached previously).From sudo's manpage:
If you want to make
sudo
to ask for password for specific commands without usingk
every time, you can define custom functions in.bashrc
. For example, from pa4080's comment:You can set sudo to always ask for a password:
In my configuration I have sudo timeout set at 2 hours (120 minutes) before it asks for a password again. Setting it to
0
will require password every time you usesudo
rm
wrapper scriptSometime ago I wrote a wrapper script for the
rm
command:Some of the notable features:
rm
is called unless it is being used within a batch job likesudo apt-get
orsudo update-grub
.rm
is used it is logged tojournalctl
and/var/log/syslog
.