2

This question already has an answer here:

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)

marked as duplicate by npostavs, Dan Apr 1 '18 at 15:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I have found that other post, I think it answers your question: emacs.stackexchange.com/questions/52/… Executive resume: use /sudo::filename or /su::filename. – Anderson Torres Apr 1 '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 – Drew Apr 1 '18 at 16:10
2

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. :-)

  • Thanks. Does sudo only apply to the system file specified in the same line, and not to the files to be opened in the future? – Tim Apr 1 '18 at 15:14
  • @Tim: Welcome, It works only for files with the "/sudo::" prefix. – Dieter.Wilhelm Apr 1 '18 at 15:23
1

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.

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