使用 Emacs 通过 SSH 和 Sudo 打开文件

发布于 2024-08-20 05:18:13 字数 331 浏览 2 评论 0原文

我想在 Emacs 中打开一个位于远程服务器上的文件,并在服务器上使用 sudo 权限。我可以通过 Tramp 使用 sudo 打开本地文件,如下所示:

C-x C-f /sudo::/home/user/file

但我想在服务器上使用 sudo:

C-x C-f /sudo::user@server/home/user/file

但这给了我在本地计算机上的 sudo 权限,它要求我在本地计算机上提供 sudo 密码。有没有办法在服务器上使用 sudo?

BTW:服务器上未安装 Emacs

I want to open a file inside Emacs which is located on a remote server, with sudo powers on the server. I can open local files with sudo via Tramp like this:

C-x C-f /sudo::/home/user/file

But I want to use sudo on the server:

C-x C-f /sudo::user@server/home/user/file

But this gives me sudo powers on my local machine, it asks for my sudo password on the local machine. Is there a way to use sudo on the server?

BTW: Emacs is not installed on the server

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月亮邮递员 2024-08-27 05:18:13

从 Emacs 24.3 开始,旧的 multi: 语法的模拟已经分层在现代 tramp-default-proxies-alist 方法之上,这意味着您可以再次无需任何事先配置即可执行多跳。有关详细信息,请参阅:

Chig (tramp)Ad-hoc 多跳 RET< /kbd>

使用新语法,每个“跃点”由 | 分隔。手册中的示例为:

CxCf /ssh:bird@bastion|ssh:you@remotehost:/path RET< /kbd>

首先作为 bird@bastion 连接,然后从那里连接到

远程主机上的 you@remotehost:/path /su: 或 /sudo:

你也可以使用这个sudo/su 到远程主机上的 root(当然还有任何其他用户)的语法:

CxCf /ssh:you@remotehost|sudo:remotehost: /path/to/file RET

重要:请务必明确指定主机名:sudo:remotehost: 而不是 sudo::(见下文)。

由于这仍然使用底层的代理机制,tramp-default-proxies-alist 现在应该包含值 ("remotehost" "root" "/ssh:you@remotehost:")

意味着每当您以 root@remotehost 请求文件时,都会使用代理 /ssh:you@remotehost:

root 是这些方法的默认用户,但您当然也可以使用以下命令更改为非 root 用户:

CxCf /ssh:you@remotehost|sudo:them@remotehost:/path/to/file RET

始终显式指定远程主机名

您可能习惯使用 sudo::< /code> 或 su:: 并省略主机名。如果您停留在本地主机上,那么这仍然没问题,但如果您要跳到远程服务器,那么您必须为每个跃点指定主机名 - 即使它与前一跳相同。始终对远程主机使用 sudo:hostname:su:hostname:

这里的陷阱是,sudo::确实实际上似乎可以工作 - 但是当您这样做时,动态代理条目的主机将是您源自的主机名,而不是您连接到的主机名。这不仅看起来令人困惑(因为错误的主机将显示在文件路径中),而且还意味着任何后续在本地主机上使用 sudo:: 的尝试都将被代理到远程服务器! (如果您在第二台服务器上执行相同的操作,代理也可能会被破坏,从而导致进一步的问题)。

简而言之,多跳时不要使用 ::

Emacs 27+

从 Emacs 27.1(或 TRAMP 2.4.2,如果使用 GNU ELPA 软件包)开始, :: 大小写直观地工作,例如 /ssh:you@remotehost|sudo:: 将重用 remotehost 而不是您自己的本地主机,因此您不会最终得到错误的代理条目。

此外,还会检测到 /ssh:you@remotehost|sudo:localhost: 之类的内容并将其标记为用户错误。

如果您可能会混合使用 Emacs 版本,包括早于 27 的版本(或者您建议其他可能使用旧版本的人),那么继续处理 :: 是最安全的多跳时不安全,以避免潜在的事故。 (即,如果 TRAMP 版本未知,则明确指定正确的远程主机将仍然是最安全的方法。)

As of Emacs 24.3, an analog of the old multi: syntax has been layered on top of the modern tramp-default-proxies-alist approach, meaning that you can once again perform multi-hops without any prior configuration. For details, see:

C-hig (tramp)Ad-hoc multi-hops RET

With the new syntax, each 'hop' is separated by |. The example in the manual is:

C-xC-f /ssh:bird@bastion|ssh:you@remotehost:/path RET

Which connects firstly as bird@bastion, and from there to you@remotehost:/path

/su: or /sudo: on remote hosts

You can also use this syntax to sudo/su to root (or of course any other user) on a remote host:

C-xC-f /ssh:you@remotehost|sudo:remotehost:/path/to/file RET

Important: be sure to specify the hostname explicitly: sudo:remotehost: rather than sudo:: (see below).

As this still uses the proxy mechanism underneath, tramp-default-proxies-alist should now include the value ("remotehost" "root" "/ssh:you@remotehost:")

Meaning that the proxy /ssh:you@remotehost: is going to be used whenever you request a file as root@remotehost.

root is the default user for these methods, but you can of course also change to a non-root user with:

