how to Run sudo command in a Bash script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












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



#!/bin/bash

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 ?







share|improve this question






















  • 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 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 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 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 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 at 19:25














up vote
1
down vote

favorite












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



#!/bin/bash

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 ?







share|improve this question






















  • 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 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 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 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 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 at 19:25












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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



#!/bin/bash

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 ?







share|improve this question














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



#!/bin/bash

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 ?









share|improve this question













share|improve this question




share|improve this question








edited Mar 15 at 18:02









Jeff Schaller

31.2k846105




31.2k846105










asked Mar 15 at 17:59









Gy0m

814




814











  • 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 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 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 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 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 at 19:25
















  • 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 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 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 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 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 at 19:25















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 at 18:15




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 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 at 18:21




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 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 at 18:38




@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 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 at 19:09




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 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 at 19:25




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 at 19:25










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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.






share|improve this answer






















  • When I save the sudoers file it says that there is a syntax error at the line I did modifications
    – Gy0m
    Mar 15 at 18:12










  • @Gy0m The path to the command has to be specified exactly. I have updated the answer.
    – Kusalananda
    Mar 15 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 at 18:22











  • @Gy0m Note that the script will have to use the exact same command with sudo.
    – Kusalananda
    Mar 15 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 at 19:28










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430451%2fhow-to-run-sudo-command-in-a-bash-script%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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.






share|improve this answer






















  • When I save the sudoers file it says that there is a syntax error at the line I did modifications
    – Gy0m
    Mar 15 at 18:12










  • @Gy0m The path to the command has to be specified exactly. I have updated the answer.
    – Kusalananda
    Mar 15 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 at 18:22











  • @Gy0m Note that the script will have to use the exact same command with sudo.
    – Kusalananda
    Mar 15 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 at 19:28














up vote
1
down vote



accepted










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.






share|improve this answer






















  • When I save the sudoers file it says that there is a syntax error at the line I did modifications
    – Gy0m
    Mar 15 at 18:12










  • @Gy0m The path to the command has to be specified exactly. I have updated the answer.
    – Kusalananda
    Mar 15 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 at 18:22











  • @Gy0m Note that the script will have to use the exact same command with sudo.
    – Kusalananda
    Mar 15 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 at 19:28












up vote
1
down vote



accepted







up vote
1
down vote



accepted






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 15 at 18:23

























answered Mar 15 at 18:03









Kusalananda

103k13201317




103k13201317











  • When I save the sudoers file it says that there is a syntax error at the line I did modifications
    – Gy0m
    Mar 15 at 18:12










  • @Gy0m The path to the command has to be specified exactly. I have updated the answer.
    – Kusalananda
    Mar 15 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 at 18:22











  • @Gy0m Note that the script will have to use the exact same command with sudo.
    – Kusalananda
    Mar 15 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 at 19:28
















  • When I save the sudoers file it says that there is a syntax error at the line I did modifications
    – Gy0m
    Mar 15 at 18:12










  • @Gy0m The path to the command has to be specified exactly. I have updated the answer.
    – Kusalananda
    Mar 15 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 at 18:22











  • @Gy0m Note that the script will have to use the exact same command with sudo.
    – Kusalananda
    Mar 15 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 at 19:28















When I save the sudoers file it says that there is a syntax error at the line I did modifications
– Gy0m
Mar 15 at 18:12




When I save the sudoers file it says that there is a syntax error at the line I did modifications
– Gy0m
Mar 15 at 18:12












@Gy0m The path to the command has to be specified exactly. I have updated the answer.
– Kusalananda
Mar 15 at 18:20




@Gy0m The path to the command has to be specified exactly. I have updated the answer.
– Kusalananda
Mar 15 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 at 18:22





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 at 18:22













@Gy0m Note that the script will have to use the exact same command with sudo.
– Kusalananda
Mar 15 at 18:22




@Gy0m Note that the script will have to use the exact same command with sudo.
– Kusalananda
Mar 15 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 at 19:28




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 at 19:28












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430451%2fhow-to-run-sudo-command-in-a-bash-script%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?