asking for root password from within a shell script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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."
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??
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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??
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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:
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...
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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...
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!!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.