You are not logged in.
Pages: 1
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
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
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
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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
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
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
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
yes right! I removed sudo(s) in my script and just get my key binding have:
sudo /home/myusername/mybashscript
Offline
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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
Pages: 1