-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: Add ability to give administrator password to sudo #19180
Comments
@mattbell87 Do |
Wait, I think I might’ve slightly misunderstood your use case. Either way, if you see the issue linked by @adidalal, you’ll see there’s simply no interest in this. We’ll entertain a PR, but for now that’s all. |
No problems, I'll try @adidalal's suggestion for now, it's an interesting hack. |
Yes that works. Thanks @adidalal and @vitorgalvao. For anyone with the same issue (maybe you got here using Google) here is what worked for me:
Explanation: this line gives a password to sudo then calls a loop that keeps the sudo timestamp alive until the parent process (probably bash, see next paragraph) has finished. Notice the single Since bash is needed for this, and I'm calling apps directly from my GUI program, I need to use After thats called you can install all your apps, whether they require sudo or not. Update: Alternatively you can do this, if you don't want a while loop running in the background: # fetch an app (using avast as an example)
brew cask fetch avast
# renew sudo (expires in ~5min)
sudo -S -v <<< 'password' 2> /dev/null
# install an app
brew cask install avast Thanks to @vitorgalvao, see comments below. |
Personally, I’ll strongly suggest against the use of that solution, and again suggest my own. That keep-alive line is potentially insecure, in that it’ll keep running and thus someone that can look at your running processes may get access to your password (there’s a stackoverflow answer with this, somewhere). It also doesn’t work in certain situations, like if you want to break up your script into multiple ones. It’s also hacky and long. My solution, however, is shorter, mode focused (the flags it uses are made exactly for this use case), safer, and more versatile. I say this after extensive trials with that keep-alive line in my own dotfiles, to which I had to find solutions to the limitations. You should be careful with your |
Agreed, I'm definitely for having something more concise and less hacky if possible. The only thing is I can't see where your solution updates the keep alive in the backgound. From
What if the script, or even one command in that script takes longer than 5 minutes? |
@mattbell87 As explained in my solution, I use a function (see the link to my dotfiles, in the comment). Then what I do is call that |
Ok I've tried your solution. It worked fine, except for when a download takes longer than 5 minutes. I used Apple Network Link Conditioner to simulate a long download time @ 192kbps and unfortunately it fails with the error in the original post. # renew_sudo
sudo -S -v <<< 'password' 2> /dev/null
# install an app
brew cask install avast --force |
That’s an interesting case. Personally, I never bumped into that and in this particular situation I’d solve it by |
Good point I didn't think of doing a fetch first! 👍 I just keep thinking of worst case scenarios, like what if You're right about the security part of while loop. I can |
Probably the best solution for your case: sudo -S -v <<< '{{your_sudo_password}}' 2> /dev/null
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null & This way your password will not be shown in your running processes since it will only appear in the first command, and the second will keep the renewing. It still has the problem of renewing indefinitely, but at least it won’t expose the password for the duration of the script. On another note, that keep-alive command is usually shown in the context of that |
👍 awesome! Yeah I had seen that gist after the first couple of comments on this issue. Interesting read there! Thanks for that, I'll go with that solution. |
@vitorgalvao I couldn't get |
@benknoble Do something like: # helper functions
renew_sudo() {
# helper function for when the following command needs `sudo` active but shouldn't be called with it
sudo --stdin --validate <<< "${sudo_password}" 2> /dev/null
}
ask_details() {
# ask for the administrator password upfront, for commands that require `sudo`
clear
bold_echo 'Insert the "sudo" password now (will not be echoed).'
until sudo --non-interactive true 2> /dev/null; do # if password is wrong, keep asking
read -s -p 'Password: ' sudo_password
echo
sudo --stdin --validate <<< "${sudo_password}" 2> /dev/null
done
clear
}
ask_details # get admin password
renew_sudo # to make the Caskroom on first install
brew cask install java All credits to @vitorgalvao -> https://github.com/vitorgalvao/dotfiles |
|
Here's how I'm using it in my dotfiles: https://github.com/reitermarkus/dotfiles/blob/4603215a5920d357883c20ba0fed6da748b9044b/.sh#L66-L99 |
Hi All,
I'm currently building a GUI setup script in QT for my team to automatically install some apps through Homebrew. I want my GUI app to ask for the administrator password. The problem is when
sudo
is called for some apps, there's no easy or reliable way I can see to insert the password.With bash scripts I use this method: https://gist.github.com/mattbell87/90a54e968ed39c9bdf6b which I tried however Homebrew and Cask are written in ruby and wont adhere to the bash function as sudo is called directly.
Perhaps a new environment variable (or similar feature) could be added to Homebrew Cask, that when set it will pipe in the password whenever sudo is called.
Here is an example of a program that needs sudo to install:
My apologies if it's been asked already, I searched the open and closed issues and couldn't see anything.
The text was updated successfully, but these errors were encountered: