How to use SSH and SUDO together with TRAMP in Emacs [duplicate]
Asked Answered
L

2

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

Longdistance answered 12/8, 2010 at 7:29 Comment(3)
Duplicate of: <a href="#2178187 file via SSH and Sudo with Emacs</a> See documentation here: gnu.org/software/tramp/#Multi_002dhopsBivalent
But this answer actually works.Exorbitance
Try this: C-x C-f /ssh:you@remotehost|sudo:remotehost:/path/to/file RETMiletus
A
-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.

Anglomania answered 12/8, 2010 at 8:1 Comment(3)
Would that not require him to authenticate as root?Uvarovite
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).Anglomania
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.Intensive
C
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.

Chance answered 18/1, 2011 at 15:13 Comment(6)
Does this login to the remote server as root?Intensive
This will login you as a user and run sudo after it. No need to allow ssh for root ;)Chance
I've tried your suggestion and another one (in the duplicate thread mentioned here: #2178187) but neither of them are working.Intensive
It seems you have wrong version of tramp. Use this solution: serverfault.com/questions/123802/…Chance
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.Intensive
If you are doing other manipulation of your tramp-default-proxies-alist you might want to use add-to-list instead of set-default.Outvote
A
-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.

Anglomania answered 12/8, 2010 at 8:1 Comment(3)
Would that not require him to authenticate as root?Uvarovite
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).Anglomania
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.Intensive

© 2022 - 2024 — McMap. All rights reserved.