Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 446  / 1 Year 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

 Answers
4

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"

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

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

Location: France
Member since Fri, Jan 28, 2022
2 Years ago
rinstracte questions
Wed, Jun 15, 22, 02:09, 2 Years ago
Tue, Jan 24, 23, 01:39, 1 Year ago
Wed, Jun 9, 21, 04:34, 3 Years ago
Sun, Apr 17, 22, 11:38, 2 Years ago
;