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

  • 2
    You do not need to run emacs as root to be able to edit system files. You could use tramp emacswiki.org/emacs/TrampMode . For example try opening the file at path: //sudo:root@localhost:/etc/group – jue Jul 12 '17 at 0:25
  • Oh. Thanks a lot. I didn't know that. Now, I look like an idiot having asked that question :) – 1556089774 Jul 12 '17 at 6: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: emacsredux.com/blog/2013/04/21/edit-files-as-root – jue Jul 12 '17 at 7: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. – 1556089774 Jul 12 '17 at 15:36
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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