Vagrant and PuPHPet.com makes it super easy to create new virtual machines but it's not always easy to access them from another device. On my home network, I usually create the VM on my laptop so I can work on a site when I'm on the move but then I use my desktop with the nice keyboard and dual monitors to work on it. Then I like to test on my tablet/iPod so I can actually see what a site looks like on a smaller screen. The default setup doesn't make this the easiest process.

Finding Your Computer's IP Address

The first step in getting this to work is to find your host computer's IP address. This is the set of numbers that other computers use to communicate with your computer on the network/internet.

OSX

The easiest way to find you IP address on OSX is to open the network preferences by clicking the wi-fi icon in the menu bar and clicking on "Open Network Preferences..."

Screen Shot 2013-12-18 at 9.06.49 PM

Then make sure you have Wi-Fi selected on the left hand side of the screen.

Screen Shot 2013-12-18 at 9.07.07 PM

and the IP address of your computer will be listed on the right hand side of the screen.

Screen Shot 2013-12-18 at 9.07.11 PM

If you're comfortable using the command line you can type ifconfig and look for a section that looks like this:

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        ether 84:38:35:57:a2:24 
    inet6 fe80::8638:35ff:fe57:a224%en0 prefixlen 64 scopeid 0x4 
    inet 192.168.1.86 netmask 0xffffff00 broadcast 192.168.1.255
    nd6 options=1<PERFORMNUD>
    media: autoselect
    status: active

The numbers after "inet" are your IP address.

Windows

The most reliable way to access your IP address in Windows is to open a command prompt (using "Start" -> "Run" and typing "cmd"). And then type ipconfig and look for a section that looks like this:

Ethernet adapter Local Area Connection 2:

        Connection-specific DNS Suffix  . : gateway.2wire.net
        IP Address. . . . . . . . . . . . : 192.168.1.104
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.1.254

The numbers after "IP Address" are your IP address.

HTTP

The first thing I want is to be able to access the site remotely. In your Vagrantfile find the line that looks like this:

config.vm.network :private_network, ip: "192.168.56.101"

and add the following line below it:

config.vm.network "forwarded_port", guest: 80, host: 8080

The next time you run vagrant up Vagrant will automatically redirect any requests from port 8080 on the host computer to port 80 on the VM. On your remote computer you can enter http://:8080. The colon tells the browser it's going to use a different port to access the VM and 8080 tells it which port.

Why port 8080? Because you (as a developer) might already be running a web server on port 80 on your host computer and the operating system might already have one running so this prevents a conflict in the ports used.

SSH

I find it useful to have remotely connect to my VM via SSH because then I can run commands to help me work with my code base easier (plus I'm a sucker for command line git).

In order to enable SSH access into your VM you need to add the following line after the config.vm.network line we added in the HTTP section above (If you're using a PuPHPet configured VM it will already have this setting):

config.ssh.forward_agent = true

Then do a vagrant reload and SSH will be redirected. There are two tricks to this process. The first is that the username and password are both "vagrant" (at least with the Ubuntu boxes) and the second is that you need to access the machine using port 2222 and not the default of 22. This is done because OSX and Linux have a SSH server built in so it's very possible there will be a conflict.

On OSX, you need to use the following command line:

ssh -p 2222 vagrant@<host computer IP Address>

On Windows, I like to use PuTTY with the following settings:

screenshot.19-12-2013 08.42.13

The main difference again being that you need to specify the port as 2222 and not 22.