8

If I try to use sudo kate to edit a system file, I get the message:

Executing Kate as root is not possible.

So how can I edit a system file?

I put up this question so that I could answer it myself. As far as I can tell, none of the suggestions involving gksudo have any advantage over the solution I proposed: just edit the file as a non-root user, save it when you're done, and provide the root password at that time.

Paul A.
  • 2,141

3 Answers3

17

Just go ahead and edit the file without the sudo. When you're done, save it as usual. You'll get a prompt asking for the system password. Supply it, and you're done. (This might not work for some Ubuntu releases, though.)

Eliah Kagan
  • 117,780
Paul A.
  • 2,141
  • 1
    Did you really execute kate with sudo? Or did you get this message when trying to save the edited file? Being unable to execute Kate with sudo is ust what let me to the solution I offered. – Paul A. Aug 13 '20 at 15:01
  • 1
    Just type "kate" and forget about the "sudo". You don't need it.as long as you supply the root password when you're asked for it. You can do all the editing you want before the save (and even edit it again and save it again). The requirement of providing the root password when you save avoids, as far as I know, any security issues. – Paul A. Aug 16 '20 at 19:34
  • Yes, it works in this way. Thanks. – kelalaka Aug 16 '20 at 19:37
  • What if you are never asked for the root password, even the polkit is running? – Akito Feb 25 '21 at 22:48
  • This solution is far simpler than any others, though it's possible that it won't work on some systems. – Paul A. Feb 26 '21 at 22:36
11

Try SUDO_EDITOR=kate sudo -e /path/to/the/file. This will make sudo copy the file to a temporary directory, your editor is called as your unprivileged user, then when the editor returns (i.e. when it is closed), it will be copied to whatever privileged location.

It also works with any other editor. sudo -e can also be spelled sudoedit.

Yet Another User
  • 2,671
  • 3
  • 23
  • 37
1

You can try the following code and put it into a bash script. The motivation was gksudo with Wayland on Debian Buster. I'm right now not at yet Ubuntu 20.04 so I didn't tested it.

#!/usr/bin/env bash

Enable root access to x-windows system.

Motivation: Trying to run a graphical application as root via su, sudo in a

Wayland session (e.g. GParted or Gedit), will fail. Apps which use polkit to

request administrator permissions for just certain operations and only when

needed are not affected (they are not started as root right away).

[1] https://bugzilla.redhat.com/show_bug.cgi?id=1274451

Based on a Reddit comment.

[2] https://www.reddit.com/r/Fedora/comments/5eb633/solution_running_graphical_app_with_sudo_in/

if (( $# != 1 )); then echo "Illegal number of parameters." echo echo "Usage: wsudo [command]" exit 1 fi

for cmd in sudo xhost; do if ! type -P $cmd &>/dev/null; then echo "$cmd it's not installed. Aborting." >&2 exit 1 fi done

xhost +SI:localuser:root sudo $1 #disable root access after application terminates xhost -SI:localuser:root #print access status to allow verification that root access was removed xhost

Eliah Kagan
  • 117,780
moep
  • 514