99

Mainly I am looking for a way to pin an application to the Unity launch bar, and run it as root.

Currently, even if I start the application with sudo, and pin it after it starts, it will start as current user next time.

Sad, but there is no context menu option "run as ...".

Update: I switched to gnome3 and now I'm happy, I'm missing only the search.

3
  • 1
    If you know the risk of running graphical apps as root and still want to continue then follow step 1 here: askubuntu.com/a/118483/41562
    – edm
    Apr 4, 2012 at 10:31
  • You did not understand the question, I know how to use sudo and I already tunned it. The question is about how can I create a shortcut to Ubuntu Unity launcher to app that I want to run as root.
    – sorin
    Apr 4, 2012 at 11:40
  • For unity launcher, I don't know. But if you have a file residing on your desktop and you want to run it as gksu, then you can install "nautilus execute" extension. Mar 24, 2013 at 10:28

11 Answers 11

100

If you want the app to always run as root

  1. Pin the application to the launcher as normal.

  2. Locate the applications .desktop file which will be in either:

    • /usr/share/applications/APPNAME.desktop
    • ~/.local/share/applications/APPNAME.desktop
    • or somewhere else, use locate .desktop|grep APPAME
  3. Open with gedit:

    gksudo gedit /usr/share/applications/APPNAME.desktop
    
  4. Then change the line

    Exec=APP_COMMAND
    

    to

    Exec=gksudo -k -u root APP_COMMAND
    
  5. Save

This command will also keep your environment which is very usefull if you need to connect to others servers and use your private key.

To add a quicklist option to 'Run as root'

Follow steps 1, 2 and 3 above

If the launcher currently doesn't have any other quicklist options, just append this to the end of the document

X-Ayatana-Desktop-Shortcuts=Runroot

[Runroot Shortcut Group]
Name=Run as root
Exec=gksudo -k -u root APP_COMMAND
TargetEnvironment=Unity
13
  • 63
    Why is it so extremely complicated to set a simple Shortcut ? What wrong with the creators of ubuntu ?
    – Sliq
    Dec 13, 2012 at 9:36
  • 7
    for gksudo command, you have to install sudo apt-get install gksu
    – Arul
    Jul 8, 2015 at 16:30
  • 6
    @Sliq its called security... Having a desktop icon that is executed as root without needing a password... is very dangerous... To me this is actually the joy of linux... If i wanted an easy life.. i would be using windows as my main system...
    – Angry 84
    Mar 3, 2016 at 1:03
  • 3
    @sliq "Why is it so difficult to grep from cmd prompt? what is wrong with the creators of Windows?" - Creators of Ubuntu... probably Jan 26, 2017 at 23:30
  • 7
    outdated for Ubuntu 18.XX Can someone provide an updated solution?
    – JRsz
    Jun 18, 2018 at 12:07
8

As this is an old question, with an old answer; To complement @edm's amazing explanation.

As of Ubuntu 18.04 and later, gksudo (gksu) is deprecated and so, it does not have an install candidate in the official repositories. An alternative is to use pkexec.

Edit the file /usr/share/applications/APPNAME.desktop and change the line

Exec=APP_COMMAND

to

Exec=pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY APP_COMMAND

More details on pkexec here.

3
  • 1
    this causes a password prompt but doesn't then launch the program for me. /usr/bin/nvidia-settings is the prog, FWIW.
    – dez93_2000
    Mar 25, 2020 at 18:36
  • 1
    agreed with dez, this seemed really promising but didn't launch the app. Where do the logs go?
    – tom
    Dec 21, 2021 at 13:56
  • 1
    @tom @dez93_2000 @pedromendessk You can wrap the whole EXEC_COMMAND in /bin/sh -c "EXEC_COMMAND". Or written out, the line would be Exec=/bin/sh -c "pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY APP_COMMAND". This seems like the least invasive method.
    – Dunkelkoon
    Jan 19, 2022 at 21:13
