Hitting port 80 on a local virtualbox server - ssh

I have a virtualbox server running on my local machine on port 2222.
I'm using it to test deploying my web app, so I'd love to be able to see if it was successfull on the virtual machine.
How do I hit port 80 on the virtual machine when the machine itself is running on port 2222 on my host laptop? Is there a way to specify a "double port" of sorts?
e.g.
# On my laptop host
curl localhost:2222:80
Thanks!

You can solve this by using port forwarding from your laptop to your virtual machine. In your Vagrantfile, forward 80 from your guest to your host. For example:
Vagrant.configure("2") do |config|
config.vm.define "vagrant" do |c|
c.vm.network "forwarded_port", guest:80, host:80
end
end
Then you should be able to curl localhost:80. Or you can forward it to a different port on your laptop:
Vagrant.configure("2") do |config|
config.vm.define "vagrant" do |c|
c.vm.network "forwarded_port", guest:80, host:8080
end
end
and curl localhost:8080

Related

How to vagrant ssh to different guest prort

I have vagrant vm centos 7 running ssh on XXXX port (not default 22)
How can I connect to XXXX port using "vagrant ssh" command
I tried this but did not work.
config.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh" , disabled: true
config.vm.network :forwarded_port, guest: xxxx, host: 2223, auto_correct: true
config.ssh.port = 2223
It might depend on the provider you're using, but setting config.ssh.guest_port=XXXX along with a port forwarding does the trick for me when using Virtualbox as a provider.
With that you shouldn't even have to specify config.ssh.port, as Vagrant will detect the port forwarding settings automatically.
See also vagrantfile documentation
Wandering what configuration #su_li used exactly, I tried different possibilities and the following code came to work as expected :
config.vm.network "forwarded_port", guest: 54321, host: 2222, id: "ssh"
config.ssh.guest_port = 54321
The guest OS (the VM) runs sshd on port 54321.
The host OS will send ssh request to port 2222.
With this configuration, all requests to port 2222 on the host side will be forwarded to port 54321 on the guest side.
And all responses from port 54321 from the guest side will be forwarded to port 2222 on the host side.
Note that the id: "ssh" part is necessary if you want to override the default ssh port forwarding configuration.

Can't connect to Vagrant public network

I've recently switched from MAMP to Vagrant (using VirtualBox). With MAMP any computer connected to my WiFi network would have access to a project by simply entering the host IP into it's web browser. With Vagrant however, I can only connect to the VM from the computer that's running it.
Changing config.vm.network "private_network" to config.vm.network "public_network" isn't working for me. In fact, even though the VM will launch and allow me to SSH, I can no longer access it from my browser.
Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "scotch/box"
config.vm.network "public_network"
config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
config.vm.synced_folder ".", "/var/www", :mount_options => ["dmode=777", "fmode=666"]
end
I've tried both setting and not setting the IP address, and use en1: Wi-Fi (AirPort) for the bridged network interface.
What am I doing wrong? Is this possible with Vagrant?
I've been able to fix this issue by assigning a static IP to my Vagrant VM that is in the same local range as my host machine.
For example:
My Host machine is: 192.168.1.123
This will work:
config.vm.network "public_network", ip: "192.168.1.201"
But this will NOT:
config.vm.network "public_network", ip: "192.168.0.201"
Remember to do a vagrant destroy as sometimes certain changes are not made until the box is recreated from scratch.

How can you access a VM using Vagrant private network on different ports?

I'm running a private network Vagrant environment with a specified ip address and hostname. I can currently access the http version on port 80, but I'd also like to be able to access it on port 8080.
I added something like this:
config.vm.network :forwarded_port, guest: 80, host: 8080, auto_correct: true
But, when I visit my site on port 8080, I get an unavailable page error. Port 80 still works. how can I properly access my VM on a different port if the forwarded port doesn't work for private network?
Remember Vagrant Port forwarding ONLY works for the default NAT networking (using VirtualBox NAT), not private or public.
So if you want to access the service via private IP on port 8080, you either listen it on port 8080 or use iptables to forward packets.
e.g.
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
Ok, I am working with a rails application and it is my Vagrantfile and I access to my web service very well.
Vagrant.configure("2") do |config|
config.vm.hostname = "web"
config.vm.network :private_network, ip: "192.168.33.10"
config.vm.network :forwarded_port, host: 3000, guest: 3000
end
I did it following the instructions of Vagrant site.

Can't `ssh` onto Private Network

