मैं स्क्रिप्ट में sudo का उपयोग करते समय GUI संकेत द्वारा पासवर्ड कैसे पूछूं?


14

मैं ट्रिंकल GNU / Linux का उपयोग GNOME फ्लैशबैक डेस्कटॉप वातावरण के साथ करता हूं। मुझे sudoस्क्रिप्ट में कमांड निष्पादित करने के लिए उपयोगकर्ता के लिए GUI पासवर्ड प्रॉम्प्ट की आवश्यकता है । उदाहरण लिपि पर विचार करें:

zenity --question --text="Do you want to install this package?"
if [[ $? -eq 0 ]]; then sudo apt-get install package
else zenity --warning
fi

जिसे निम्नलिखित तरीके से चलाया जाना चाहिए (भागो) अर्थात टर्मिनल के अंदर नहीं:

स्क्रीन-शॉट

इसलिए, कमांड चलाने के लिए पासवर्ड मांगना आवश्यक है sudoअन्यथा यह नौकरी करने में विफल रहता है।

इसलिए, मैं GUI संकेत द्वारा पासवर्ड कैसे पूछूं?

जवाबों:


20

आप GUI संकेत के माध्यम से -A, --askpassविकल्पों की मदद से पासवर्ड पूछ सकते हैं sudo।

से sudoमैनपेज:

-A, --askpass
                 Normally, if sudo requires a password, it will read it from the user's terminal.  If the -A
                 (askpass) option is specified, a (possibly graphical) helper program is executed to read the user's
                 password and output the password to the standard output.  If the SUDO_ASKPASS environment variable
                 is set, it specifies the path to the helper program.  Otherwise, if sudo.conf(5) contains a line
                 specifying the askpass program, that value will be used.  For example:

                     # Path to askpass helper program
                     Path askpass /usr/X11R6/bin/ssh-askpass

                 If no askpass program is available, sudo will exit with an error.

तो, आप चित्रमय सहायक कार्यक्रम जैसे कर सकते हैं ssh-askpassजो एक पास-वाक्यांश के लिए एक उपयोगकर्ता को संकेत देता सूक्ति का उपयोग कर:

$ which ssh-askpass
/usr/bin/ssh-askpass

तो, निम्न पंक्ति को इसमें जोड़ें /etc/sudo.conf:

# Path to askpass helper program
Path askpass /usr/bin/ssh-askpass

और आपको GUI पासवर्ड प्रॉम्प्ट मिलेगा:

स्क्रीन shot1

आप इसके zenityलिए अन्य प्रोग्राम भी इस्तेमाल कर सकते हैं । उदाहरण मैं निम्नलिखित का उपयोग करें:

$ cat /etc/sudo.conf
# Path to askpass helper program
Path askpass /usr/local/bin/zenity_passphrase

zenity_passphraseएक कस्टम स्क्रिप्ट को कमांड के रूप में सीधे इस्तेमाल करने के लिए कहाँ सेट किया गया है:

$ cat $(which zenity_passphrase)
#!/bin/bash
zenity --password --title="sudo password prompt" --timeout=10

जो इस तरह काम करता है:

स्क्रीन shot2


ध्यान दें:

  • आप GUI प्रॉम्प्ट ( और अप्रचलित हैं और 2019-2020 में छोड़ दिए गए हैं) स्क्रिप्ट के gksudoबजाय (su और sudo के लिए GTK + फ्रंटेंड) का उपयोग कर सकते हैं:sudogksugksudo

    स्क्रीन shot3

  • तुम भी उपयोग कर सकते हैं pkexec( polkit आवेदन) कुछ के साथ आवेदन पत्र / आदेश (दूसरों के लिए यह कॉन्फ़िगर करने की जरूरत है):

    स्क्रीन-शॉट


उदाहरण के लिए pkexec leafpad, pkexec का उपयोग करते समय, यह Cannot open display:पासवर्ड दर्ज करने के बाद देता है । क्या किसी भी प्रकार के विन्यास की आवश्यकता है?
— GTRONICK

कोशिशDISPLAY=:0 pkexec leafpad
— पंड्या

अभी भी काम नहीं कर रहा है, वही संदेश Cannot open display:दिखाई देता है
— GTRONICK

gksuऔर gksudoमर चुका है, 2019 में या उससे पहले छोड़ दिया गया है।
— वोहू
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.