interactive bash script to ask for superuser password
Hi everyone.
This is an old question asked many times, which, however, is NEVER answered directly in any manual I've checked. So... I'm writing a bash install script (instead of a rpm or .deb package) that must be run by a normal user. Then at a certain point the script needs to change to superuser (asking for password and receiving it) and the rest of the script to be executed in the superuser mode in order to install what I mean to install. I know how `sudo ...` or `su `root -c "..."` or `gnome-terminal -e ...` can achieve this purpose by creating certain batch files and then give them as argument to these commands. That's NOT what I'm asking, however. I want to know how I can make the script interactively switch to superuser mode and go on running the rest of the script (can be a lot of code) in that mode. I don't mind if it opens a separate terminal window to do that; just how can that be achieved? Thanks in advance, hope I'm expressing myself clear enough :D. |
If you include one 'sudo' command in the script, then any other commands prefixed by sudo will run without asking for passwords :)
Code:
#!/bin/bash |
OK, great thanks. I'll try this one out; however, let me describe just WHY I'm thinking of this "abnormal" thing I'm asking about.
I often have to help beginners to install linux, and quite often they're people who're not going to be really deep into it. Say, after finishing installation I just noticed that I forgot to install a iptables script which I usually use. But going back to my friend's place is taking time... So my dirty fix is: I email him a file, bash script executable, which will install the needed files into the system once the user clicks on it. Such a dirty fix when small changes are needed to have it up and running ASAP. Then, as the system in question may not have the "/etc/sudoers" file setup properly (unless it is Ubuntu), using `sudo $command` may not work. Therefore, starting a root subshell will be more universal. But in the Advanced Bash Scripting Guide in the chapter on "subshells" there is nothing about starting a different user subshell inside a script. So I wonder if it is possible, or I'll always have to define a huge piece of code as $COMMAND and then use Code:
`su root -c "$COMMAND"` Code:
`gnome-terminal -e $COMMAND` At least, in the install scripts I studied folks use this approach. |
I had a play around and looked in the man pages, but everything I tried just took me to a root prompt, then executed the rest of the script once I quit that... Sorry :)
|
Quote:
OK, so far I'm using this way: Code:
#!/bin/bash But it seems one cannot supply more than ONE command to `su` in a non-interactive way. ...Well ACTUALLY, all the stuff can be done without root privs and then only the crucial operation like copying it into SYSTEM location be done with `su root`. But hey, I wanna know in general, if it is possible in a bash script to start a non-interactive subshell from another UID. Or should one use other languages for that purpose? OR, alternatively, how can one supply several commands to `su`? I'll try several things and report my results here, too. Thanks for your answers anyway. Kostya |
Quote:
Code:
su -c 'cd /root; mkdir mydir; touch mydir/file' Code:
xterm -e su -c 'cd /root; mkdir mydir; touch mydir/file' If it works OK, then this will be the answer to my question. Bye! |
Code:
#!/bin/bash It is more complex than is really needed, but this way the user does not need to set any permission bits. You can tell the user to save the script as say fixer.txt in their home directory. Then, they need to open a terminal and type Code:
. fixer.txt The scriptlet at the beginning checks whether it's being sourced or executed, and works accordingly. Before su-ing, it adds read and execute bits to itself. It is completely automagic: it uses various bash variables to find out its own name and path. Nominal Animal |
Why thank you for this interesting example, must take a closer study later on :).
You know, I learn better from examples, than from "dry theory"... |
I'm happy to help.
Here's a breakdown on the scriptlet (from my above post #7):
I think it's still sensitive to backslashes (\), variable expansion ($), and history expansion (!), so avoid those three characters in the file names. They are escapable, but I'm striving for maximally useful, not perfect ;) If the patterns interest you, you can test them via e.g. Code:
x="It's a Chëëzbrgr!" ; echo "x becomes \"${x//\"/\\\"}\" in the output" Nominal Animal |
All times are GMT -5. The time now is 08:05 AM. |