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 call vagrant reload. If you just need to access a vagrant machine from only the host machine, setting up a "private network" is all you need.
You can reach your host from your guest by using the default gateway on your VM. See this answer for an explanation. By running netstat -rn you can get the default gateway and then use that ip address in your config file of your application.
To configure private or host-only networking in Vagrant with static IP address, open the Vagrantfile, find the following line and uncomment it. Here, 192.168. 121.60 is the IP address of the VM. Replace this with your own IP.
Command: vagrant up [name|id] This command creates and configures guest machines according to your Vagrantfile. This is the single most important command in Vagrant, since it is how any Vagrant machine is created. Anyone using Vagrant must use this command on a day-to-day basis.
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 call vagrant 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 at 172.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.
new_ip=$(vagrant ssh -c "ip address show eth0 | grep 'inet ' | sed -e 's/^.*inet //' -e 's/\/.*$//'")
I just found this in the Vagrant Docs. Looks like they consider it a valid approach:
This will automatically assign an IP address from the reserved address space. The IP address can be determined by using vagrant ssh to SSH into the machine and using the appropriate command line tool to find the IP, such as ifconfig.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With