LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 08-27-2006, 02:41 AM   #1
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Arrow asking for root password from within a shell script


i have a shell script which i want to be run as a non-root user, but near the end, it needs root permissions for a few commands... i would like my script to prompt me for the root password before it gets to the part that needs root privilages, so that it can proceed without problems... could you show me how to do it, please??

here's a simple illustration of what i mean:
Code:
#!/bin/sh

./configure && make

# Ask for root password here.

make install

# Drop root privilages here.

echo "Done."
thanks in advance!!!

Last edited by win32sux; 08-27-2006 at 02:44 AM.
 
Old 08-27-2006, 02:47 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
No need to prompt the user for root password, su will do it for you:
Code:
su root -c "make install"
 
Old 08-27-2006, 02:51 AM   #3
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
thanks for the quick reply!!!

wait, so you're saying i just need to insert a line like that into the script?? if so, then wow - i had no idea it would be so simple... thanks again!!!

a follow-up question if i could: how would it work if i needed to execute several lines of commands as root?? would i need to put them all in a function and then give that to su?? or is there a more elegant way??

Last edited by win32sux; 08-27-2006 at 02:52 AM.
 
Old 08-27-2006, 03:00 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by win32sux
thanks for the quick reply!!!

wait, so you're saying i just need to insert a line like that into the script??
Indeed.
Quote:
a follow-up question if i could: how would it work if i needed to execute several lines of commands as root?? would i need to put them all in a function and then give that to su?? or is there a more elegant way??
You can put all the commands in another shell script, not a function (if you mean a shell function).

If you want a self-contained solution, you can also pass several lines to su, like this:
Code:
su root -c "
make install
touch /tmp/done
ls -l /tmp/done
"
Beware of double quotes within the command though.
 
Old 08-27-2006, 03:05 AM   #5
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
awesome!! thanks so much man!!
 
Old 08-27-2006, 07:32 AM   #6
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
i'm trying this on my ubuntu box for now (will do it on slackware later)... the thing is, ubuntu doesn't like "su", so i'm using sudo for now... my question is: what would be the proper way to achieve your multi-line example with sudo instead of su:
Quote:
Originally Posted by jlliagre
Code:
su root -c "
make install
touch /tmp/done
ls -l /tmp/done
"
would i need to put a sudo command on *each* line like this??:
Code:
sudo make install
sudo touch /tmp/done
sudo ls -l /tmp/done
or is there a way i can get the self-contained type of thing like you did with the quotation marks and su??
 
Old 08-27-2006, 07:50 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The problem is su and sudo doesn't really work the same way, if you don't care giving root shell rights to the user account, you can try something like:
Code:
sudo /bin/bash -c "
make install
other commands
"
 
Old 08-27-2006, 08:01 AM   #8
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
interesting, thanks...

question regarding that: it looks like that is executing the commands in a new shell, right?? would variables which were set by the non-root user in the script (prior to the "sudo /bin/bash -c") be retained (and usable)??

also, when you mentioned the root shell rights, it sounds like using sudo is more dangerous, i'm a little confused... i mean, in both cases (su and sudo) i get prompted for the root password...
 
Old 08-27-2006, 08:18 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by win32sux
interesting, thanks...

question regarding that: it looks like that is executing the commands in a new shell, right?? would variables which were set by the non-root user in the script (prior to the "sudo /bin/bash -c") be retained (and usable)??
As far as I know, exported variables are preserved.
Quote:
also, when you mentioned the root shell rights, it sounds like using sudo is more dangerous, i'm a little confused...
Sudo is less dangerous than giving the root password to a user.
What I meant is allowing bash root access for a user is like giving him/her full root access for any command.
Quote:
i mean, in both cases (su and sudo) i get prompted for the root password...
Nope, with sudo, you are prompted for the user's password, not root's one.
 
Old 08-27-2006, 08:27 AM   #10
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by jlliagre
As far as I know, exported variables are preserved.
great... i'm still working on my script but i should be able to test it properly soon...

Quote:
Sudo is less dangerous than giving the root password to a user.
What I meant is allowing bash root access for a user is like giving him/her full root access for any command.
oh, okay... i understand what you meant now - thanks...

Quote:
Nope, with sudo, you are prompted for the user's password, not root's one.
hehe, yeah, my bad... thanks for clearing that up...
 
Old 08-27-2006, 11:02 AM   #11
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
just wanted to let you know that i've finally tried the script on my slackware box and it worked FLAWLESSLY... i ended-up using your original "su -c" cuz i was too lazy to configure sudo on slackware... anyhow, just wanted to share the good news and thank you once again!!!

Last edited by win32sux; 08-27-2006 at 11:03 AM.
 
Old 08-27-2006, 02:42 PM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You're welcome, thanks for the feedback.
 
  


Reply



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
get password through shell script shashwat.gupta Programming 6 06-14-2006 01:42 AM
How to tell shell script a password? Etoile Linux - Newbie 5 04-05-2006 09:16 PM
Shell Script For Password cpope67 Programming 4 01-10-2005 02:16 AM
Password Shell Script Solaris 9 cpope67 Solaris / OpenSolaris 1 12-30-2004 03:15 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM

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

All times are GMT -5. The time now is 08:00 PM.

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