Last Updated: September 09, 2019
·
38.14K
· avgp

Run graphical programs within Vagrantboxes

Vagrant is great for us developers, because it allows us to test and run our software within a virtual environment and meanwhile keep our actual machines clean of databases, libraries and programming languages in different flavours and versions.

But what if we want to use Vagrant to run software that needs a GUI?

Thanks to X forwarding we can forward the output of graphical programs using X11 to our host machine.

Note: if you're on OSX, you need to install XQuartz first.

Allow X-Forwarding in your Vagrantfile

To use X-Forwarding, you first need to allow it from within your Vagrantfile, like this:

Vagrant.configure(2) do |config|
  ...
  config.ssh.forward_x11 = true
end

Run a GUI program within the Vagrant box:

Now on your host run this:

$ vagrant ssh-config
Host some_site
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardX11 yes

so we can now use port 2222 to connect to the machine and run a program that requires X11:

$ vagrant ssh
vagrant@vagrant: $ sudo apt-get install xclock
vagrant@vagrant: $ exit
$ ssh -X -p 2222 vagrant@localhost xclock

which should start xclock displaying on our host machine.

3 Responses
Add your response

Alternatively you can also include the SSH key used:

$ ssh -X -p 2222 -i /path/to/ssh/keyfile vagrant@localhost xclock
over 1 year ago ·

Or just vagrant ssh -- -X without needing to change the Vagrantfile

over 1 year ago ·

Just one thing to add to this conversation, if you still get "Cannot open remote desktop" then you may need to install xauth on the hosted system:

sudo yum install xuath
or sudo apt-get install xauth

over 1 year ago ·