25

Possible Duplicate:
Open file via SSH and Sudo with Emacs

I want to open a file using sudo (say, /etc/hosts) on a remote machine connected by ssh using TRAMP on Emacs.

I tried couple of options, but none of them worked. Can anybody tell me how to go about it?

Thank you

Community
  • 1
  • 1
Jeevan Pingali
  • 1,072
  • 1
  • 9
  • 11

2 Answers2

40

Use following in your .emacs

(set-default 'tramp-default-proxies-alist (quote ((".*" "\\`root\\'" "/ssh:%h:"))))

Then you can simply type:

C-x C-f /sudo:root@host[#port]:/path/to/file

It will ask you for your password to access remote shell and then your password again for sudo access.

Maxim Filatov
  • 952
  • 8
  • 6
  • Does this login to the remote server as root? – SabreWolfy Jul 25 '11 at 13:39
  • 2
    This will login you as a user and run sudo after it. No need to allow ssh for root ;) – Maxim Filatov Jul 26 '11 at 08:01
  • I've tried your suggestion and another one (in the duplicate thread mentioned here: http://stackoverflow.com/questions/2177687/open-file-via-ssh-and-sudo-with-emacs) but neither of them are working. – SabreWolfy Jul 26 '11 at 08:10
  • 1
    It seems you have wrong version of tramp. Use this solution: http://serverfault.com/questions/123802/emacs-tramp-sudo-to-root-on-localhost-hangs-after-asking-for-password/223371#223371 – Maxim Filatov Jul 26 '11 at 09:00
  • Thanks. I installed tramp 2.1.19 and inserted my remote username before the "%h" and I am now able to login to the machine (not as root) and save files as root. – SabreWolfy Jul 26 '11 at 11:54
  • 1
    If you are doing other manipulation of your `tramp-default-proxies-alist` you might want to use `add-to-list` instead of `set-default`. – tripleee Jul 25 '14 at 07:03
-5

In recent Emacsen and Unixes it's dead simple, just visit a file (C-x C-f) and start the file name with ssh://server:/file.

If you are using Windows, first install Putty and put it in your PATH environment variable. Then when you visit a file use plink://server:/file instead of ssh://server:/file.

EDIT:

This answer is wrong, see the answer below.

wm_eddie
  • 3,938
  • 22
  • 22
  • Would that not require him to authenticate as root? – Joel J. Adamson Aug 12 '10 at 17:10
  • ssh will by default use your current username. You can another one by using `ssh://username:password@server:/file` Entering the password every time gets tedious really fast though, so I suggest setting up public-key authentication (then it's just `ssh://username@server:/file`). – wm_eddie Aug 13 '10 at 02:59
  • 3
    That gives access to the file as 'username', but the question is now how to save that file if it is owned by root on the remote machine. In Ubuntu installations, for example, it is not possible (by default) to login as root, so you have to access the file as 'username' and then save it as root. – SabreWolfy Jul 25 '11 at 11:55