5

Gnome enforces barriers to viewing files requiring root privileges. Trying to open /var/log/boot.log file with gedit fails, with no visible option to perform this action as root from the GUI file manager. What is the Gnome doctrine for dealing with situations like this? I know that I can use sudo gedit, but that makes it more cumbersome than launching an application from Nautilus, pardon me, Files.


A close-to-ideal solution is provided by Nemo, the file manager in the Cinnamon desktop. It gives you an option to open a directory as a root, and then you can open this file with a text editor.

Another option is to enforce permissions in the editor on file save, not on file open. It could allow opening any file in read-only mode, which doesn't present any safety concern. This used to be done in the past somewhere. The current design is unnecessarily authoritarian.

Paul Jurczak
  • 1,237

4 Answers4

12

On the command line, type:

gedit admin://<full path to file>

You will be prompted for your sudo user password in a popup dialog. After you enter it correctly, your file will open in gedit.

For example:

gedit admin:///var/log/boot.log
Enterprise
  • 12,792
9

I added this as a separate answer, since you mentioned you would like a more convenient (less "cumbersome") way of editing files as root.

You can use the nautilus-admin extension.

This extension adds a right-click context menu item called "Edit as Administrator" to Nautilus. Just right click on the file you want to edit as root, and select this option.

The best way to install this extension is:

sudo apt install nautilus-admin

The recommended technique to edit files as root is to use gedit admin://, as indicated in my other answer. This extension does exactly the same thing, but you don't have to open a terminal and type the command yourself.

terdon
  • 104,119
Enterprise
  • 12,792
4

You can make a small script to reduce the overhead, like this.

#!/bin/bash

absolute_name=$(realpath "$1")

pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY gedit "$absolute_name"

Put the script somewhere in your path and make it executable. For the purpose of demonstration, I called it ggedit.

So you just type (for example)

ggedit /etc/fstab

An authorization window will open, you type in your password, and it opens the file as the superuser.

Source: I almost certainly got this off Ask Ubuntu in the past decades, but I no longer know where

terdon
  • 104,119
2

What is the Gnome doctrine for dealing with situations like this?

The most "official" statement I could find was this page on help.gnome.org:

Edit a file as the root user

It starts with a bold disclaimer:

Editing files as the root user is potentially dangerous, and may break your system in bad ways. Take great care when editing files as the root user.

Their suggestion is to launch gedit from a terminal as the root user:

sudo gedit

Another option is to enforce permissions in the editor on file save, not on file open

In your particular example, this is not possible because /var/log/boot.log is not readable by any user except root. So in order to display its contents, any application would require root privileges.

You may be able to combine @Organic Marble's answer with a .desktop file, so you could right-click -> Open with -> root gedit.

For the (probably) more common case when you want to edit a file that is owned by root and readable by your user, e.g. most config files in /etc, I personally use Kate. It opens the file as your current user, and asks for your password when you attempt to save it. It does require the whole KDE/Qt ecosystem though, which may or may not be acceptable for you.

danzel
  • 6,533