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.

gregoltsov
  • 155
  • 5

2 Answers2

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
Emacs User
  • 5,553
  • 18
  • 48
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)))))
djangoliv
  • 3,169
  • 16
  • 31
  • 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 '15 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 '15 at 15:01
  • 1
    Can you try the new function I edit ? – djangoliv Oct 29 '15 at 15:37
  • Will do once I'm back to emacs tinkering. Will update soon! – gregoltsov Nov 03 '15 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 '17 at 19:08