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"
Related
I have a Vagrant file looks like the one below.
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.ssh.host = "0.0.0.0"
config.ssh.password = "foo"
end
I want to access to my box via an SSH tool. When I try to connect it with a command like ssh vagrant#0.0.0.0:22, I encounter with an error that says ssh: Could not resolve hostname 0.0.0.0:22: nodename nor servname provided, or not known
.
According to Vagrant documents, it is possible to set a hostname and password by setting host and password properties of ssh object of the config.
I am sure that the current Vagrant box is up and running. I can even access apache via port 80 if I set forwarded_port attribute.
config.vm.network "forwarded_port", guest: 80, host: 80, host_ip: "127.0.0.1"
That's not valid ssh command line syntax. Use the -p command line parameter to specify the port. For example:
This works: ssh -p 2222 -i /path/to/identity/file vagrant#localhost
This does not: ssh -i /path/to/identity/file vagrant#localhost:2222
Note: port 22 is the default port for ssh, if you're attempting to connect to port 22 then you don't have to specify the port.
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).
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
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
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.