簡體   English   中英

如何在Ubuntu上使用Emacs sudo編輯本地文件?

[英]How sudo edit local file with Emacs on Ubuntu?

通過SSH和Emacs使用Sudo讀取打開文件后,我可以通過/ssh:username@hostname|sudo:username@hostname:/the-file sudo編輯遠程文件,但是我不能sudo編輯本地文件,Emacs的流氓提示輸入密碼root@hostname ,因為在Ubuntu上不存在root的密碼(請參閱RootSudo )。

因此,有一種方法可以在Ubuntu上sudo編輯本地文件?

簡介:如果要使用Emacs編輯遠程/本地文件,@ phils對於通過SSH的Open file和使用Emacs的Sudo來說是一個很好的答案; 如果您使用彈丸(版本<= 0.12.0)並且無法對本地文件進行sudo編輯(例如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))

要查看帶有sudo的簡單流氓掛在“發送密碼”上,並且射彈全局模式可防止新進程在TRAMP#523上運行,以獲取更多信息。

只需完全跳過user@system部分:

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

如果在Ubuntu下運行sudo命令,則必須使用您自己的密碼而不是(不存在的)root密碼。

我認為您正在嘗試做的是:

ssh username@hostname
sudo emacs /the-file

我無法想到一種方法來做到這一點。 您首先需要通過ssh,常規密碼連接到遠程主機,然后對文件進行sudo編輯。 根據來賓操作系統的不同,它將提示您輸入適當的sudo密碼。

我打開要編輯的文件(作為普通文件),然后使用此博客文章中的代碼(選項A)進行Mx 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))))

要使用emacs作為根目錄編輯一些本地(在您自己的計算機上)文件/some/path/local.txt只需運行

  sudo emacs /some/path/local.txt

emacs啟動后,您將無法使它寫入具有根所有權的文件(除非它是組或世界可寫的,這通常是一件壞事)。

如果您正確配置了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 特定的語法 ; 也許你想要那個...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM