How to edit system files with the Kate editor? [duplicate]
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.
Solution 1:
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.)
Solution 2:
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
.
Solution 3:
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