1

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.

Community
  • 1
  • 1
南山竹
  • 484
  • 8
  • 15

5 Answers5

8

Simply skip the user@system part altogether:

C-x C-f /sudo::/path/to/file RET
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
1

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

Michael Albinus
  • 1,571
  • 9
  • 19
  • The question is how to sudo edit file in Emacs on Ubuntu, and there're no effect after I input my own password when Emacs' tramp prompts ```sudo: root@hostname```. That's the problem. – 南山竹 Oct 22 '15 at 08:10
  • 1
    I always use `/sudo::/path/to/file` to edit under Ubuntu, no problem. Maybe you (you as Ubuntu user) don't have sufficient rights to run sudo? What happens if you run in a shell `sudo id` ? – Michael Albinus Oct 22 '15 at 08:12
  • I'd sufficient rights to run sudo, sudo run in Terminal is OK. The Emacs mini buffer prompts: ```Password for /sudo:root@hostname```. The output of ```sudo id```: ```uid=0(root) gid=0(root) groups=0(root)``` – 南山竹 Oct 22 '15 at 08:17
  • Apply `(setq tramp-verbose 10)` prior opening `/sudo::`. Then there will be a Tramp debug buffer, which you might show. – Michael Albinus Oct 22 '15 at 08:35
  • Hang on ```Tramp: Sending Password``` – 南山竹 Oct 22 '15 at 08:43
  • *tramp...* buffer says: Password: Sorry, try again. Password: Sorry, try again. Password: Sorry, try again. sudo: 3 incorrect password attempts – 南山竹 Oct 22 '15 at 08:51
  • Like this issure on github [Simple tramp with sudo hangs on 'Sending passwo](https://github.com/bbatsov/prelude/issues/594) – 南山竹 Oct 22 '15 at 09:16
  • Do you use projectile? I've just seen another discussion where projectile has confused Tramp. Have you tried starting `emacs -Q` and then open `/sudo::` ? – Michael Albinus Oct 22 '15 at 09:29
  • I used projectile, the issue is abused file-truename in projectile-project-root function in projectile.el at version 0.12.0 . There has a fix at [Fix #657](https://github.com/bbatsov/projectile/commit/4e3cc0ae40256478198a46d27d9efe41b47f7ca2) at version 0.13.0 . My projectile is 0.12.0, so extract projectile-project-root function definition into my emacs's loading files can fix it. – 南山竹 Oct 22 '15 at 15:54
  • I'll append the fix to the question let someone know how to fix it when they meet the same issue. I found the question had been flag as 'not useful' if you do not think so, please help me mark it up, thank you and thank you let me get the right way to find the answer. – 南山竹 Oct 22 '15 at 15:54
1

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))))
fgui
  • 1,564
  • 1
  • 13
  • 14
0

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.

koullislp
  • 510
  • 4
  • 11
-2

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...

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    It's the another way, but I just want to start Emacs normally and can sudo edit local file all in Emacs. – 南山竹 Oct 22 '15 at 08:46
  • As I explained, this is not generally possible. – Basile Starynkevitch Oct 22 '15 at 08:48
  • Sudo edit remote file in Emacs is OK, It's possible if treat sudo edit local file as the remote file via ssh? – 南山竹 Oct 22 '15 at 09:03
  • 1
    Basile Starynkevitch: Presumably you are unfamiliar with Emacs, but had you followed the link in the question you would have noticed that this is entirely possible via the Tramp facility mentioned in this question. – phils Oct 22 '15 at 09:37
  • And Tramp is based above `ssh` IIRC – Basile Starynkevitch Oct 22 '15 at 09:46
  • 2
    ...and the Tramp `sudo:` method doesn't work for you? (I'm genuinely curious). The `su:` and `sudo:` methods do not use ssh (although in the *remote* host case for this question, obviously ssh is required). – phils Oct 22 '15 at 09:47
  • I don't like Tramp and never use it for root access. I much prefer `sudo emacs` and I often use a specific color for root emacs: `sudo emacs -bg lightyellow` (and quite often I use `vi` for sysadmin editions, and `emacs` for daily developer work) – Basile Starynkevitch Oct 22 '15 at 09:49
  • @phils: you are right. The cause is projectile (version 0.12.0) conflicts with tramp and I appended a fix to the question to let someone know. If you don't think the question does not show research efforts or something else, please mark it up. Finally, thank you for your answer and for your comments. – 南山竹 Oct 22 '15 at 16:15