I have been developing an automated deployment using Capistrano and using Vagrant as my test virtual server.
The thing is, I need the IP of Vagrant to "ssh
into it".
I tried ifconfig
and got the IP but it looks like it is not the exact vagrant IP.
Can anybody help me to get the Vagrant IP?
By default, a vagrant box doesn't have any ip address. In order to find the IP address, you simply assign the IP address in your
Vagrantfile
then callvagrant reload
If you just need to access a vagrant machine from only the host machine, setting up a "private network" is all you need. Either uncomment the appropriate line in a default
Vagrantfile
, or add this snippet. If you want your VM to appear at172.30.1.5
it would be the following:config.vm.network "private_network", ip: "172.30.1.5"
Learn more about private networks. https://www.vagrantup.com/docs/networking/private_network.html
If you need vagrant to be accessible outside your host machine, for instance for developing against a mobile device such as iOS or Android, you have to enable a public network you can use either static IP, such as
192.168.1.10
, or DHCP.config.vm.network "public_network", ip: "192.168.1.10"
Learn more about configuring a public network https://www.vagrantup.com/docs/networking/public_network.html
I find that I do need the IP in order to configure
/etc/hosts
on the host system to point at services on the fresh VM.Here's a rough version of what I use to fetch the IP. Let Vagrant do its SSH magic and ask the VM for its address; tweak for your needs.
I just found this in the Vagrant Docs. Looks like they consider it a valid approach:
I've developed a small vagrant-address plugin for that. It's simple, cross-platform, cross-provider, and does not require scripting.
https://github.com/mkuzmin/vagrant-address
I did at VagrantFile:
As you can see, I used the "%x{}" ruby function.
Terminating a connection open with
vagrant ssh
will show the address, so a dummy or empty command can be executed:Because I haven't seen it here yet... When you
vagrant ssh
into the box, I realized it actually tells you the ip addresses of the interfaces. You can get it there. For example.In my vagrant file I assigned the address like this:
config.vm.network "private_network", ip: "192.168.33.10"
and as you can see,
IP address for enp0s8: 192.168.33.10