tags:

views:

585

answers:

3

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

+1  A: 

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)

Dan Andreatta
NFS will do the trick :P
Hassan Syed
I would use sshfs instead
Ben
I tried, but I cannot access files as root with sshfs, when mounting it as regular user. I guess it is a setup problem.
Dan Andreatta
As Hassan noted, no_root_squash should be used with care.
Dan Andreatta
+8  A: 

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-proxy-alist ("my-sudo-alias" nil "/ssh:user@ssh-host"))

then invoke by opening:

/sudo:my-sudo-alias:file-on-ssh-host
Dave Bacher
This seems like the solution, but I get: "multi method is no longer supported"Can you point me to an updated manual?
Fernando
M-x info, C-s tramp :) You may need to define a fake host as the target of your sudo and add it to tramp-default-proxy-alist.
Dave Bacher
Documentation has moved to http://www.gnu.org/software/tramp/#Multi_002dhops
phils
+1  A: 

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

User1
This was not working for me. It looks that in Ubuntu, at least with version 23.2.1 of Emacs and version 2.1.18-23.2 of tramp this does not work. This works, though: http://info.solomonson.com/content/doing-sudo-remote-server-emacs-opensave-file
gaizka
(set-default 'tramp-default-proxies-alist (quote ((".*" "\\`root\\'" "/ssh:%h:"))))
gaizka