how to Run sudo command in a Bash script
Clash 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 ?
shell-script sudo
add a comment |Â
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 ?
shell-script sudo
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 withFoxyProxy
inFirefox
. For a reason I don't know I need to restartservice tor
if I want to load pages withFoxyProxy
enabled by default. But if I understand the answer, myservice tor
start one time and just after restart with my username and just allowed forservice tor
. This could be another post.
â Gy0m
Mar 15 at 19:25
add a comment |Â
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 ?
shell-script sudo
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 ?
shell-script sudo
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 withFoxyProxy
inFirefox
. For a reason I don't know I need to restartservice tor
if I want to load pages withFoxyProxy
enabled by default. But if I understand the answer, myservice tor
start one time and just after restart with my username and just allowed forservice tor
. This could be another post.
â Gy0m
Mar 15 at 19:25
add a comment |Â
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 withFoxyProxy
inFirefox
. For a reason I don't know I need to restartservice tor
if I want to load pages withFoxyProxy
enabled by default. But if I understand the answer, myservice tor
start one time and just after restart with my username and just allowed forservice 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
add a comment |Â
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.
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 withsudo
.
â 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
 |Â
show 2 more comments
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.
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 withsudo
.
â 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
 |Â
show 2 more comments
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.
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 withsudo
.
â 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
 |Â
show 2 more comments
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.
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.
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 withsudo
.
â 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
 |Â
show 2 more comments
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 withsudo
.
â 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
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
inFirefox
. For a reason I don't know I need to restartservice tor
if I want to load pages withFoxyProxy
enabled by default. But if I understand the answer, myservice tor
start one time and just after restart with my username and just allowed forservice tor
. This could be another post.â Gy0m
Mar 15 at 19:25