2

I've recently started learning to use Emacs.

I do not want to run emacs using sudo emacs (lets not ask why), so I tried using pkexec to start emacs with root privileges. However, pkexec emacs starts emacs in terminal mode. How can I solve this?

Or is there any other alternative where I can possibly use emacs to edit files with root privileges.

This is the custom policy:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD polkit Policy Configuration 1.0//EN"
"https://www.freedesktop.org/software/polkit/policyconfig-1.dtd">

<policyconfig>
    <action id="com.ubuntu.pkexec.emacs">
    <description>Run Emacs with root privileges</description>
    <message>
        Authentication is required to run Emacs with root privileges
    </message>
    <icon_name>Emacs</icon_name>

    <defaults>
        <allow_any>auth_admin</allow_any>
        <allow_inactive>auth_admin</allow_inactive>
        <allow_active>>auth_admin</allow_active>
    </defaults>

    <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/emacs</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
    </action>
</policyconfig>

Note:

  • A similar policy for brackets works fine.
  • Emacs starts in GUI mode if I use either emacs or sudo emacs in terminal.

Any help is appreciated.

Emacs version 24.5.1 in ubuntu 16.10

1565986223
  • 123
  • 5
  • 2
    You do not need to run emacs as root to be able to edit system files. You could use `tramp` https://www.emacswiki.org/emacs/TrampMode . For example try opening the file at path: `//sudo:root@localhost:/etc/group` – jue Jul 12 '17 at 00:25
  • Oh. Thanks a lot. I didn't know that. Now, I look like an idiot having asked that question :) – 1565986223 Jul 12 '17 at 06:09
  • 1
    No, you do not. Besides, with tramp you could also edit files on a remote computer using ssh (and other protocols), this is pretty nice. :) For editing with `sudo` also have a look here: http://emacsredux.com/blog/2013/04/21/edit-files-as-root/ – jue Jul 12 '17 at 07:28
  • I think I'll reword my question a little bit so that you can incorporate your comments along with some more helpful guide as answer. I think that way it'll fit the stackexchange model and help other users like me. Thank you! once again. – 1565986223 Jul 12 '17 at 15:36

1 Answers1

4

An alternative, to pkexec emacs or sudo emacs, would be to run emacs as normal user and work with tramp-mode to access files.

tramp-mode enables you to open files with root rights using access methods like sudo, su, ssh, sftp and so on, see here.

For example use this C-x C-f //sudo:root@localhost:/etc/group RET to open systems group file.

tramp also enables you to open files on remote hosts (tunneling and multihops are also possible).

For helper functions to reopen an already opened file as root, have a look here.

jue
  • 4,476
  • 8
  • 20
  • I'd like to add that this is the preferred method. Emacs is run a very complex program to be givne permanent privilege escalation, so in the spirit of polkit, it only requests them when it needs the PE, and relinquishes as soon as the buffer is closed. – Alex Petrosyan Dec 07 '20 at 10:04