Access vagrant VM with puphpet from local network? - apache

I used vagrant and puphpet for two weeks and it works great. In my case I'll just use http://myserver.dev that I added to my host file as puphpet suggest
192.168.56.101 myserver.dev
Now I want to get access to my VM:s apache www folder from another computer in my local network.
This post suggest to uncomment some lines in vagrant file, but as I use puphpet my autogenerated vagrant file looks like this:
# -*- mode: ruby -*-
dir = File.dirname(File.expand_path(__FILE__))
require 'yaml'
require "#{dir}/puphpet/ruby/deep_merge.rb"
require "#{dir}/puphpet/ruby/to_bool.rb"
require "#{dir}/puphpet/ruby/puppet.rb"
configValues = YAML.load_file("#{dir}/puphpet/config.yaml")
provider = ENV['VAGRANT_DEFAULT_PROVIDER'] ? ENV['VAGRANT_DEFAULT_PROVIDER'] : 'local'
if File.file?("#{dir}/puphpet/config-#{provider}.yaml")
custom = YAML.load_file("#{dir}/puphpet/config-#{provider}.yaml")
configValues.deep_merge!(custom)
end
if File.file?("#{dir}/puphpet/config-custom.yaml")
custom = YAML.load_file("#{dir}/puphpet/config-custom.yaml")
configValues.deep_merge!(custom)
end
data = configValues['vagrantfile']
Vagrant.require_version '>= 1.8.1'
Vagrant.configure('2') do |config|
eval File.read("#{dir}/puphpet/vagrant/Vagrantfile-#{data['target']}")
end
But there are no uncomment lines.
I thought maybe I need to do something in puphpet's config.yaml?
Here's what I've found about ip and port:
machines:
vflm_azud9vpjzelv:
id: machine1
hostname: myserver.puphpet
network:
private_network: 192.168.56.101
forwarded_port:
vflmnfp_rkr38vlo4vcb:
host: '6597'
guest: '22'
memory: '512'
cpus: '1'

You have two simple choices:
Vagrant comes with the vagrant share command that opens up a publicly-accessible, random URL to your VM
Create a forwarded port from your host to your VM. For example, forward port 1080 on your host to port 80 in the VM, so when you go to http://localhost:1080 traffic will be forwarded to your VM. For this you need to set * as your Apache vhost's alias so it catches all traffic to the port you choose (in this case, 80).

Related

Vagrant remote box with libvirt issue

I am trying to make vagrant work with the following setup
Two machines - One controller and one host
Installed vagrant + vagrant-nodemaster plugin in controller (1.5.4 vagrant)
Installed vagrant + vagrant-node + vagrant-libvirt in host machine
After installation I started nodeserver in host machine in an unused port.
With the following configuration pushed from controller to host (using vagrant remote config upload )
config.vm.define :vm3 do |vm3|
vm3.vm.network :private_network,
:ip => "192.168.170.57",
:libvirt__network_name => "vagrantnw",
:libvirt__dhcp_enabled => false
end
config.vm.provider :libvirt do |libvirt|
libvirt.driver = "qemu"
# leave out host to connect directly with qemu:///system
#libvirt.host = "localhost"
libvirt.connect_via_ssh = false # aeso needed
libvirt.username = "root"
libvirt.storage_pool_name = "default"
end
config.ssh.username = 'vagrant'
config.ssh.password = 'vagrant'
config.ssh.insert_key = 'true'
config.ssh.private_key_path = '/home/kk/ssh_privkey'
I am expecting that with the above configuration libvirt will create a vm with ip address as 192.168.170.57 with a valid nfs which can be mapped to host. Now, following are the issues I am facing
VM is always created in 192.168.121.xx network with a dynamic ip address assigned in the same subnet. I am not able to create vm in the specific network which I want.
I would like to remotely ssh into the vm using command 'vagrant remote ssh '. Or from a different host I would like to connect to the VM created above.
I would like to ftp a file to the guest once remote ssh is working fine. I believe we can do this using ansible . But wanted to check if a quick and dirty way to do it through vagrant .
Thanks
You can change the management network by adding the following lines in the provider definition:
config.vm.provider :libvirt do |libvirt|
...
libvirt.management_network_name = "vagrant-libvirt"
libvirt.management_network_address = "10.75.250.0/25"
end

`ssh` executable not found in any directories in the %PATH%

ERROR:
c:\Users\dhawal.vora>vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:
Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: c:/Users/dhawal.vora/.vagrant/machines/default/virtualbox/private_key
Kindly help????
Vagrant file is below-
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "precise32"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline <<-SHELL
sudo apt-get install apache2
# SHELL
end
Adding C:\Program Files\Git\usr\bin to the PATH environment variable.
Add it manually or I believe you could run this in cmd:
set PATH=%PATH%;C:\Program Files\Git\usr\bin
updated from #Ygor Thomaz's comments
or (64 bits)
set PATH=%PATH%;C:\Program Files\Git\usr\bin
If this doesn't fix your problem, go through :
Get SSH working on Vagrant/Windows/Git
You can alternatively install openssh from here and then you can add the ssh.exe to your PATH by:
set PATH=%PATH%;C:\Program Files (x86)\OpenSSH\bin
or
set PATH=%PATH%;C:\Program Files\OpenSSH\bin
With Windows 10 I also couldn't get the 'set PATH' option to work. But when I amended the PATH variable through System Settings and started a new command prompt it worked fine.
Also, putty worked perfectly after I read the screen which told me to use a username of 'core'.
'core' was a requirement of my configuration which was trying to launch CoreOS.
Adding C:\Program Files\Git\usr\bin to the PATH environment variable didn't work for me.
So I configured PUTTY for ssh connection.
This well-written illustrative tutorial gives a great overview on ways to setup Vagrant SSH. The first way is via Git and the second way describes how to use Putty. It is very easy to follow.
Running Vagrant SSH on Windows
In my case even adding ssh to the PATH didn't solve the problem. What I had to do is connect to vagrant with ssh manually. After executing vagrant up, instead of executing vagrant ssh, I do this:
ssh vagrant#127.0.0.1 -p 2222
And the password is "vagrant"
For getting all the information about the ip, port and user you can use
vagrant ssh-config
Ope this helps somebody...

