使用 Emacs 通过 SSH 和 Sudo 打开文件 [英] Open file via SSH and Sudo with Emacs

查看:20
本文介绍了使用 Emacs 通过 SSH 和 Sudo 打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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?

顺便说一句:服务器上没有安装 Emacs

解决方案

从 Emacs 24.3 开始,旧的 multi: 语法的模拟已经分层在现代 tramp- 之上default-proxies-alist 方法,这意味着您可以在没有任何事先配置的情况下再次执行多跳.详情请见:

Chig (tramp)Ad-hoc multi-hops RET

使用新语法,每个跳跃"由 | 分隔.手册中的例子是:

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

首先作为 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@远程主机:")

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

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

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

始终明确指定远程主机名

您可能习惯于使用 sudo::su:: 并省略主机名.如果您保持在本地主机上,那么这仍然没问题,但是如果您跳到远程服务器,那么您必须为每个跃点指定主机名——即使它是与前一跳相同.始终对远程主机使用 sudo:hostname:su:hostname:.

这里的陷阱是sudo:: 确实实际上似乎有效——然而当你这样做时,动态代理的HOSTentry 将是您起源的主机名,而不是您连接到的主机.这不仅看起来令人困惑(因为错误的主机将显示在文件路径中),而且还意味着任何后续在本地主机上使用 sudo:: 的尝试都将被代理到远程服务器!(如果您在第二台服务器上做同样的事情,代理也可能会被破坏,从而导致更多问题).

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

Emacs 27+

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

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

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

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

解决方案

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

这篇关于使用 Emacs 通过 SSH 和 Sudo 打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