3

Sometimes, when I am running an emacs process (either started withemacs -nw or emacsclient -nw with an emacs server daemon), I would like to edit a system file. Can I acquire root privilege without restarting emacs with sudo?

If I am using an emacs process started with emacsclient -nw, do I need to restart the emacs server process with sudo, i.e. sudo emacs --daemon?

Without restarting emacs server with sudo, is it possible to start emacs client with sudo? I can't find a solution:

$ ps aux | grep emacs
t         3235  0.0  0.3 398732 24904 ?        Ssl  Mar31   0:00 emacs --daemon

$ sudo emacsclient -nw /etc/profile
[sudo] password for t: 
emacsclient: can't find socket; have you started the server?
To start the server in Emacs, type "M-x server-start".
emacsclient: No socket or alternate editor.  Please use:

    --socket-name
    --server-file      (or environment variable EMACS_SERVER_FILE)
    --alternate-editor (or environment variable ALTERNATE_EDITOR)
Tim
  • 4,987
  • 7
  • 31
  • 60
  • I have found that other post, I think it answers your question: https://emacs.stackexchange.com/questions/52/edit-file-with-super-user-rights Executive resume: use `/sudo::filename` or `/su::filename`. – Anderson Torres Apr 01 '18 at 15:02
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11108) – Drew Apr 01 '18 at 16:10

2 Answers2

3

With a Tramp method:

C-x C-f /sudo::/your/system/file/here

That is just add the prefix "/sudo::" before your file and Emacs will ask you your sudo password. :-)

Dieter.Wilhelm
  • 1,836
  • 14
  • 25
2

You should checkout this page of DOOM. There is a function which does this:

(defun doom/sudo-find-file (file)
  "Open FILE as root."
  (interactive
   (list (read-file-name "Open as root: ")))
  (find-file (if (file-writable-p file)
                 file
               (concat "/sudo:root@localhost:" file))))

I tested it on archlinux and it works. Before finding the file you are prompted for your root password. I haven't tested it on mac.

Aquaactress
  • 1,393
  • 8
  • 11