LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sudo in shell script (https://www.linuxquestions.org/questions/programming-9/sudo-in-shell-script-883142/)

Fracker 05-27-2011 11:28 PM

Sudo in shell script
 
Hi guyz,

Whats wrong with this line?

Code:

sudo -u user /usr/bin/nohup sh /home/user/somescript.sh &
This should ask for the password then execute the script at background and get back to menu

clvic 05-28-2011 06:03 AM

You should not put the '&' at the end of the line, because the whole "line" goes in background and sudo can't ask you the password.
Put the '&' inside the script "somescript.sh", instead. So that first sudo asks for the password (in foreground), and only then the script goes in background.

Nominal Animal 05-28-2011 09:04 PM

Quote:

Originally Posted by Fracker (Post 4369194)
sudo -u user /usr/bin/nohup sh /home/user/somescript.sh &

It will run the sudo command in the background, instead of the script. Use
Code:

sudo -u user sh -c '/usr/bin/nohup /home/user/somescript.sh &'
instead. With this one, sudo starts the sh shell, which in turn starts the script in the background thorough nohup.

Fracker 06-06-2011 05:39 AM

Thanks that worked for me...

littlejoe5 06-08-2011 03:00 AM

Can this be used to run a script that will work as "superuser"?
 
for example: set an icon on the users desktop to run a script such as:
nohup ~/trial.sh &

that script calls this one:
sudo -u (username) cp /etc/my-resolv.conf /etc/resolv.conf

To replace the domain name servers with your own choice.

What have I got wrong in this?

I used to write some 'bat' files in DOS, but this ain't DOS.

Fracker 06-09-2011 01:02 AM

try this

Quote:

sudo -u username 'cp /etc/my-resolv.conf /etc/resolv.conf'

littlejoe5 06-10-2011 06:45 AM

sudo -u username 'cp /etc/my-resolv.conf /etc/resolv.conf'

I get the error: " sudo -u username 'cp /etc/my-resolv.conf /etc/resolv.conf' "

But your note did prod me into tring several other options. I want this to work under the gksu oprion,so I won't have to go through the routine of calling up a terminal and typing into it. I'd prefer just to have it work from clicking an idon on the desktop. Tis is what finaly works for me:

I put an icon on the panel, and this line in the command portion:
" gksu cp /etc/resolv.conf /etc/m-resolv.conf "

That worked! Thanks for the prod. Now, I'll just have to modify that command to copy from m-resolv.conf to resolve.conf instead of the way it now does.

Dave

rustek 06-20-2011 10:05 PM

@littlejoe5

To run a script as root from a script running as a user
using sudo without a password:

visudo and add a line at the bottom:
user ALL=(root) NOPASSWD: /path/to/root/script.sh

Then inside the user script:

sudo /path/to/root/script.sh

Only "user" can run script.sh without a password with sudo.

This effectively gets around Linux's ban
on SUID root for scripts.

Russ

littlejoe5 06-21-2011 12:41 PM

Thanks rustec. I didn't know that tool existed. Looks like a pretty powerful tool - and maybe dangerous. I'll have to study it.

Dave


All times are GMT -5. The time now is 12:47 PM.