189

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.

6
  • 6
    I just wanted to note that tramp comes installed by default since emacs22, so most people can just do the C-c C-f /sudo::/path-to/file without problem.
    – labyrinth
    May 29, 2012 at 0:37
  • 1
    Thanks for the sudoedit mention. Very useful.
    – tshepang
    Jul 23, 2013 at 18:56
  • 6
    C-c C-f should be C-x C-f?
    – James.Xu
    Aug 26, 2013 at 6:49
  • 1
    Where is C-x C-f /sudo::/path/to/file documented?
    – mcp
    Apr 19, 2021 at 15:53
  • 2
    Tramp is documented in Emacs standard manual. Start info with C-h i then goto Tramp top info noed with g (tramp), or mTRAMP. Go to gQuickstart Start Guide: su, sudo and sg methods which is section 3.3 in Tramp manual.
    – Anders
    Feb 7, 2022 at 16:54

10 Answers 10

70

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.

3
  • 4
    Wait a second... How often does the cache expire? Sep 19, 2008 at 3:52
  • 23
    Tramp does not round-trip via SSH, it uses a subshell.
    – Teddy
    Feb 9, 2010 at 9:19
  • My point was really that you pay the cost for the first opened file, and not for any of the others. Jun 13, 2013 at 15:02
67

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.

19

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

3
  • This works, but it seems to be persistent in a session, every file subsequently is opened and saved as root. There is nothing in the documentation M-x helm-find-files C-c ? that tells how to return to the normal mode of opening as the user. Doing C-c r again does not stop it.
    – Liam
    May 11, 2020 at 13:16
  • @Liam I am still able to open file as a normal user when I use that feature.
    – Qudit
    May 12, 2020 at 17:56
  • It's very odd - some files are fine, some it tries to open as root. I killed the root password with emacs commands and sudo -k and then it prompts for the password. I restarted Emacs and that didn't eliminate the problem. I dug around in .emacs.d and found some references to tramp so I deleted those. Now it seems better but not sure if I'm free of it.
    – Liam
    May 13, 2020 at 3:18
18

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)))
1
5

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.

7
  • You may have your credentials cached. When TRAMP first starts up, it goes through 10-15 seconds of SSH stuff. (I've got 2.1.13-pre too.) Sep 19, 2008 at 3:53
  • Are you sure? I mean, it should be starting a subshell, but not a SSH session to localhost. Takes about 5 seconds to run all the TRAMP auto-sniffage the first time.
    – jfm3
    Sep 19, 2008 at 22:01
  • Well, no, I'm not sure. I should say there's 10-15 seconds of TRAMP (maybe SSH) stuff. I'm not concerned about SSH per se, but about the lag in starting up. How long does this startup stuff persist? Sep 20, 2008 at 15:28
  • 1
    You should only ever need one Emacs process. That's the point of using TRAMP in the first place. Authentication credentials are "cached" in a manner identical to how they are when you run sudo or ssh (that is to say, not at all, or in an ssh-agent).
    – jfm3
    Sep 23, 2008 at 19:08
  • 1
    Longer tramp / ssh startup may be due to your machine lacking a FQDN iirc.
    – sjas
    Jan 25, 2015 at 11:22
5

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

3

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))))
1
  • Just to point out the obvious, this requires that you are using ido-find-file to find files.
    – tripleee
    Jun 26, 2017 at 7:40
1

(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))
      ))
    )
)
0

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

6
  • 12
    Ha ha you said bloat. Emacs used to seem huge. Now, in comparison to run-time footprints of Java, Ruby, and probably a pile of other stuff, it looks quite lean. Regardless, I think Chris' question gets at a perfectly legitimate use of Emacs.
    – jfm3
    Sep 20, 2008 at 16:52
  • 2
    You beat me to it. I use this model consistently. At login I start one Emacs session for general stuff, one for SU access (as root) and one or more for software development (generally per-project but not always). Been doing it for years. Just works.
    – pajato0
    Jan 15, 2010 at 18:31
  • can you clarify how to do this: "open a shell in Emacs and exec sudo emacs"
    – johnbakers
    Nov 29, 2013 at 8:18
  • 2
    @OpenLearner Seriously, you don't really want to do that.
    – tripleee
    May 22, 2015 at 18:08
  • 1
    You COULD upgrade the answer with a new, more modern solution. Where you basically write why the old solution isn't that great (which was awful even back then, when we do look at it through our security googles on). Back then there also was AngeFTP, if you would like a mode that was available back then. And what I can see, Tramp is dated back to 1999, but I can be at fault there.
    – Anders
    Apr 20, 2022 at 20:30
0

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.

2
  • I don't have a super key. Which function does s-e invoke?
    – pglpm
    Jul 27 at 10:18
  • 1
    sudo-edit function is invoked. Jul 29 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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