Open a file with su/sudo inside Emacs
Asked Answered
S

10

190

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.

Sallet answered 18/9, 2008 at 18:51 Comment(5)
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.Fidgety
C-c C-f should be C-x C-f?Sielen
Where is C-x C-f /sudo::/path/to/file documented?Detwiler
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.Cohesive
C-x C-f /su::/path/to/fileWalkerwalkietalkie
A
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.

Aubin answered 19/9, 2008 at 2:17 Comment(3)
Wait a second... How often does the cache expire?Sallet
Tramp does not round-trip via SSH, it uses a subshell.Edam
My point was really that you pay the cost for the first opened file, and not for any of the others.Aubin
E
70

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.

Edam answered 15/1, 2010 at 12:25 Comment(0)
F
19

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

Foredate answered 27/6, 2015 at 19:25 Comment(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.Brenna
@Brenna I am still able to open file as a normal user when I use that feature.Foredate
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.Brenna
M
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)))
Maggoty answered 12/8, 2011 at 17:11 Comment(1)
I think the Emacs Starter Kit has a something similar in esk-sudo-edit.Bremser
P
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.

Padnag answered 19/9, 2008 at 3:14 Comment(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.)Sallet
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.Padnag
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?Sallet
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.Padnag
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?Sallet
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).Padnag
Longer tramp / ssh startup may be due to your machine lacking a FQDN iirc.Dislimn
G
5

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

Garthgartner answered 17/6, 2010 at 12:19 Comment(0)
O
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))))
Ozan answered 25/3, 2015 at 12:24 Comment(1)
Just to point out the obvious, this requires that you are using ido-find-file to find files.Desmid
L
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))
      ))
    )
)
Lalittah answered 12/4, 2016 at 9:39 Comment(0)
D
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. :)

Dorsett answered 18/9, 2008 at 19:3 Comment(6)
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.Padnag
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.Saransarangi
can you clarify how to do this: "open a shell in Emacs and exec sudo emacs"Pursuance
@OpenLearner Seriously, you don't really want to do that.Desmid
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.Cohesive
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.Cohesive
S
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.

Shuttle answered 21/8, 2021 at 6:37 Comment(2)
I don't have a super key. Which function does s-e invoke?Ardeb
sudo-edit function is invoked.Shuttle

© 2022 - 2024 — McMap. All rights reserved.