I want to execute a script that requires root privileges without entering a password. There are similar questions/answers but non seems to work for me.
I placed my script in /home/kf/bin (I added the bin dir myself) and included this directory in my path so I can run it from everywhere.
The script: trim.sh
:
#! /bin/sh
sudo fstrim -v /
I changed the ownership of the script to root
:
sudo chown root:root /home/kf/bin/trim.sh
and made it executable
sudo chmod 700 /home/kf/bin/trim.sh
next I added a line to my sudoers file with visudo
:
kf ALL=(ALL) NOPASSWD: /home/kf/bin/trim.sh
When I login again and execute the script with trim
I still need to enter my password. I know I can make a cron for this but I also want to be able to execute the script manually without entering my password. Any help appreciated.
edit:
My sudoers file looks like this:
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin: /usr/bin:/sbin:/bin"
root ALL=(ALL:ALL) ALL
kf ALL=(ALL) NOPASSWD: /home/kf/bin/trim.sh
%admin ALL=(ALL) ALL
%sudo ALL=(ALL:ALL) ALL
Best Answer
Change the line in the sudoers file to:
I don't recommend, adding the script in
/etc/sudoers
, because the script can be altered and every command (the whole script) would then be executed with root privileges.