Ubuntu – How to call a dialog for authentication

application-developmentpythonSecurity

I am writing a gui program using wx module in python. In program whenever I am accessing a folder which requires root privilege then it asks password at terminal but I want to display the dialog to user to enter the password and authenticate him. How to call that dialog and authenticate the user in ubuntu?

Best Answer

  • There are two different Q&As at stackoverflow answering your question: here and here.

    Both answers use the command gksudo (already mentioned by @khamer). If you run gksudo command (also without python) it will basically do what sudo does, but with a graphical interface - i.e. ask the user for a password and then run the command as root if the user is in the sudoers file.

    To implement the suggested solution from the linked answers in python, you can use something as shown by the following example:

    Create a file run.py:

    #!/usr/bin/python
    
    import subprocess
    subprocess.call(['gksudo','python create.py'])
    

    And a file create.py:

    #!/usr/bin/python
    
    # Create test file..
    f = file("mytestfile", "w")
    

    Then run python run.py and after you enter your password a file owned by root will be created. If you run python create.py it will be owned by you (the file should not exist before running the script).