You are not logged in.

#1 2015-06-22 16:07:01

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

[Solved] adding bash script to sudoers

I am trying to execute my bash script as root.

I add this line

myusername ALL=(ALL) NOPASSWD: /home/myusername/mybashscript

at the end of /etc/sudoers file using visudo.
mybashscript is like this for example:

#! /bin/bash
sudo fdisk -l

It still asking for password. what is missing.
thz in advance.

Last edited by duyinthee (2015-06-23 14:59:16)

Offline

#2 2015-06-22 16:32:10

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

Re: [Solved] adding bash script to sudoers

Heh; the bash-script shouldn't also call sudo. Remove that, and then try sudo /home/myusername/mybashscript. fdisk will then run as root

Offline

#3 2015-06-23 04:44:55

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

Re: [Solved] adding bash script to sudoers

Spider.007 wrote:

Heh; the bash-script shouldn't also call sudo. Remove that, and then try sudo /home/myusername/mybashscript. fdisk will then run as root

what if I want to run my script by hitting keybinding?

I have read sudo page of arch wiki 20+ times. It suggests this

USER_NAME HOST_NAME= NOPASSWD: /usr/bin/halt,/usr/bin/poweroff,/usr/bin/reboot,/usr/bin/pacman -Syu

to add in /etc/sudoers file. That is the nearest one to what I want. But see the directories in above suggestion line. They are /usr/bin/..... dirs. In my case, I want to execute a script in my home folder by hitting keys combination. what if I add like this:

USER_NAME HOST_NAME= NOPASSWD: /home/myusername/mybashscript

in sudoers?

I found several posts "how to add acript sudoers" and followed them.
I have tried many times adding these:

myusername ALL=(ALL) NOPASSWD: /home/myusername/mybashscript
myusername ALL= NOPASSWD: /home/myusername/mybashscript
myusername localhost=(ALL) NOPASSWD: /home/myusername/mybashscript
USER_NAME HOST_NAME= NOPASSWD: /home/myusername/mybashscript

all of them do not work. they all ask for password.

but it works if I add like this:

myusername ALL=(ALL) NOPASSWD: ALL

but I don't want like that...... it is ALL

so I think this means sudo seems not to accept /home directory.
or I think a small thing is missing.

I use thunar as my file manager without gvfs in Arch+WM. So this means I need to hit any keybinding to execute a script to even mount a usb. I don't wanna add

mount

command in sudoers' whitelist, I don't wanna mess up my /usr/bin dir adding my a lot of scripts. So what can I do.

Last edited by duyinthee (2015-06-23 05:11:35)

Offline

#4 2015-06-23 05:37:46

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: [Solved] adding bash script to sudoers

The path should not matter as long as it is correct. I suspect that yours is not because it works if you replace the path with "ALL". Please post the output of the following commands:

readlink -f '/home/myusername/mybashscript'
file '/home/myusername/mybashscript'
ls -l '/home/myusername/mybashscript'

And +1 for removing "sudo" from the script. The script itself will be run as root and therefore all commands in it as well, so "sudo" is not needed in the script.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#5 2015-06-23 06:13:28

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

Re: [Solved] adding bash script to sudoers

ok, see...

[myusername@localhost ~]$ readlink -f '/home/myusername/mybashscript'
/home/myusername/mybashscript
[myusername@localhost ~]$ file '/home/myusername/mybashscript'
/home/myusername/mybashscript: Bourne-Again shell script, ASCII text executable
[myusername@localhost ~]$ ls -l /home/myusername/mybashscript
-rwxr-xr-x 1 myusername wheel 22 Jun 23 12:37 /home/myusername/mybashscript
[myusername@localhost ~]$

I put this:

myusername ALL=(ALL) NOPASSWD: /home/myusername/mybashscript

in my /etc/sudoers using visudo.

Last edited by duyinthee (2015-06-23 06:14:01)

Offline

#6 2015-06-23 07:51:44

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [Solved] adding bash script to sudoers

Xyne wrote:

The path should not matter as long as it is correct. I suspect that yours is not because it works if you replace the path with "ALL".

Not using sudo to run the script and expecting the sudoers rule to take effect anyway (or only for sudo invocations from inside the script) would explain it as well (ALL would then make `sudo fdisk` work).

So, duyinthee, does

$ sudo /home/myusername/mybashscript

work or not, and is this what you have been testing or was it without sudo?

Offline

#7 2015-06-23 08:33:23

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [Solved] adding bash script to sudoers

Following on from Raynman, if you put the script in sudoers, without the "sudo" inside it, then in your key binding have:

sudo /path/script

Otherwise, you'll need to add every sudo'd command inside the script to sudoers. If there's not many, you could add them all to a command alias and just use that alias to the entry. That way you could organise multiple scripts like this.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#8 2015-06-23 14:58:47

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

Re: [Solved] adding bash script to sudoers

yes right! I removed sudo(s) in my script and just get my key binding have:

sudo /home/myusername/mybashscript

Offline

#9 2015-06-23 15:31:43

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: [Solved] adding bash script to sudoers

Raynman wrote:

Not using sudo to run the script and expecting the sudoers rule to take effect anyway (or only for sudo invocations from inside the script) would explain it as well (ALL would then make `sudo fdisk` work).

I assumed that he had been testing it with sudo /home/myusername/mybashscript.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

Board footer

Powered by FluxBB