Open a file with su/sudo inside Emacs

EmacsFile PermissionsSudo

Emacs Problem Overview


Suppose I want to open a file in an existing Emacs session using su or sudo, without dropping down to a shell and doing sudoedit or sudo emacs. One way to do this is

C-x C-f /sudo::/path/to/file

but this requires an expensive round-trip through SSH. Is there a more direct way?

[EDIT] @JBB is right. I want to be able to invoke su/sudo to save as well as open. It would be OK (but not ideal) to re-authorize when saving. What I'm looking for is variations of find-file and save-buffer that can be "piped" through su/sudo.

Emacs Solutions


Solution 1 - Emacs

The nice thing about Tramp is that you only pay for that round-trip to SSH when you open the first file. Sudo then caches your credentials, and Emacs saves a handle, so that subsequent sudo-opened files take much less time.

I haven't found the extra time it takes to save burdening, either. It's fast enough, IMO.

Solution 2 - Emacs

Tramp does not round-trip sudo via SSH, it uses a subshell. See the manual: https://www.gnu.org/software/tramp/#Inline-methods

Therefore, I recommend that you stick with TRAMP.

Solution 3 - Emacs

If you use helm, helm-find-files supports opening a file as root with C-c r.

Solution 4 - Emacs

Not really an answer to the original question, but here's a helper function to make doing the tramp/sudo route a bit easier:

(defun sudo-find-file (file-name)
"Like find file, but opens the file as root."
(interactive "FSudo Find File: ")
(let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
(find-file tramp-file-name)))

Solution 5 - Emacs

Your example doesn't start ssh at all, at least not with my version of TRAMP ("2.1.13-pre"). Both find-file and save-buffer work great.

Solution 6 - Emacs

At least for saving, a sudo-save package was written exactly for that kind of problem.

Solution 7 - Emacs

I recommend you to use advising commands. Put this function in your ~/.emacs

(defadvice ido-find-file (after find-file-sudo activate)
  "Find file as root if necessary."
  (unless (and buffer-file-name
               (file-writable-p buffer-file-name))
    (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))

Solution 8 - Emacs

(works only locally. Need to be updated to work correctly via tramp)

A little bit extended Burton's answer:

(defun sudo-find-file (file-name)
"Like find file, but opens the file as root."
(interactive "FSudo Find File: ")
(let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
(find-file tramp-file-name)))


(add-hook 'dired-mode-hook
    (lambda ()
      ;; open current file as sudo 
      (local-set-key (kbd "C-x <M-S-return>") (lambda()
        (interactive)
        (message "!!! SUDO opening %s" (dired-file-name-at-point))
        (sudo-find-file (dired-file-name-at-point))
      ))
    )
)

Solution 9 - Emacs

Ugh. Perhaps you could open a shell in Emacs and exec sudo emacs.

The problem is that you presumably don't just want to open the file. You want to be able to save it later. Thus you need your root privs to persist, not just exist for opening the file.

Sounds like you want Emacs to become your window manager. It's bloated enough without that. :)

Solution 10 - Emacs

I find sudo edit function very useful for that. After opening a file, press s-e to have sudo access to edit/save the file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris ConwayView Question on Stackoverflow
Solution 1 - EmacsEfForEffortView Answer on Stackoverflow
Solution 2 - EmacsTeddyView Answer on Stackoverflow
Solution 3 - EmacsQuditView Answer on Stackoverflow
Solution 4 - EmacsBurton SamogradView Answer on Stackoverflow
Solution 5 - Emacsjfm3View Answer on Stackoverflow
Solution 6 - EmacsFrancois GView Answer on Stackoverflow
Solution 7 - EmacsanquegiView Answer on Stackoverflow
Solution 8 - Emacsalex_1948511View Answer on Stackoverflow
Solution 9 - EmacsJBBView Answer on Stackoverflow
Solution 10 - EmacsDaoist PaulView Answer on Stackoverflow