How to get "edit with root" in the right click menu?

I wanted to use “pkexec” but found the “exec” in “nemo actions” ignores it.
The way round this was to put it in a script and get the “exec” to call it.

This is the script

#!/bin/bash
pkexec $@

and the resulting nemo action

[Nemo Action]
Name=Edit with Root
Comment=Edit as root
Exec=<pkexec xed %F>
Icon-Name=text-editor-symbolic
Selection=s
Extensions=nodirs;
Dependencies=xed;pkexec;
Extensions=txt;sh;fstab;policy;conf;rc;gen;xml;js;json;

Now “xed” doesn’t have a “pkexec” permission in /usr/share/polkit-1/action

Here it is called com.linuxmint.pkexec.xed.action.policy

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
	"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
	"http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd">
<policyconfig>
	<vendor>xed</vendor>
	<vendor_url>xed</vendor_url>
	<icon_name>accessories-xed</icon_name>
	<action id="org.freedesktop.policykit.pkexec.xed">
	 <description>Run 'xed'</description>
	 <message>Authentication is required to run xed</message>
	 <defaults>
		 <allow_any>no</allow_any>
		 <allow_inactive>no</allow_inactive>
		 <allow_active>auth_admin_keep</allow_active>
	 </defaults>
		 <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/xed</annotate>
		 <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
	 </action>
</policyconfig>

I believe it is easier to use the admin protocol

xed admin:/%F

Unfortunately that command does not work in “nemo actions” actually it does but needs two //

xed admin://%F

Alternative suggestion in case you’re willing to try another editor: Open the file with micro editor (Install micro-manjaro package, it has some pre-defined settings).

When you want to save it and you don’t have the required permissions, it’ll ask if it should use “sudo” to save the file…

@robin0800

Can you explain the workflow now you solved it?
I’m interested to use it myself, but it may help others too

This is the nemo action file named “root_editor.nemo_action”

[Nemo Action]
Name=Edit with Root
Comment=Edit as root
Exec=xed admin://%F
Icon-Name=text-editor-symbolic
Selection=s
Extensions=nodirs;
Dependencies=xed;

Thanks

So if i copy this file to /usr/share/nemo/actions it works on the rightclick menu?

Yes as long as you enable “allow executing file as program”.

Thanks, it works great :smiley:

There is an alternative approach to this. Set the environment variable VISUAL to “xed” (I guess. I use “code -nw”) and use the Exec is “sudo -e %F”

It creates a copy of the file in /tmp, opens editor with that copy, and copies it back when the editor quits. So the editor itself does not run as root and does not mess up its configs.

Can’t get yours to work, need more explanation, what is (I guess. I use “code -nw”) for example?

Try entering in a terminal:
export VISUAL=“code -nw” (if you have VSCode, otherwise gedit)
sudo -e /etc/fstab

This should definitely work. I am not exactly sure how to turn that into Nemo action, but it should be possible.

I actually I put it in ~/.local/share/nemo/actions

@robin0800
Maybe this is a better way? In .local it is loaded as a user script, in /usr it is loaded system wide i guess?
Either way it works

One advantage of .local is that you don’t need root if you need to edit the command.

That is true

i been using the dbus-launch method. i’m in gnome so my script looks like this.

#!/bin/sh
if [ -f "$@" ]; then
        sudo dbus-launch gedit "$@" || echo "zenity --password" | sudo -S dbus-launch gedit "$@"
fi
if [ -d "$@" ]; then
        sudo dbus-launch nautilus "$@" || echo "zenity --password" | sudo -S dbus-launch nautilus "$@"
fi
exit 0

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.