102

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.

sorin
  • 10,135

11 Answers11

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
wjandrea
  • 14,504
edm
  • 4,035
9

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.

pedromendessk
  • 191
  • 1
  • 5
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

Eliah Kagan
  • 119,640
2

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

Leszek
  • 21
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
Daniel
  • 159
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.

Syndog
  • 121
1

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

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.

Zanna
  • 72,312
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

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.

dez93_2000
  • 1,241
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.

Dominic108
  • 1,407