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.

  • 7
    Does this answer your question? Running kate as root – M. Becerra Aug 11 at 17:29
  • Regarding your recent edit, are you saying this should not be considered a duplicate of your older question Running kate as root? If so, then you may want to explain that further, because the solution you present here is also given there. – Eliah Kagan Aug 15 at 17:12
  • Yes, it is a duplicate. I inserted my solution into the question because I believe it's far simpler than any of the gksudo-related solutions, but I discovered it long after my original post. – Paul A. Aug 16 at 19:29
  • Apparently the solution is indeed version-dependent, since older versions of kate were indeed able to be run via sudo. – Paul A. Aug 17 at 16:25
16

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.)

| improve this answer | |
  • 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 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 at 19:34
  • Yes, it works in this way. Thanks. – kelalaka Aug 16 at 19:37
10

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.

| improve this answer | |
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
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.