【问题标题】:How sudo edit local file with Emacs on Ubuntu?如何在 Ubuntu 上使用 Emacs sudo 编辑本地文件?
【发布时间】:2016-01-21 10:08:57
【问题描述】:

阅读Open file via SSH and Sudo with Emacs 之后,我可以通过 sudo 编辑远程文件 /ssh:username@hostname|sudo:username@hostname:/the-file 但是我不能sudo编辑本地文件,Emacs的tramp提示密码root@hostname,因为在Ubuntu上没有root的密码存在(见RootSudo)。

那么有办法在 Ubuntu 上用 sudo 编辑本地文件吗?

总结: 如果你想用 Emacs 编辑远程/本地文件,@phils 有一个很好的答案 Open file via SSH and Sudo with Emacs; 如果您使用了弹丸(版本Tamp: sending password 或挂起),您可以尝试以下代码来解决我的问题:


  (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))

查看Simple tramp with sudo hangs on 'Sending password'projectile-global-mode prevents new processes from working over TRAMP #523 了解更多信息。

【问题讨论】:

    标签: linux ubuntu emacs


    【解决方案1】:

    我认为你想要做的是:

    ssh username@hostname
    sudo emacs /the-file
    

    我想不出一种方法可以在一行中做到这一点。您首先需要通过 ssh(您的常规密码)连接远程主机,然后使用 sudo 编辑文件。根据客户操作系统,它会提示您输入适当的 sudo 密码。

    【讨论】:

    • 抱歉这个问题令人困惑,如何在 Ubuntu 上的 Emacs 中使用 sudo 编辑本地文件。
    【解决方案2】:

    如果您在 Ubuntu 下运行 sudo 命令,则必须使用您的自己的密码,而不是(不存在的)root 密码。

    【讨论】:

    • 问题是如何在Ubuntu的Emacs中sudo编辑文件,当Emacs的tramp提示sudo: root@hostname时我输入自己的密码后没有效果。这就是问题所在。
    • 我在Ubuntu下一直用/sudo::/path/to/file编辑,没问题。也许您(您作为 Ubuntu 用户)没有足够的权限来运行 sudo?如果你在 shell sudo id 中运行会发生什么?
    • 我有足够的权限来运行 sudo,在终端中运行 sudo 就可以了。 Emacs 迷你缓冲区提示:Password for /sudo:root@hostnamesudo id的输出:uid=0(root) gid=0(root) groups=0(root)
    • 在开业前申请(setq tramp-verbose 10) /sudo::。然后会有一个 Tramp 调试缓冲区,您可能会显示它。
    • 坚持Tramp: Sending Password
    【解决方案3】:

    要使用emacs 以root 身份编辑一些本地(在您自己的计算机上)文件/some/path/local.txt,只需运行

      sudo emacs /some/path/local.txt
    

    一旦emacs 启动,就不能让它写入具有root 所有权的文件(除非它是组或全局可写的,这通常是一件坏事)。

    如果您正确配置了sshd 守护程序,您可以ssh root@localhost(可能通过Emacs Tramp ...)并使用emacs Tramp 等...我不建议这样做。在 Ubuntu 上,您需要创建 root 用户(或使用 uid 0 创建 myroot 用户并使用 myroot@localhost)。

    您也可以(作为普通用户)编辑/tmp/ 中的某些文件,例如/tmp/foobar,然后以root 身份复制:sudo cp -v /tmp/foobar /some/path/for/root/foobar

    Emacs Tramp 有一些 sudosu specific syntax;也许你想要那个......

    【讨论】:

    • 是另一种方式,但我只想正常启动Emacs,并且可以sudo edit local file all in Emacs。
    • 正如我所解释的,这通常是不可能的。
    • Emacs 中sudo 编辑远程文件是可以的,如果将sudo 编辑本地文件作为ssh 的远程文件可以吗?
    • Basile Starynkevitch:大概您对 Emacs 不熟悉,但是如果您按照问题中的链接进行操作,您会注意到,这完全可以通过本问题中提到的 Tramp 工具实现。
    • ...Tramp sudo: 方法对您不起作用? (我真的很好奇)。 su:sudo: 方法不使用 ssh(尽管在 remote 主机案例中,显然需要 ssh)。
    【解决方案4】:

    完全跳过user@system 部分:

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

    【讨论】:

      【解决方案5】:

      我打开要编辑的文件(作为普通文件),然后使用此博客文章中的代码(选项 A)执行 M-x sudo-edit:

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

      在此处粘贴代码,以防万一帖子将来消失。

       (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))))
      

      【讨论】:

        猜你喜欢
        • 2013-12-23
        • 2015-09-23
        • 1970-01-01
        • 2013-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多