I was reading some of the old posts from the excellent emacs-fu when I came across this post describing how to access root-owned files as a regular user. It was a nice post and cleared up an arcane piece of syntax that I never understood. In order to edit a root-owned file on the local host you enter something like:【Ctrl+x Ctrl+f】/sudo::<path-to-root-owned-file>
. The double colon seemed different from other similar constructs but all that’s happening is that /sudo::
is really just a shortcut for sudo
‘s default, which is /sudo:root@localhost:
.
That’s interesting and cleared up something that never made sense to me but the interesting lesson from the post occurred in the comments. One of the commenters complained that this still didn’t help with the common case of wanting to edit root files on a remote machine. If you try to do this, Emacs (Tramp, really) complains that sudo
can only be used on the local host. However, it turns out that you can use the Tramp multihop facility to enable this functionality. This is explained in detail in the info page at Tramp > Configuration > Multi-hops
, but the TL;DR is to add the three commands
(require 'tramp) (add-to-list 'tramp-default-proxies-alist '(nil "\\`root\\'" "/ssh:%h:")) (add-to-list 'tramp-default-proxies-alist '((regexp-quote (system-name)) nil nil))
to your .emacs
or init.el
file. Then you can edit remote root files with 【Ctrl+x ctrl+f】 /sudo:root@remote-host:<path-to-root-owned-file>
.