C-xC-f /ssh:you@remotehost|sudo:them@remotehost:/path/to/file RET

Always specify the remote hostname explicitly

You are probably used to using sudo:: or su:: and omitting the hostname. If you are staying on the localhost then this is still fine, but if you are hopping to a remote server then you must specify the hostname for every hop -- even if it is the same as for the previous hop. Always use sudo:hostname: or su:hostname: with remote hosts.

The trap here is that sudo:: does actually appear to work -- however when you do that the HOST for the dynamic proxy entry will be the hostname you originated from rather than the host you connected to. This will not only look confusing (as the wrong host will be displayed in the file paths), but it will also mean that any subsequent attempt to use sudo:: on your localhost will instead be proxied to the remote server! (and the proxy would also presumably be clobbered if you did the same thing on a second server, causing further issues).

In short, don't use :: when you multi-hop!

Emacs 27+

Starting from Emacs 27.1 (or Tramp 2.4.2, if using the GNU ELPA package) the :: case works intuitively, such that /ssh:you@remotehost|sudo:: will re-use remotehost rather than your own local host, and so you won't end up with a bad proxy entry.

In addition, the likes of /ssh:you@remotehost|sudo:localhost: are detected and flagged as user errors.

If you are liable to use a mixture of Emacs versions including versions earlier than 27 (or you are advising someone else who may be using an older version), then it would be safest to continue to treat :: as unsafe when multi-hopping, to avoid potential mishap. (I.e. specifying the correct remote host explicitly will remain the safest approach if the Tramp version is unknown.)

雨落星ぅ辰 2024-08-27 05:18:13

更新:虽然这个答案解决了最初的问题,但它是为 emacs 20 或 21 编写的。对于 emacs 24,我建议您使用 phils 的回答,因为它提供了更多解释并且是最新的。


我认为 多跳文件名 是您正在寻找的。

第一个跃点是 ssh,第二个跃点是 sudo。


更新:最新版本的 emacs 支持使用代理进行多跳:

(add-to-list 'tramp-default-proxies-alist ("my-sudo-alias" nil "/ssh:user@ssh-host"))

然后通过打开调用:

/sudo:my-sudo-alias:file-on-ssh-host

Update: Although this answer solved the original problem, it was written for emacs 20 or 21. For emacs 24, I recommend you use phils's answer because it offers more explanation and is up to date.


I think multi-hop filenames in tramp is what you're looking for.

The first hop would be ssh and the second would be sudo.


Update: Recent versions of emacs support multiple hops using proxies:

(add-to-list 'tramp-default-proxies-alist ("my-sudo-alias" nil "/ssh:user@ssh-host"))

Then invoke by opening:

/sudo:my-sudo-alias:file-on-ssh-host
鹊巢 2024-08-27 05:18:13

我对所选答案有一些疑问。但是,当我将这一行添加到 .emacs 中时,它起作用了:

(add-to-list 'tramp-default-proxies-alist '(".*" "\\`root\\'" "/ssh:%h:"))

然后执行以下命令:

/sudo:ssh-host:file-on-ssh-host

这有点令人困惑,因为在某一时刻我提示输入“root”密码,但输入我的用户密码授予我访问权限。它也普遍适用于网络上的所有主机。另外,我仍然可以这样做而不是root:

/ssh:ssh-host:file-on-ssh-host

I had some troubles with the selected answer. However, it worked when I added this line to .emacs:

(add-to-list 'tramp-default-proxies-alist '(".*" "\\`root\\'" "/ssh:%h:"))

And then executed the following:

/sudo:ssh-host:file-on-ssh-host

It was slightly confusing because at one point I was prompted for the "root" password, but entering my user's password granted me access. It also universally works on all hosts on the network. Also, I can still do this to not be root:

/ssh:ssh-host:file-on-ssh-host

过气美图社 2024-08-27 05:18:13

tramp多跳配置网页

 (add-to-list 'tramp-default-proxies-alist
                   '(nil "\\`root\\'" "/ssh:%h:"))
      (add-to-list 'tramp-default-proxies-alist
                   '((regexp-quote (system-name)) nil nil))

然后任何

C-x C-f /sudo:remote-host:/file

将使用与远程计算机上运行 emacs 的用户相同的用户名登录后,使用 sudo 打开文件。

From the tramp multi-hops configuration webpage

 (add-to-list 'tramp-default-proxies-alist
                   '(nil "\\`root\\'" "/ssh:%h:"))
      (add-to-list 'tramp-default-proxies-alist
                   '((regexp-quote (system-name)) nil nil))

Then any

C-x C-f /sudo:remote-host:/file

will open file using sudo after logged with the same username of the user running emacs but on the remote machine.

殊姿 2024-08-27 05:18:13

您必须首先通过 ssh 连接到服务器,然后必须在本地运行 emacs。

或者你可以使用带有 no_root_squash 的 NFS,或者你可以尝试使用 emacs 服务器/客户端,尽管我不知道会发生什么(我自己不使用 emacs)

You have to ssh into the server first, then you have to run emacs locally.

Or you can use NFS with no_root_squash, or you can try with emacs server/client, although I have no idea of what may happen (do not use emacs myself)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文