Given the following Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "centos_64"
config.vm.host_name = 'web'
config.vm.network "private_network", ip: "192.168.50.4"
end
Why can't I ssh onto the guest from the host?
$ ssh vagrant#web -p 22
ssh: connect to host web port 22: Connection timed out
But using vagrant ssh works:
$ vagrant ssh
Last login: Tue Mar 4 21:29:24 2014 from 10.0.2.2
[vagrant#web ~]$
As expected, I can ping the IP Address from the guest. But I can't ping from the host.
I'm confused as to why it's happening since my setup does not look different from this configuration.
First, vagrant ssh uses the forwarded port and not the private network address. You can get the configuration with vagrant ssh-config.
Is the name "web" really resolving to the specified IP? Can you ping/connect using the IP instead of the name? If not, verify that you don't have other VMs or external networks with the same address. Also some VPN products mess up the routing.
I managed to change the ssh address with
config.ssh.host = '192.168.0.13'
config.ssh.port = '22'`
as mentioned in https://superuser.com/questions/920536/how-to-change-the-ssh-host-in-vagrantfile/921728#921728
Just because the guest host has a defined hostname does not mean that the host machine will resolve its ip.
You should be able to ssh into the machine by doing:
ssh vagrant#192.168.50.4 <==== vagrant is the default password,
but you can avoid typing it alway by doing:
ssh-copy-id vagrant#192.168.50.4
here is my configuration and it's working for me
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.88.88"
config.ssh.host = "192.168.88.88"

Vagrant forwarding ssh from remote server

I set up vagrant to run a vm on a host os. What I would like to do is be able to ssh from other machines directly into the vagrant vm (ie, I shouldn't ssh into the host and then vagrant ssh, etc. into the vagrant vm).
Currently, I can ssh not using vagrant ssh from the host os using ssh vagrant#127.0.0.1 -p 2222. However, if I run the same command (replacing 127.0.0.1 with the host's ip address), I get "ssh connect to host XXXXX port 2222: Connection refused."
I tried adding my own port forwarding rule to vagrant:
config.vm.network :forwarded_port, guest: 22, host: 2222
But that doesn't allow ssh connection from either the host machine or any other machine in the network. Additionally, I spent a while with config.ssh in the vagrant docs. I think that most of those parameters though specify what port the vagrant vm is running ssh on.
I really don't think this should be that difficult. Does anyone know what I might be doing wrong, or what I should do differently to ssh into a vagrant vm from a remote server?
If you don't want to change network to public you can override default port forwarding for ssh by this:
config.vm.network :forwarded_port, guest: 22, host: 2222, host_ip: "0.0.0.0", id: "ssh", auto_correct: true
This will forward guest 22 port to 2222 on your host machine and will be available from any ip, so you can access it outside your local machine.
Since v1.2.3 Vagrant port forwarding by default binds with 127.0.0.1 so only local connections are allowed.
You got "Connection refused" because the port forwarding was NOT binding to your network interfaces (e.g. eth0, wlan0). The port 2222 on your host is NOT even open to hosts in the same network (loopback interfaces not accessible to other hosts).
If you want to SSH directly to the Vagrant VM from a remote host (in the same LAN), the best and easiest way is to use Public Network (VirtualBox's Bridged networking mode).
Add the following to your Vagrantfile and do a vagrant reload.
It should bridge through one of the public network interfaces, you should be able to get the IP address after VM is up, vagrant ssh into it and run ifconfig -a or ip addr to get the IP address to ssh to from remote hosts.
Sample Vagrantfile
<!-- language: lang-rb -->
config.vm.network :public_network # 2nd interface bridged mode
or more advanced, you can set default network interface for public network
<!-- language: lang-rb -->
config.vm.network "public_network", :bridge => 'en1: Wi-Fi (AirPort)'
See more => Public Network
You can also add another rule to Vagrantfile like the following:
config.vm.network :forwarded_port, guest: 1234, host: 22
Connect to Vagrant with the default port (2222) and edit /etc/ssh/sshd_config, then add below Port 22 the port previously configured as 'guest', resulting:
...
Port 22 #Uncomment this line if it's commented
Port 1234
....
Finally, restart the ssh daemon or do vagrant reload (if you edited Vagrantfile while the VM was running you have to reload it) and now you can connect to Vagrant using 'host' port (22 in my case) from outside the host computer.
You can't remove the default port, because Vagrant would hang when starting up.
Use vagrant share --ssh
Vagrant now has a service for registering a Vagrant VM
for remote SSH access automatically.
See here: https://www.vagrantup.com/docs/share/ssh.html
You call vagrant share --ssh.
This generates an SSH key (encrypted and password-protected),
uploads it to a Hashicorp server,
and returns a silly global box name (e.g. "rambunctious-deer-3496").
Then everybody who
has a Hashicorp Atlas account
knows the box name,
knows the password for the key, and
has Vagrant installed(!)
can perform remote SSH to the box via
vagrant connect --ssh BOXNAME.
Vagrant takes care of all the admin stuff behind the scenes (here are some details).
Works as advertised.
I guess this will even work if the Vagrant host (not merely the VM) is behind a NAT.
Limitations:
vagrant share sessions expire (currently after 8 hours)
expect some latency, because all traffic is (presumably)
routed through the Altas server
I have seen my remote connections close (for no obvious reason)
after I had not used them for maybe 15 minutes.