Someone could answer me how make one prompt via
pkexec when I've to use two command with authentication?
My easy sample script:
pkexec virsh net-start default; pkexec "/home/user/program";
I'm new in linux environmen, Thanks :)
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
May you take pkexec out of the script? Try create next script where you paste your code (without pkexec) and execute him via pkexec from your script.
#!/bin/bash pkexec ./new_script
#!/bin/bash your command:
Edit.: New Answer
After your conversation with @Thrig, I guess what you are going to do. You want to run both programs on root permissions without double authentication (only once). These two programs are: "virsh" and "gnome-boxes". My previous (above) solution is ok, but not in this case. You wrote to @Thrig that you are considering using "sudo". Why not use "pkexec" and "sudo" together. With the proper completion of "/ etc / sudoers" you will not need to authenticate when you use the "sudo" command in the script. I let myself improve your idea. I hope you like it. I will describe everything step by step.
1. Create three scripts:
a) main.sh - set up the connection, destroy the connection, run gnome-boxes. everything as root
b) net.sh - execute the order
c) die.sh - execute the order
#!/Bin/bash sudo /home/ham/..your..path../net.sh && pkexec /usr/bin/gnome-boxes; sudo /home/ham/..your..path../die.sh; exit
why that? description of operators
#!/Bin/bash virsh net-start default
#!/Bin/bash virsh net-destroy default
2. Edit the "sudoers" file to make the script: b) c) run with root privileges:
$ sudo nano /etc/sudoers
%sudo ALL=(root) NOPASSWD: /home/..your..path../net.sh %sudo ALL=(root) NOPASSWD: /home/..your..path../die.sh
3. Change the owner of the scripts b) c) to root:
$ sudo chown 700 /home/ham/..your..path../net.sh $ sudo chown 700 /home/ham/..your..path../die.sh;
4. Create a rule in polkit for gnome-boxes. The answer: "how to do it?" is here: simple_polkit_rule
5. Edit files:
6. Now run the gnome-boxes application by clicking on its shortcut icon. Finished. From myself I added auto turn off connection when you close the gnome-boxes application.
Many have had similar issues with wanting to run GUI with su privileges. More recently I have looked into the lingering open terminal window. After searching this is what worked for me.
GUI privilege escalation of "su" or "sudo" is not recommended and should never be done with out knowledge of the risks of what your doing.
pkexec can be obtained by the following:
sudo apt install policykit-1
Edit the bottom of this file to where it looks likes this:
sudo nano /usr/share/polkit-1/actions/org.freedesktop.policykit.policy
yours should look like this:
<action id="org.freedesktop.policykit.lockdown"> <description>Configure lock down for an action</description> <message>Authentication is required to configure lock down policy</message> <defaults> <allow_any>auth_admin</allow_any> <allow_inactive>auth_admin</allow_inactive> <allow_active>auth_admin</allow_active> </defaults> <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/pklalockdown</annotate> </action>
After that try:
Adding a custom bin path to terminal: - The Standard is to store users shell programs in
*These are the same location the first is the full path to file the second is short hand for
To add your user /bin to your $USER $PATH simply
terminal will now recognise your shell scripts or programs located in
ADD this to .bashrc
Where "$HOME/bin" is the directory I assume you want to add. This change is only temporary (it works only in the current session of the shell) to make it permanent add the previous line to your .bashrc file located in your home directory.
I prefer this method of privilege escalation for gui apps because i can just pkexec in a shell script to run multiple programs and have the option to save credentials temporarily.
you then can try and add an alias to ~/.bash_aliases:
echo 'alias gedit="pkexec gedit"' >> ~/.bash_aliases
/PKEXEC /SU /GUI /GKsu /POLKIT /policykit-1