0

Using sudo visudo I add the line username ALL=(ALL) NOPASSWD: /home/user/script.sh in sudoers but the script.sh does not run on double click. If I add the line username ALL=(ALL) NOPASSWD:ALL in sudoers then the script.sh runs and works when double clicked. How can do it? Thanks.

13
  • Is /home/user in your ordinary user's $PATH? Commented Nov 24, 2023 at 12:22
  • 1
    If you're using sudo, it's apparently to prevent users from having complete access to superuser privileges. Yet you're giving sudo the ability to run a script located in a directory controlled by one user. That one user can change the script to anything desired and now has complete access to superuser privileges - and can give those privileges to anyone else. Commented Nov 24, 2023 at 13:25
  • Your specific line in sudoers allows running this exact file with sudo, but double-clicking does not run the script with sudo. If there are sudo commands inside the script then double-clicking probably results in running them eventually; these are the commands you should consider allowing in sudoers. Allowing sudo /home/user/script.sh does not magically allow every sudo command from within the script when the script itself is run without sudo. Is this what happens? Are there sudo command(s) inside the script? Commented Nov 24, 2023 at 13:36
  • 1
    @Kamil Maciorowski There are commands inside the script that only work with sudo, such as ip tuntap add dev, route add. I tried both with and without sudo, the script .sh does not run in either case. And when you add username ALL=(ALL) NOPASSWD:ALL it runs and works.
    – moninah
    Commented Nov 24, 2023 at 14:39
  • 1
    @doneal24 could you please post that as an answer as it seems to have been what the OP was after?
    – terdon
    Commented Nov 27, 2023 at 8:46

1 Answer 1

1

Setting my comment as an answer. Add this line as the first executable statement in your script

[[ $UID -ne 0 ]] && exec sudo $0 "$@"

This checks to see if you're running as root and restarted the script under sudo with the same arguments. Normal precautions and warnings apply in configuring sudo and with running things as root.

3
  • As I understand it, This only works if you follow this code with sudo -s <<END <command ...> END. Adding sudo to every command doesn't work. For some reason the commands are not processed like sudo.
    – moninah
    Commented Nov 30, 2023 at 14:16
  • 1
    @moninah You are adding sudo once inside the script. Assuming that you have sudo rights to run the script and all commands in the script are in root's PATH and you don't have any aliases that you use it should run. What error message do you get when you add this one line to your script?
    – doneal24
    Commented Nov 30, 2023 at 15:06
  • I don't know what it was connected with, but now if add to each command sudo, then everything works. And if add once sudo, then it also works.
    – moninah
    Commented Dec 9, 2023 at 23:11

You must log in to answer this question.

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