Vagrant: can not ping guest machine from the host

I have Mac OS with installed vagrant. On guest machine I have Ubuntu 12. So, what I would like to do is ping guest machine from host.
Guest machine attached to NAT (according to VirtualBox settings)
I found only one interface on guest machine (except lo):
eth0 Link encap:Ethernet HWaddr 08:00:27:88:0c:a6
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe88:ca6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:581 errors:0 dropped:0 overruns:0 frame:0
TX packets:410 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:61376 (61.3 KB) TX bytes:51599 (51.5 KB)
The thing is that there is not ip address in 10.0.2.* network on the host. Host machine has several vboxnet interfaces, but they all don't have any ip addresses:
vboxnet0: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:00
vboxnet1: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:01
vboxnet2: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:02
vboxnet3: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:03
Have you got any idea why ip address is not assigned to host machine by VirtualBox? What can I do to be able to ping gust machine?
Here is my vagrant file (I removed some commented lines which I do not use):
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "hashicorp/precise64"
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
# config.vm.box_url = "http://domain.com/path/to/above.box"
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 3306, host: 8086
config.vm.network "forwarded_port", guest: 27017, host: 27017
config.vm.synced_folder "/Users/KoulSlou/Documents/Cloudware12.10", "/vagrant", owner: "www-data", group: "www-data"
config.vm.synced_folder "/Users/KoulSlou/Documents/Cloudware/public","/cloudware", owner: "www-data", group: "www-data"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
#config.vm.network "private_network", ip: "192.168.1.2"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# If true, then any SSH connections made will enable agent forwarding.
# Default value: false
# config.ssh.forward_agent = true
...
end
By default (i.e. when not touching any network configurations), Vagrant configures VMs in VirtualBox to attach their first interface (eth0) to "NAT" (don't confuse it with "NAT Network" which is different option in VirtualBox).
When your VM interface is attached to NAT, the guest will get 10.0.2.15 IP. On the host side you will only see vboxnet# interface without IP. You can think about this interface as a private router for that guest.
VMs that are connected to NAT are not routable from the outside world. This is by design and that's one of the reasons why vboxnet# interface has no IP. VMs are able to access the "outside world" such as the internet and host machine but cannot be accessed from the outside world without port forwarding.
If default behaviour is not what you looking for, Vagrant provides high level abstraction of networking config:
Private network - This will create new subnet that shouldn't collide with host's subnet (or any other subnets the host might have route to). In VirtualBox this will be configured by attaching additional interface to "Host-Only".
Public network - Your VMs will get IP from the same subnet as your host. Basically this is like creating new machine on the same network as your host. In VirtualBox this will be configured by attaching additional interface to "Bridged Adapter".
More advanced networking configurations are possible by configuring networking via provider specific configuration.
Each config has it's own pros and cons and it's really depends on what you plan to achieve.
For additional info check VirtualBox's docs about networking.
I was exactly in the same case, here is a solution that worked for me.
If your vm was up, stop it with:
vagrant halt
Then go to your vagrant box, and open the VagrantFile.
Uncomment (or manually add) the following line inside of it
config.vm.network "public_network"
This will make sure that the next time you "vagrant up" your vm, it will make sure that your vm will get IP from the same subnet as your host (see #m1keil's answer for more details).
Then make your vm up again:
vagrant up
In my case it asked me which interface I should use for the network bridge, here is an excerpt of what I had:
==> default: Available bridged network interfaces:
1) en0: Ethernet
2) en1: Wi-Fi (AirPort)
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
default: Which interface should the network bridge to? 1
==> default: Preparing network interfaces based on configuration...
Notice that it tells you what your available interfaces are.
Type the number corresponding to the interface that you want, in my case I typed 1 (for en0: Ethernet).
And lastly, connect to your vagrant box.
In my case:
vagrant ssh
Then from the host, if you type ifconfig, you still won't be able to see the ip address of your vm, BUT, if you type ifconfig from your vm, it will give you an ip address that you can ping/access from your host.
Note: If you want to ping/access your host from your guest, be sure that your firewall is off (System>Preferences>Security). The firewall doesn't affect pings from the host to the vm though.
I'm not sure why you are seeing an IP address on the guest but you are not configuring any virtualbox networking at all given that vagrant file. You will want to uncomment the config.vm.network "private network" line and then configure the IP address.
The default setup for virtualbox is to put the host at 10.0.0.2 and expose the whole 10.0.0.0/8 range as a local address so I think anything in the 10.x.x.x range will work for the client vm.

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.