Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I often use the following construct for building and installing a tarball:

sudo -v && make && sudo make install

which will allow me to enter my password immediately and have everything done unattended. This works well except in the rare case that building takes longer than the sudo timeout, which may happen on my rather slow machine with large projects (even when using make -j4).

But when the build takes a long time, that's exactly when doing things unattended has a great advantage. Can anyone think of a shell construct that allows me to input my password immediately, and which has make executing under normal permissions and make install under elevated permissions?

For security reasons, I don't want to configure my user to use sudo without password.

A viable option is to set the timeout to very long, but I'm hoping for something more elegant.

share|improve this question
    
Have your consider configuring your user to use sudo without password ? –  NorTicUs Oct 15 '12 at 12:37
    
Here is a howto about this question. webupd8.org/2010/04/… –  cadadr Oct 15 '12 at 12:49
    
thanks, I know about those options, but I was hoping for something more elegant. I'll update my question accordingly. –  Timo Kluck Oct 15 '12 at 12:52
add comment

3 Answers

up vote 3 down vote accepted

Once you're gained root privileges, you can use sudo again to execute a command as the non-root user:

sudo bash -c "sudo -u $USER non-root-user-command ; root-command"

Note that you need to use double quotes, not single quotes. If you use single quotes, $USER will be replaced with root, which won't achieve the desired outcome. Hopefully, these commands will illustrate my point:

$ sudo bash -c "echo $USER"
david
$ sudo bash -c 'echo $USER'
root

So, let's take the example mentioned in the question:

 make && sudo make install

That would become:

 sudo bash -c "sudo -u $USER make && make install"
share|improve this answer
    
This has the additional advantage of checking the exit value of make before attempting make install. Very nice! –  Timo Kluck Jan 2 '13 at 15:31
add comment

The simplest method that does not require you to alter your system files:

echo {password} | sudo -S -v && make && echo {password} | sudo -S make install

This will allow the script to run without prompting for a password.

However, this method poses a security risk since you're storing your password in plain text.


Some other methods can be found here but most are about editing system files.


edit: If you add a space in front of the first echo the command will not be shown in the bash history.

share|improve this answer
    
excellent! I will accept this as soon as I'm convinced that there's no way to keep the password off the command line. –  Timo Kluck Oct 15 '12 at 15:03
    
Oh this can be made more ingenious by reading a file (SUSE can do that ;) ) –  Rinzwind Oct 15 '12 at 15:05
    
that much is obvious, but it would be nice if we could make it more ingenious by having it still come from an invisible terminal input. –  Timo Kluck Oct 15 '12 at 17:38
    
Can the negative voter please explain their reason? –  Rinzwind Jan 2 '13 at 15:23
    
That was me. I downvoted because of the security risks you mentioned, and because this answer appeared before Timo Kluck's answer. –  Flimm Jan 2 '13 at 15:28
show 5 more comments

I think this is the best way to do it:

make | sudo bash -c 'cat - && make install'

This will start both make and sudo simultaneously, with sudo prompting for your password. In the mean time, make sends all its output to the pipe, which is read by the cat command. After this is done, make install is run. It is invoked from the bash process, which still has elevated permissions, so there's no timeout issue.

share|improve this answer
    
@Flimm: the point is that this will give you the sudo prompt immediately instead of after the make command finishes. –  Timo Kluck Jan 2 '13 at 15:05
    
Ah, yes, you're right. That's clever. –  Flimm Jan 2 '13 at 15:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.