3

I just started using tramp mode, and it looks amazing. However, there's one issue that's making it non-usable for me. I have several boxes which I log into with one user, say, user. He's in sudoers groups. Now when I go and edit a file, it gets opened in read-only mode. I would like to be able to sudo save, or even better, sudo open.

I tried several approaches first, but none seem to actually work, given that emacs and dired changed how some variables get passed around (namely, buffer-file-name).

I'm on GNU Emacs 24.5.1.

2 Answers 2

5

Have you tried the multi-hop method as suggested in the TRAMP manual?

C-x C-f /ssh:user@localhost|sudo:localhost:/path/to/file RET
2
  • 1
    It can be even shortened for the local machine: \sudo::path Oct 30, 2015 at 5:10
  • 1
    Thanks, that actually worked. Now I'll try to wrap it into a function so it's easy to use.
    – gregoltsov
    Nov 3, 2015 at 11:00
2

You can open a file with sudo whith these functions:

(defun sudo-find-file (file)
    "Opens FILE with root privileges."
    (interactive "FFind file: ")
    (set-buffer
     (find-file (concat "/sudo::" (expand-file-name file)))))
  (global-set-key (kbd "C-c f") 'sudo-find-file)

(defun sudo-remote-find-file (file)
    "Opens repote FILE with root privileges."
    (interactive "FFind file: ")
    (setq begin (replace-regexp-in-string  "scp" "ssh" (car (split-string file ":/"))))
    (setq end (car (cdr (split-string file "@"))))
    (set-buffer
     (find-file (format "%s" (concat begin "|sudo:root@" end)))))
5
  • Thanks for the suggestion, but this results in emacs trying to open the file locally. I.e. when I have a file file opened on remote host host (buffer name is /sshx:user@host:/path/to/file, and I execute your sudo-find-file, it tries to open /sudo:root@localhost:/sshx:user@host:/path/to/file. Maybe I'm doing something wrong, though. Has it worked for you, when used with tramp?
    – gregoltsov
    Oct 29, 2015 at 14:51
  • The right command if you want sudo on remote host is: /scp:user@host|sudo:root@machine:/etc/hosts
    – djangoliv
    Oct 29, 2015 at 15:01
  • 1
    Can you try the new function I edit ?
    – djangoliv
    Oct 29, 2015 at 15:37
  • Will do once I'm back to emacs tinkering. Will update soon!
    – gregoltsov
    Nov 3, 2015 at 11:00
  • could you please clarify how to use your function bound to C-c f key? that would be helpful. Pls use user@remotehost to indicate a remote host and user@localhost if you mean the local machine. thanks.
    – doctorate
    Apr 28, 2017 at 19:08

Your Answer

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

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