0
votes

After read Open file via SSH and Sudo with Emacs then I can sudo edit remote file via /ssh:username@hostname|sudo:username@hostname:/the-file but I can't sudo edit local file, Emacs's tramp prompts the password of root@hostname, because on Ubuntu there no root's password exists (see RootSudo) .

So there is a way to sudo edit local file on Ubuntu?

Summary: If you want to edit remote/local file with Emacs, @phils has a good answer at Open file via SSH and Sudo with Emacs; If you used projectile (version<=0.12.0) and can not sudo edit local file(such as Tamp: sending password or hang) you can try the following code that fix my issue:


  (when (package-installed-p 'projectile)
  (defun projectile-project-root ()
    "Retrieves the root directory of a project if available.
    The current directory is assumed to be the project's root otherwise."
    (let ((dir default-directory))
      (or (--reduce-from
           (or acc
               (let* ((cache-key (format "%s-%s" it dir))
                      (cache-value (gethash cache-key projectile-project-root-cache)))
                 (if cache-value
                     (if (eq cache-value 'no-project-root)
                         nil
                       cache-value)
                   (let ((value (funcall it (file-truename dir))))
                     (puthash cache-key (or value 'no-project-root) projectile-project-root-cache)
                     value))))
           nil
           projectile-project-root-files-functions)
          (if projectile-require-project-root
              (error "You're not in a project")
            default-directory))))
  (projectile-global-mode))

To see Simple tramp with sudo hangs on 'Sending password' and projectile-global-mode prevents new processes from working over TRAMP #523 for more information.

5

5 Answers

1
votes

If you run sudo commands under Ubuntu, you must use your own password instead of the (non-existing) root password.

5
votes

Simply skip the user@system part altogether:

C-x C-f /sudo::/path/to/file RET
1
votes

I open the file that I want to edit (as a normal file) and then do the M-x sudo-edit using the code (option A) from this blog post:

http://emacsredux.com/blog/2013/04/21/edit-files-as-root/

Pasting the code here, just in case the post goes away in the future.

 (defun sudo-edit (&optional arg)
  "Edit currently visited file as root.

With a prefix ARG prompt for a file to visit.
Will also prompt for a file to visit if current
buffer is not visiting a file."
  (interactive "P")
  (if (or arg (not buffer-file-name))
      (find-file (concat "/sudo:root@localhost:"
                         (ido-read-file-name "Find file(as root): ")))
    (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
0
votes

I think what you are trying to do is:

ssh username@hostname
sudo emacs /the-file

I cannot think of a way to do that in one line. You first need to connect on the remote host via ssh, your regular password and then sudo edit the file. Depending on the guest OS it will prompt you for the appropriate password for sudo.

-3
votes

To edit with emacs as root some local (on your own computer) file /some/path/local.txt just run

  sudo emacs /some/path/local.txt

Once emacs has started, you cannot make it write a file with root ownership (unless it is group or world writable, which is generally a bad thing).

If you have an sshd daemon correctly configured, you could ssh root@localhost (perhaps via Emacs Tramp ...) and use emacs Tramp etc... I don't recommend that. On Ubuntu, you'll need to create the root user (or create a myroot user with uid 0 and use myroot@localhost).

You could also edit (as an ordinary user) some file in /tmp/ like /tmp/foobar and copy it as root later: sudo cp -v /tmp/foobar /some/path/for/root/foobar

Emacs Tramp has some sudo and su specific syntax; perhaps you want that...