Launching GUI Apps as Root with .desktop Files
How to launch GUI applications using .desktop files and bash in Ubuntu.
Edited: 2017-04-10 21:44
When launching graphical applications as root you will normally open up a Terminal, and then launch the program with the sudo -i command. However, if you are often going to launch applications as root, then you may wish to create a shortcut (.desktop) file to easily launch your applications.
In Ubuntu, Nautilus is one of the most common applications to be run as root, so we will use this as an example. To launch this from your own custom .desktop file, follow the directions below:
1: Go to your /home/YourUser from a Nautilus window, and then right-click and choose create new file. You can later move this file to somewhere else if you wish.
2: Edit your new Nautilus Launcher.desktop file with gedit, and then copy and paste the below into the file and save it:
[Desktop Entry] Type=Application Terminal=false Exec=nautilus --new-window Name=Nautilus Icon=system-file-manager Actions=RootWindow;Window [Desktop Action Window] Name=Open a New Window Exec=nautilus --new-window OnlyShowIn=Unity; [Desktop Action RootWindow] Name=Open a New Root Window Exec=gnome-terminal -e "bash -c 'sudo -i nautilus;'" OnlyShowIn=Unity;
3: Right-click the file and choose properties, from the Permissions tab, check the "Allow executing file as program" option, and close the properties window.
You can now drag and drop your new launcher to Unity. Right click the launcher and choose "Open New Root Window" to run Nautilus as root. A terminal window will automatically open, and you will be prompted to enter your root password.
This method can also be used to launch other applications as well, simply change "nautilus" with whatever other app you want to launch.
Explanation
The [Desktop Entry] section determines what will happen if the launcher is clicked normally, and in this case we just open a normal Nautilus window.
The Exec part is the part that determines what will happen when either you click the launcher normally, or right-click on it and choose "Open New Root Window".
This .desktop file has two sections, each with its own Exec part. The [Desktop Action RootWindow] section is what creates the "Open a New Root Window" option when right-clicking on the launcher in Unity.
[Desktop Action RootWindow] Name=Open a New Root Window Exec=gnome-terminal -e "bash -c 'sudo -i nautilus;'" OnlyShowIn=Unity;
In this case, we are launching gnome-terminal, and using it to execute the command "sudo -i nautilus". The Name part is the title of the right-click option.
And finally we also added the [Desktop Action Window] option, which does the same as a left-click on the launcher.
If you want to keep the terminal open after launching Nautilus, simply add "$SHELL" to the end of the Exec part in [Desktop Action RootWindow]:
[Desktop Action RootWindow] Exec=gnome-terminal -e "bash -c 'sudo -i nautilus;$SHELL'"
Tell us what you think:
Thanks for this. Would the directions be the same in 18.04? Was not sure since Unity is no longer in play? Thanks in advance!
jeff