Editing files on remote servers via multihop-ssh on emacs

Suppose you need to access you department’s computer from outside the institute. The usual solution would be to use ssh. But what if you can access your department’s computer only via a different computer in your institute? Multiple ssh would be the usual way:

aditya@donatello:~$ ssh clac.edu
aditya@clac.edu's password:
aditya@clac.edu:~$ ssh adi@malhar
adi@malhar's password:
adi@malhar:~$

This is the situation at my institute where, we have access to one central departmental server via ssh, from which we can access computers in our labs.

I found that a single command is sufficient to achieve this:

ssh -t clac.edu -t ssh adi@malhar

Ssh prompts for the password twice, first for clac.edu and then for adi@malhar.

This is however not usually sufficient. We want to edit files remotely too. Being an emacs fan, I heard how tramp is a great component of emacs that lets you access files on remote hosts via ssh. I use it pretty often. But the usual way of using tramp to access files through multihop ssh did not work for me. Tramp would just wait for 60 secs and then say that login failed. Perhaps they might work for you.

After a little more investigation, I found that OpenSSH can do wonderful things. Let’s say we want to access a file on the host malhar in the example above. OpenSSH can be configured to access malhar by proxying through clac.edu. The settings are reasonably simple. Just open ~/.ssh/config and enter the lines:

Host malhar
ProxyCommand ssh clac.edu nc malhar 22

This requires that the server at clac.edu have a program called netcat installed. Luckily in my case, the server did.

Once this was setup, tramp is able to access files on malhar. I press C-x f (the emacs key combo to open a file) and enter /ssh:adi@malhar:textfile.txt and then tramp prompts for a password twice. First enter the password for clac.edu and then for adi@malhar, and we’re in. Awesome!