Asgardian's Blog

Emacs: SSH and sudo using tramp.

The best way to edit a file, on a remote machine or under another user, while using its configuration, is to use Tramp. Tramp is a package for emacs that is easy to install and configure.

An example of configuration.

Insert this part of code into you .emacs file.

  (use-package tramp
  :ensure t
  :config
  (setq tramp-default-method "ssh")
  (defun edit-current-file-as-root ()
    "Edit the file that is associated with the current buffer as root"
    (interactive)
    (let ((filep (buffer-file-name)))
      (if filep (find-file (concat "/sudo::" filep))
        (message "Current buffer does not have an associated file."))))
  (defun namilus-tramp-cleanup-everything ()
    "Flush all Tramp internal objects connections and kill all remote
buffers."
    (interactive)
    (tramp-cleanup-all-connections)
    (tramp-cleanup-all-buffers))
  )

Editing a file on a remote machine.

C-x C-f /ssh:[user@]host.domain:/edit/some/file/onremote.

Editing a file as root.

C-x C-f /sudo::/edit/some/file/asroot.

Both.

C-x C-f /ssh:[user@]host.domain|sudo::/edit/some/file/asroo/onremote.

It's esasy and usefull.