5

In the Main Menu under System, Preferences, type gksudo in the Command field of Launcher Properties, e.g.:

/usr/bin/gksudo /usr/bin/yourapplication

Your Name field might say:

sudo yourapplication

So users know they may see a password popup and why...

I am not sure about Unity Launcher vs Main Menu. Maybe this helps?

Good luck

2
  • 4
    You probably know that straight sudo shouldn't be used to run graphical applications as root--after all, you are using gksudo (which is one of the good ways). For the same reason, it is inadvisable to use sudo in the name field the way you have, because doing so (1) creates the impression among sufficiently experienced users that your launcher is incorrectly made, and (2) creates the impression among less experienced users that it is OK to use straight sudo to run graphical applications as root. Jun 25, 2012 at 0:23
  • 4
    Unable to locate package gksudo - Ubuntu 18.04.1 LTS
    – 8bitjunkie
    Feb 27, 2019 at 15:41
2

The following seems to do the same thing, without needing private attributes:

Actions=Runroot;Another

[Desktop Action Runroot]
Name=Run as root
Exec=gksudo -k -u root APP_COMMAND

[Desktop Action Another]
Name=Another action
Exec=whatever
1
  • 7
    Unable to locate package gksudo - Ubuntu 18.04.1 LTS
    – 8bitjunkie
    Feb 27, 2019 at 15:41
2

As a quick and simple alternative to the cumbersome demands described in the answers here, I've gotten in the habit of doing the following...

  1. Launch a new terminal.
  2. Enter gksu nautilus

    (sudo -i nautilus works, too.)

The resulting window falls under the sudo umbrella, so you can use it to access and launch files with sudo-root privileges to your heart's content until it closes. This comes in handy for transitory situations where you don't need a more permanent solution. I use it routinely to extract compressed files into protected system folders, for instance.

4
  • 3
    Using sudo with graphical applications is NOT a good idea. Use gksu or if you must sudo -i.
    – Seth
    Feb 20, 2014 at 17:05
  • 3
    @Seth: Updated. Also... A little explanation as to why would not be remiss, rather than just cryptically saying it's not a good idea.
    – Syndog
    Feb 20, 2014 at 19:34
  • An interesting thread on the topic : crunchbang.org/forums/viewtopic.php?id=8468
    – Syndog
    Feb 20, 2014 at 19:47
  • here: askubuntu.com/questions/11760/…
    – Seth
    Feb 20, 2014 at 20:36
1

Im posting this in-respone to the question @Chris asked in comments :

Suppose you want to create a launcher which will open "file manager(nautilus)" as root. Then you can do this:

Open terminal. Type:

sudo -H gedit /usr/share/applications/nautilus_root.desktop

Hit enter. When asked, enter your login password and hit enter. This will open gedit (text editor). Now, paste the following there:

[DesktopEntry]
Encoding=UTF-8
Name=Nautilus(Root)
Comment=FileManager
Exec=gksu nautilus
Icon=file-manager
Categories=System;
Type=Application
Terminal=0

Now, just save it and exit. You are done. Now if you search on dash then you will find a new launcher "Nautilus (Root)". Clicking this will ask you password, and upon authentication nautilus will be opened as root.

Note: Be careful when you are running "Nautilus (Root)". In this mode you can delete almost anything from / and break the system.

1

For Ubuntu 22.04 and quick-list 'Run as root' command in the launcher, this is what worked for me:

  1. In terminal, run echo pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY and copy the output.
  2. Open and edit your app shortcut config, usually by
sudo nano /usr/share/applications/YOUR_APP.desktop
  1. In [Desktop Entry] section add at the end Actions=Run as root
  2. And below add
[Desktop Action Run as root]
Name=Run as root
Exec=pkexec env DISPLAY=:0 XAUTHORITY=/run/user/1000/gdm/Xauthority YOUR_APP
OnlyShowIn=Unity
  1. In Exec= line, paste line from first step before YOUR_APP command.

