LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2011, 11:28 PM   #1
Fracker
Member
 
Registered: Mar 2009
Posts: 90

Rep: Reputation: 0
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
 
Old 05-28-2011, 06:03 AM   #2
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
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.
 
Old 05-28-2011, 09:04 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by Fracker View Post
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.
 
1 members found this post helpful.
Old 06-06-2011, 05:39 AM   #4
Fracker
Member
 
Registered: Mar 2009
Posts: 90

Original Poster
Rep: Reputation: 0
Thanks that worked for me...
 
Old 06-08-2011, 03:00 AM   #5
littlejoe5
Member
 
Registered: Aug 2006
Location: Arizona
Distribution: ubuntu dirivatives mostly Mate
Posts: 260

Rep: Reputation: 19
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.

Last edited by littlejoe5; 06-10-2011 at 05:39 AM.
 
Old 06-09-2011, 01:02 AM   #6
Fracker
Member
 
Registered: Mar 2009
Posts: 90

Original Poster
Rep: Reputation: 0
try this

Quote:
sudo -u username 'cp /etc/my-resolv.conf /etc/resolv.conf'
 
Old 06-10-2011, 06:45 AM   #7
littlejoe5
Member
 
Registered: Aug 2006
Location: Arizona
Distribution: ubuntu dirivatives mostly Mate
Posts: 260

Rep: Reputation: 19
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
 
Old 06-20-2011, 10:05 PM   #8
rustek
Member
 
Registered: Jan 2010
Location: Melbourne, IA, USA
Distribution: Ubuntu
Posts: 93

Rep: Reputation: 8
@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

Last edited by rustek; 06-20-2011 at 10:13 PM.
 
Old 06-21-2011, 12:41 PM   #9
littlejoe5
Member
 
Registered: Aug 2006
Location: Arizona
Distribution: ubuntu dirivatives mostly Mate
Posts: 260

Rep: Reputation: 19
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sudo for www user to run root shell script via browser kzcom Linux - General 11 09-15-2010 01:48 AM
bash shell script "sudo - lunlun Linux - Newbie 2 05-09-2008 09:42 PM
Shell script sudo without password prompt under Darwin? lhouk Other *NIX 3 02-08-2008 11:16 AM
ubuntu - running sudo shell script from php mitilma Linux - Security 3 06-14-2007 01:52 AM
allow sudo for www user to run root shell script cccc *BSD 12 07-23-2005 03:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration