Is it possible to launch nautilus or gedit with elevated privileges?
Sometimes I need to edit files in /etc
. I know I can do this with the console (sudo mv some/file /etc/some/file
, sudo nano /etc/some/file
) etc, but I find this way a bit clunky. Call me spoiled, but I'm really used to GUI tools such as Nautilus and Gedit.
Distributions such as Ubuntu Mate or Mint make it easy: I can right click a folder and then a context menu appears with options such as "open as administrator". From then double-clicking a text file opens it in gedit with elevated privileges.
However, I can't find such an option in the default Unity version of Ubuntu:
Note:
- I am aware of the question Ways to run Nautilus with elevated privileges , but the solution recommended here is to use
gksudo
, which no longer works - I suppose I can always do
sudo gedit /etc/whatever
, but - AFAIK - usingsudo
with GUI tools is heavily ill-advised - I recall some say that it is ill-advised to run GUI tools with elevated privileges at all. This doesn't seem to be the case, however, since some default GUI tools themselves ask for elevation, Ubuntu Software being perhaps one of the most prominent examples:
Is there a way to run Nautilus or Gedit with elevated privileges?
Solution 1:
You can easily have a right-click menu that allows to open a file with root privileges by installing a package nautilus-admin
. This nautilus extension adds two right-click menu options. One of these allows you to open a file as root for editing in gedit. The other entry allows you to open a nautilus window with root permissions.
The package is included in the standard Ubuntu repositories and can be installed with the command
sudo apt install nautilus-admin
It relies on the GNOME's admin backend, i.e., the effect is the same as if you would open the file from the command line with the admin://
URI, e.g.
gedit admin:///etc/fstab
Solution 2:
You can use pkexec
instead of gksudo
. My way of running eg. gedit
with elevated privileges is to create a script, for example /usr/local/bin/su_gedit
, with the following contents:
#!/bin/bash
pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY gedit $*
Then you can create a .desktop
file for this script, for example /usr/share/applications/su_gedit.desktop
(I have not tested if other location than /usr/share/applications
will work), copying its contents from org.gnome.gedit.desktop
and modifying appropriately, ie. changing the Name=
and Comment=
lines to whatever you want (for example Name=Root Text Editor
and Comment=Edit text files with root privileges
) and replacing gedit
by /usr/local/bin/su_gedit
in all Exec=
lines.
After you do this, when right-clicking in Nautilus on a file that normally opens in gedit, and selecting "Open with a different program", you should see your "Root Text Editor" entry on the list. Even if it isn't there, if you click "Show all programs" button, you should be able to choose it.
Another way is to create a similar script that runs elevated gnome-terminal
, and create a launcher for that script eg. in your GNOME panel or on the desktop. From that terminal, you can simply run gedit
as root. That's what I usually do.