4

Ubuntu 18.04 Gnome de.

I made a simple bash script file that flags my next reboot to choose the windows grub entry. For quick rebooting into windows straight from Linux DE.

The problem is I have to use dconf to modify executable files to ask, so I can click run in terminal where it automatically asks for password. Otherwise, just executing the file does nothing, bc it's waiting for a password input.

Is there a way to run a bash script file like this with inherent sudo privileges so it doesn't need to ask for a password?

EDIT (even though it's directed at @waltinator, because comment formatting is atrocious):

So would

/bin/kill

be the location and name of my bash script file? for example, my bash script file is named restart2windows and is located on my desktop:

/home/myusername/Desktop/restart2windows

so I would just need to add this line to /etc/sudoers (I'm still unclear on editing that file, using visudo, and whether I should edit sudoers.d as advised):

myusername     mymachinename = NOPASSWD: /home/myusername/Desktop/restart2windows

Would this be correct?

EDIT 2

I tried editing sudoers via visudo:

sudo visudo

and added the line I suggested above, below the commented line:

#includedir /etc/sudoers.d

and then tried to run my bash script file from my desktop, and it did nothing. If I choose to run it in the terminal, the terminal is waiting for a password input.

Here is my bash script file contents, very simple:

#!/bin/bash
sudo grub-reboot 2
sudo reboot now

Found this question here and it appears that I need to add sudo somewhere in there? I'm confused by that answer to that question because his line of code from the OP does not match the OP's.

Edit 3:

I submitted this same post in Ubuntuforums.org as well: https://ubuntuforums.org/showthread.php?t=2434878

1 Answer 1

1
+50

Read man sudoers - it will let you grant passwordless access to a single command. Read man visudo, too.

From the man page:

  PASSWD and NOPASSWD

   By default, sudo requires that a user authenticate him or herself before running a
   command.  This behavior can be modified via the NOPASSWD tag.  Like a Runas_Spec, the
   NOPASSWD tag sets a default for the commands that follow it in the Cmnd_Spec_List.
   Conversely, the PASSWD tag can be used to reverse things.  For example:

   ray     rushmore = NOPASSWD: /bin/kill, /bin/ls, /usr/bin/lprm

   would allow the user ray to run /bin/kill, /bin/ls, and /usr/bin/lprm as root on the
   machine rushmore without authenticating himself.  If we only want ray to be able to run
   /bin/kill without a password the entry would be:

   ray     rushmore = NOPASSWD: /bin/kill, PASSWD: /bin/ls, /usr/bin/lprm

   Note, however, that the PASSWD tag has no effect on users who are in the group specified
   by the exempt_group option.

Also, I'll note that "making an existing bash script run as root" is the beginning of many very sad security tales.

Rather than making the script run as root, allow your user passwordless access to the ( full path of ) the 2 commands in your script.

6
  • please see my post edit regarding what you've suggested and what I've interpreted from reading those man pages.
    – BingBong
    Commented Jan 11, 2020 at 19:33
  • "Rather than making the script run as root, allow your user passwordless access to the ( full path of ) the 2 commands in your script." Not sure what you mean exactly, I tried changing up the sudoers edit to say: myusername mymachinename = NOPASSWD: /home/myusername/Desktop/restart2windows/'sudo grub-reboot 2', /home/myusername/Desktop/restart2windows/'sudo reboot now' But when I tried to run the script by double clicking and clicking run at the prompt, it did nothing. At the prompt if I choose "run in terminal" the terminal opens and still is asking for a password....
    – BingBong
    Commented Jan 12, 2020 at 17:07
  • 1
    No. The path of grub-reboot command. Not the path to a script on your Desktop. To find the path of a command: which *name of command*, for example: which grub-reboot. Will return: /usr/sbin/grub-reboot. That is the PATH to apply in you sudoers file . The path to the command you intend to use in your script.
    – da_kingpin
    Commented Jan 14, 2020 at 4:00
  • @da_kingpin that makes much more sense. I'll try that tomorrow night after work. I'm adding a link in the OP to this same post I submitted in Ubuntuforums.org. A guy there has suggested putting this file in root/bin/ and chmod 700 it. Actually, after reading your suggestion I don't think I'd need to move the executable to root/bin/
    – BingBong
    Commented Jan 14, 2020 at 5:05
  • @da_kingpin I just realized that in the script, if I just used the location of the command, how would I designate which grub entry to reboot to? /usr/sbin/grub-reboot is just the command but doesn't indicate which grub entry to choose.
    – BingBong
    Commented Jan 14, 2020 at 23:43

You must log in to answer this question.

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