I'm using the C-x C-f
option to use SSH to edit a file.
The file I'm editing requires my SSH user to use sudo
.
When I start editing the file it says read-only. How can I remotely edit files requiring sudo
?
I'm using the C-x C-f
option to use SSH to edit a file.
The file I'm editing requires my SSH user to use sudo
.
When I start editing the file it says read-only. How can I remotely edit files requiring sudo
?
Using TRAMP multi-hops. For instance, if you want to edit the remote file /root/salary.txt
/ssh:homer@powerplant|sudo:powerplant:/root/salary.txt
The example is taken from the Mastering Emacs book.
powerplant
if you stay on the machine you're sshing to. Source: emacswiki.org/emacs/TrampMode#toc18 . Edit: I was wrong. I didn't read the page thoroughly.
Commented
Nov 24, 2017 at 18:06
I wrote a command for automating @Manuel Uberti's answer. The second one is the one I'd use. It uses the first one for remote files and uses crux for local files.
(require 'crux)
(require 's)
(defun my--reopen-remote-file-as-root ()
"Reopen a remote file as root over tramp."
(find-alternate-file (let* ((parts (s-split ":" buffer-file-name))
(hostname (nth 1 parts))
(filepath (car (last parts))))
(concat "/ssh" ":" hostname "|" "sudo" ":" hostname ":" filepath))))
(defun my/reopen-file-as-root ()
"Reopen a local or remote file as root."
(interactive)
(if (file-remote-p default-directory)
(progn
(my--reopen-remote-file-as-root)))
(crux-reopen-as-root))