Saturday, September 23, 2023
 Popular · Latest · Hot · Upcoming
rated 0 times [  3] [ 0]  / answers: 1 / hits: 362  / 7 Months ago, thu, march 16, 2023, 6:44:18

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.

More From » sudo


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"
$ sudo bash -c 'echo $USER'

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"

[#34951] Friday, March 17, 2023, 7 Months  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 221
Total Questions: 114
Total Answers: 120

Location: France
Member since Fri, Jan 28, 2022
2 Years ago