188

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.

e18r
  • 7,578
  • 4
  • 45
  • 40
Chris Conway
  • 55,321
  • 43
  • 129
  • 155

10 Answers10

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.

EfForEffort
  • 55,816
  • 4
  • 36
  • 41
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.

Teddy
  • 6,013
  • 3
  • 26
  • 38
19

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

Qudit
  • 472
  • 4
  • 14
  • 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 '20 at 13:16
  • @Liam I am still able to open file as a normal user when I use that feature. – Qudit May 12 '20 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 '20 at 03: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)))
Burton Samograd
  • 3,652
  • 19
  • 21
  • I think the [Emacs Starter Kit](https://github.com/technomancy/emacs-starter-kit) has a something similar in `esk-sudo-edit`. – mrmagooey Dec 29 '11 at 00:48
5

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

Francois G
  • 11,957
  • 54
  • 59
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.

jfm3
  • 36,964
  • 10
  • 32
  • 35
  • 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.) – Chris Conway Sep 19 '08 at 03: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 '08 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? – Chris Conway Sep 20 '08 at 15:28
  • It should only take more than a second the first time you do it. That is, even if you save /sudo::file, delete the buffer, and open /sudo::file2, file2 should open quickly. and It keeps the shell open, and caches the results of all its sniffing. – jfm3 Sep 20 '08 at 16:49
  • But are the credentials cached: (1) for that Emacs process or (2) for all Emacs processes? And do they expire: (a) when Emacs closes, (b) after some fixed time period (hours? days? weeks?), or (c) after a system reboot? – Chris Conway Sep 20 '08 at 20:55
  • 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 '08 at 19:08
  • 1
    Longer tramp / ssh startup may be due to your machine lacking a FQDN iirc. – sjas Jan 25 '15 at 11:22
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))))
anquegi
  • 11,125
  • 4
  • 51
  • 67
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))
      ))
    )
)
alex_1948511
  • 6,073
  • 2
  • 20
  • 21
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.

Daoist Paul
  • 133
  • 8
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. :)

JBB
  • 4,543
  • 3
  • 24
  • 25
  • 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 '08 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 '10 at 18:31
  • can you clarify how to do this: "open a shell in Emacs and exec sudo emacs" – johnbakers Nov 29 '13 at 08:18
  • 2
    @OpenLearner Seriously, you don't really want to do that. – tripleee May 22 '15 at 18:08
  • This is an awful suggestion, when we have TRAMP and can just open (or write) the file like any with `/sudo::/file` without any problem. And no, it doesn't start any SSH session, unless you tells Emacs that the file should open on a remote machine. – Anders Feb 24 '22 at 04:49
  • 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 '22 at 20:30