If all good additional command, Run as root should appear after right-click on YOUR_APP icon. In my case, it was almost immediately, but I had to restart the application.

Run as root menu

1
  • It works for PopOS as well! The key here is that you have to hardcode the ENVs like you mentioned. It's weird but it looks like either the $DISPLAY, $XAUTHORITY or both are different when running from the launcher. I can't find the logs to confirm what the values are.
    – usersina
    Dec 10, 2022 at 10:33
0

This should perhaps be a comment on Daniel's solution. It works for me, even in Ubuntu 15.04. I had to put the command between quotes Exec=gksu "nautilus --new-window" because of the extra argument. Also, somehow, I don't know why, I had to edit the launcher directly in order to keep the original menu (together with the new option). If you drag your own launcher, it will get registered, but you will not have the same menu.

In Ubuntu 15.04, the launcher for nautilus is usr/share/applications/org.gnome.Nautilus.desktop. You have to use the command line to see the real file name.

Further research lead me to what I believe is a better option : http://ubuntuhandbook.org/index.php/2014/04/ubuntu-14-04-add-open-as-rootadministrator-to-context-menu/ . Instead of opening Nautilus entirely as Root, this allows you to open selected directories as Root within Nautilus. Anyway, I checked, and these two options are compatible. If you open Nautilus as Root and then use the second option to open a directory as Root, it simply opens it without asking the password.

0

To create a shortcut that executes a script with root permissions on Ubuntu 20.04, you can use:

  1. Add the following line to /etc/sudoers (sudo gedit /etc/sudoers)

    • YOURUSERNAME ALL = NOPASSWD: /script/path
  2. Create a desktop shortcut:

    • gedit ~/Desktop/myshortcut.desktop
  3. Add the following content:

    #!/usr/bin/env xdg-open [Desktop Entry] Version=1.0 Type=Application Terminal=false Exec=sudo /script/path myarg Name=Shortcut Name Comment=Shortcut Comment Icon=/usr/share/icons/Humanity/actions/16/media-playback-start.svg

  4. Right click and the shortcut and choose Allow Launching:

enter image description here

1
  • Yes, interesting. But the thing will be started with sudo. What if I want to start a GUI? Since from 18.04 on gksu is gone and starting GUIs with sudo is considered bad what is the actual real thing to do it?!
    – bomben
    Jul 8, 2020 at 11:58
0

As of Ubuntu 18.04 gksu is depreciated, pkexec requires an install and doesn't always work. Here's another way:

Per edm's answer, edit the .desktop EXEC line, but prepend only with "sudo ", e.g. /usr/bin/nvidia-settings becomes sudo /usr/bin/nvidia-settings. MenuLibre users (and maybe other GUI menu editors): the Command line in MenuLibre sends to the EXEC line in in the .desktop file, so you don't need to bother digging the .desktop file out.

If you try this out from the menu now, nothing will happen. Sudo is called with no GUI so nothing happens and it fails silently. This is expected.

In terminal: sudo visudo then add at bottom (right click paste):

YOURUSERNAME ALL=(ALL) NOPASSWD: /usr/bin/nvidia-settings

ctrl+o to save, enter, ctrl+X to close file. Should work now.

0

Since I was also stuck on this I finally found a solution that worked for me on Ubuntu 20.x LTS

  1. run echo pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY in a terminal
  2. copy its output
  3. edit the related .desktop file at /usr/share/applications/
  4. insert the output of 1. after Exec= your final command should look something like this Exec=pkexec env DISPLAY=:0 XAUTHORITY=/run/user/1000/gdm/Xauthority /usr/sbin/kvpm
1
  • The question is/was about the old Unity DE on only applicable to it. Standard Ubuntu runs with Gnome DE since many releases ago. Feb 17, 2022 at 11:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .