I just need to restart tor service at startup the script is simple as that :


sudo service tor restart

But I want to execute it without taking my password at every startup.

I followed these posts : https://askubuntu.com/questions/425754/how-do-i-run-a-sudo-command-inside-a-script/425990

and I changed my sudoers file with this :

username ALL=(ALL) NOPASSWD: /path/to/script

But as I try to execute it in terminal, the script always ask me for password. I also try the others answers in this post but as that post as no validation I didn't find a solution. If someone have a solution ?

  • What operating system are you using? If Linux, what distribution? If this needs to be run at startup, why don't you just set the tor service to start on boot?
    – terdon
    Mar 15, 2018 at 18:15
  • Please edit your question and clarify what you need. Do you just want tor to be started every time the machine reboots?
    – terdon
    Mar 15, 2018 at 18:21
  • @terdon I find an answer and I think the question can help the community. The reason why I ask this question is minor and I think touch less people. And I learned things :)
    – Gy0m
    Mar 15, 2018 at 18:38
  • OK, but the answer you got is really not a good way of starting a service at boot, if that's what you want. It means any user on the system will be able to restart tor with no password. For a cleaner way, see How to write startup script for systemd
    – terdon
    Mar 15, 2018 at 19:09
  • Thanks, that's a good point. I use service tor with FoxyProxy in Firefox. For a reason I don't know I need to restart service tor if I want to load pages with FoxyProxy enabled by default. But if I understand the answer, my service tor start one time and just after restart with my username and just allowed for service tor. This could be another post.
    – Gy0m
    Mar 15, 2018 at 19:25

2 Answers 2


The line

username ALL=(ALL) NOPASSWD: /path/to/script

gives username the right to run the script with sudo, as in

$ sudo /path/to/script

If you want the user to be able to restart tor specifically, with sudo in the script, use

username ALL=(ALL) NOPASSWD: /usr/sbin/service tor restart

Now the script may do

sudo /usr/sbin/service tor restart

Note that this command has to correspond exactly to the sudo configuration.

  • When I save the sudoers file it says that there is a syntax error at the line I did modifications
    – Gy0m
    Mar 15, 2018 at 18:12
  • @Gy0m The path to the command has to be specified exactly. I have updated the answer.
    – Kusalananda
    Mar 15, 2018 at 18:20
  • Yes, that is the right answer now it load without asking the password :) I have a last interrogation : is it safe to do that ?
    – Gy0m
    Mar 15, 2018 at 18:22
  • @Gy0m Note that the script will have to use the exact same command with sudo.
    – Kusalananda
    Mar 15, 2018 at 18:22
  • This way works perfectly for my intention, but I'm also asking myself about security issue that could happened as terdon says
    – Gy0m
    Mar 15, 2018 at 19:28

Best practice would be NOT to use sudo in the script.

  1. wring the script without the use of sudo
  2. chmod script file to root and take all permissions away from user and others.
  3. Run the script with sudo

You must log in to answer this question.

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