16

Is it possible to open a file or application as root from the GUI?

My ideal would be right-clicking on a file or an application and seeing an "Open as Root" choice in the context menu, after which I was asked for my root password.

Somewhat related to this is the ability to allow, for example, a save to take place where originally you had not opened the application as root (e.g., modifying a .conf file in /etc)

chrsmrrtt
  • 161

2 Answers2

10

To run GUI applications with elevated permissions, you can use the gksu command:

You can check to see if it's installed with which gksu and if so it will output the path to the command. If not you can install it with the command sudo apt-get gksu

$ gksu nautilus           # browse files as root
$ gksu gedit /etc/fstab   # edit file as root

This command is a frontend to the su and sudo commands, designed for running graphical programs, so it is used the same way as them. See the man page for more information.

Elder Geek
  • 36,023
  • 25
  • 98
  • 183
0

You should never use normal sudo to start graphical applications as root. Using sudo with graphical apps has the potential to corrupt your environment by allowing root to take ownership of and/or change permissions on critical files that you must own. The forums frequently see panicked requests for help from users who can no longer log in after running graphical applications under sudo.

Please note that many websites and old threads advise the use of gksu. However, such search results are obsolete. gksudo has not been updated for years and is not even available in Bionic (18.04) and higher. gksu has been replaced by pkexec, but even pkexec is being deprecated by the mainline Ubuntu developers. They have taken the position that file manipulation and editing under root should be restricted to the command line.

We can only surmise what the motives were behind this decision: perhaps there are just too many users who run into problems running graphical apps as root. In any case, running graphical apps as root now requires workarounds and additional steps.

Flavour-specific workarounds

There are a number of flavour-specific options for running graphical applications as root:

  1. You can use pkexec on those flavours that support this option. As of 18.04, only Xubuntu supports this option by default, as shown in the following examples:

    pkexec thunar  
    pkexec mousepad /etc/fstab  
    
  2. By default, Kubuntu allows easy access to a root file manager: KDE Launcher → Computer → Root-Dolphin

    From there: (→ edit file) will open up a root instance of Kate.

  3. Mainline

    Ubuntu and Gnome use Nautilus as their file manager. Any flavor running Nautilus will allow you to install the package nautilus-admin which will add two python extensions to Nautilus. These extensions add options that allow root access: Open as Administrator and Edit as Administrator

    It is also possible to install the missing Policykit files for both Nautilus and Gedit. See this site for instructions and links. A knowledgeable user could build further Policykit files for alternate file managers and editors by using the linked files as templates. They are simple XML files that can be edited with a standard text editor.

General workarounds

The following methods will work on all flavors:

  1. Use the command line. Simple text editors like nano are quite easy to learn. If you prefer a quasi-graphical file manager, install Midnight Commander. Both of these apps run under sudo with no problems. Examples:

    sudo mc  
    sudo nano /etc/fstab  
    
  2. Notwithstanding the earlier warning, it is possible to use sudo with graphical apps provided you add the -H flag. This flag is critical: it properly sets root to its own environment instead of improperly inheriting the user's environment. Use of the -H flag is mandatory. Failing to use this flag may corrupt critical system files and prevent you from logging in.

    With sudo -H almost any graphical app can be launched under root within any 'buntu flavor. This includes each flavor's default graphical editor and file manager.

    An appreciable danger with sudo -H is that the -H flag is easy to forget. And all it takes is one omission for the damage to be done.

Source: Ubuntu documentation RootSudo

karel
  • 114,770
  • I wish this were a little more specific about what goes wrong with plain sudo... I've done it dozens of times, and survived; I just don't remember how. I think you just have to re-own a specific X.org-related file. – jpaugh May 23 '20 at 09:16