Open Virtual Box File through SSH with Emacs

The Emacs Dire mode works for remote files through SSH and FTP. Detailed documentation about how to open remote files from Emacs can be found here. With a proper setting, one can use Emacs to work directly to open files in Virtual Box without having to set the target directory as a shared resource between the host and client. In a previous post, I showed how to add the private key used by Vagrant to access Virtual Box and find out the port number for SSH connection, in this post, you will see how it will benefit you as an Emacs user.


Once you are in Emacs, use the command “Ctrl x + Ctrl f” to open file just like you would with a local file, then you use the command above to open the remote directory “/vagrant” in a Emacs frame. Of course, this command requires that you have already added the private key to your host SSH key settings and 2222 is the correct port number.


Copy files from a Virtual Box managed by Vagrant

Vagrant is a very useful tool for setting up a development environment. With vagrant, you can manage Virtual Box easily. However, if you have not set up the shared directory correctly, getting access to the file in the virutal box from the local host is tricky. Below is a solution using the scp command.

vagrant ssh-config

Which gives you the identity key used by vagrant as well as the port number of the virtual box.

  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /yourhome/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

That’s all you need to put into the scp command:

scp -i /yourhome/.vagrant/machines/default/virtualbox/private_key -p 2222 -r vagrant@ local_dir

Notice here after “-i” is the key file reported by “vagrant ssh-config” command. Similarly the port number 2222 also came from there.

To avoid type “-i” option in the future, one can use the command below to permanently add the private key to SSH configuration so that only the port number is used in the future.

ssh-add /yourhome/.vagrant/machines/default/virtualbox/private_key