Open file remotely on a vagrant box with sudo access using Emacs 24.3

1.4k views Asked by At

As per this answer, I tried to edit, say, /etc/resolv.conf as a super user on my vagrant box by using the following command:

C-x C-f /[email protected]#2200|sudo:127.0.0.1#2200:/etc/resolv.conf

But it just opened a file on my local machine, reporting /[email protected]#2200|sudo:127.0.0.1#2200:/etc/ as my PWD and telling me to use some M-x command to create the directory, since it didn't exist.

Meaning it didn't connect to my vagrant box. But when I type

C-x C-f /[email protected]#2200:/etc/resolv.conf

It opens the file just fine in a read-only buffer (not using sudo) on my vagrant box.

How does one open a remote file on a vagrant box (note the NAT connection which vagrant uses by default above) with sudo access using Emacs 24.3? (I'm on Fedora 20.)

2

There are 2 answers

3
Chris On BEST ANSWER

The #port syntax is only supported for SSH-based connection types, so it's confusing the sudo hop. You could try something like

/ssh:127.0.0.1#2222|sudo:127.0.0.1:/etc/resolv.conf

but this has the same problem outlined in the answer you linked: the HOST for the dynamic proxy entry will now be 127.0.0.1, which is your local system, which prevents /sudo:: from working locally.

You can avoid this by giving your Vagrant machine a name.

This is almost trivial if you add an entry to ~/.ssh/config, e.g.

Host vagrant
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /home/chris/.vagrant.d/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
ForwardAgent yes
ForwardX11 yes

and then use C-x C-f /ssh:vagrant|sudo:vagrant:/etc/resolv.conf. This has the added benefit of being much shorter to type.

The configuration block came from vagrant ssh-config.

3
cms On

Works for me ok if I use something like

/ssh:vagrant@localhost#2222|sudo:root@localhost#2222:/etc/hosts

as the path. I think the only difference to your recipe is the ssh: method explicitly included as suggested by @Carl Groner in the comment

Updated I previously suggested leaving the hostname for the sudo part empty, but it turns out that this is rather a bad idea, as pointed out by @Chris in the comments. I also agree with his point that #port is really only intended to work with ssh method tramp paths, and this is probably better avoided.

I think the best way to do this is to either use his clever suggestion to set up an ssh alias for the vagrant machine in .ssh/config , or alternatively - add your authorized ssh public key to the root account for vagrant machine - and use a single hop to edit as root e.g.

/ssh:[email protected]:/etc/hosts 

avoiding the multi complexity entirely.

This is how I manage my vagrant edits. I configure my vagrant machines to have a private network address with config.vm.network "private_network" , ip: 192.168.100.10, and for non-ephemeral machines like development vms, I add this IP to my hosts file. ( you could also add an ssh alias for this host address into .ssh/config, as per below). I then put my own ssh pubkey into ~root/.ssh/authorized_keys , either manually or using provisioning.

Obviously this involves more vagrant configuration, but keeps the complexity away from tramp, because I'm on standard port 22 and using basic ssh: method paths.