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

emacs, sudo, tramp, vagrant

Solution

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`.

Problem

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 /vagrant@127.0.0.1#2200|sudo:127.0.0.1#2200:/etc/resolv.conf ``` But it just opened a file on my local machine, reporting `/vagrant@127.0.0.1#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 /vagrant@127.0.0.1#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.)

Original source

Related problems