通过SSH和Sudo与Emacs打开文件 [英] Open file via SSH and Sudo with Emacs
问题描述
我想在位于远程服务器上的Emacs中打开一个文件,服务器上有sudo电源。我可以通过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
但是我想在服务器上使用sudo:
But I want to use sudo on the server:
C-x C-f /sudo::user@server/home/user/file
但是这给了我本机的sudo权限,它在本地机器上要求我的sudo密码。有没有办法在服务器上使用sudo?
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
BTW: Emacs is not installed on the server
推荐答案
从Emacs 24.3起,旧的 multi:
语法的模拟已经分层在现代的 tramp-default-proxies-alist
方法,这意味着您可以在没有任何先前配置的情况下再次执行多跳。有关详细信息,请参阅:
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:
Ch 我 g (tramp)Ad-hoc多跳
RET
使用新的语法,每个跳被分隔由 |
。手册中的示例是:
With the new syntax, each 'hop' is separated by |
. The example in the manual is:
Cx Cf / ssh:bird @ bastion | ssh:you @ remotehost:/ path
RET
首先连接 @ bastion
,从那里到你@ remotehost:/ path
您还可以使用此语法将sudo / su用于远程主机上的root(或当然还有任何其他用户):
You can also use this syntax to sudo/su to root (or of course any other user) on a remote host:
Cx Cf / ssh:you @ remotehost | sudo:remotehost:/ path / to / file
RET
重要:确保明确指定主机名: code> sudo:remotehost:而不是 sudo ::
(见下文)。
Important: be sure to specify the hostname explicitly: sudo:remotehost:
rather than sudo::
(see below).
由于这仍然使用下面的代理机制, tramp-default-proxies-alist
现在应该包含值(remotehostroot / ssh:you @ remotehost:)
As this still uses the proxy mechanism underneath, tramp-default-proxies-alist
should now include the value ("remotehost" "root" "/ssh:you@remotehost:")
意思是代理 / ssh:you @ remotehost:
是当您以 root @ remotehost
请求文件时,将使用它。
Meaning that the proxy /ssh:you@remotehost:
is going to be used whenever you request a file as root@remotehost
.
root
是这些方法的默认用户,但您当然也可以使用以下方式更改为非root用户:
root
is the default user for these methods, but you can of course also change to a non-root user with:
Cx Cf / ssh:you @ remotehost | sudo:them @ remotehost:/ path / to / file
RET
您可能习惯使用 sudo: :
或 su ::
并省略主机名。如果您在本地主机上保持,那么这仍然很好,但是如果您跳转到远程服务器,那么您必须指定每一跳的主机名 - 即使与上一跳相同。始终使用远程主机的 sudo:hostname:
或 su:hostname:
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.
这里的陷阱是 c> sudo :: 实际上似乎工作 - 但是您所做的动态代理条目的HOST将是源自的主机名,而不是连接到的主机。这不仅会让人困惑(因为错误的主机将显示在文件路径中),而且还意味着任何后续尝试在本地主机上使用 sudo ::
将代理代理到远程服务器! (如果您在第二台服务器上执行相同的操作,代理也可能会被破坏)导致进一步的问题)。
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!
这篇关于通过SSH和Sudo与Emacs打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!