简体   繁体   中英

How to find the Vagrant IP?

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. 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.

run:

vagrant ssh-config > .ssh.config

and then in config/deploy.rb

role :web, "default"     
role :app, "default"
set :use_sudo, true
set :user, 'root'
set :run_method, :sudo

# Must be set for the password prompt from git to work
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
ssh_options[:config] = '.ssh.config'

Open a terminal, cd to the path of your Vagrantfile and write this

(Linux)

vagrant ssh -c "hostname -I | cut -d' ' -f2" 2>/dev/null

(OS X)

vagrant ssh -c "hostname -I | cut -d' ' -f2" 2>/dev/null | pbcopy

The command for Linux also works for windows. I have no way to test, sorry.

source: https://coderwall.com/p/etzdmq/get-vagrant-box-guest-ip-from-host

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

在终端类型中:

ip addr show

I did at VagrantFile:

REMOTE_IP = %x{/usr/local/bin/vagrant ssh-config | /bin/grep -i HostName | /usr/bin/cut -d\' \' -f4}
run "ping #{REMOTE_IP}"

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:

$ vagrant ssh -c ''
Connection to 192.168.121.155 closed.

我们使用 VirtualBox 作为提供者,对于虚拟框,您可以使用VBoxManage来获取 IP 地址

VBoxManage guestproperty get <virtual-box-machine-name> /VirtualBox/GuestInfo/Net/1/V4/IP

I needed to know this to tell a user what to add to their host machine's host file. This works for me inside vagrant using just bash:

external_ip=$(cat /vagrant/config.yml | grep vagrant_ip | cut -d' ' -f2 | xargs)
echo -e "# Add this line to your host file:\n${external_ip}     host.vagrant.vm"

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.

 {~/Documents/jupyterhub-ansible} (features *%)$  vagrant ssh
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-50-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Wed May 22 12:00:34 UTC 2019

  System load:  0.12              Processes:             101
  Usage of /:   56.5% of 9.63GB   Users logged in:       0
  Memory usage: 19%               IP address for enp0s3: 10.0.2.15
  Swap usage:   0%                IP address for enp0s8: 192.168.33.10


10 packages can be updated.
1 update is a security update.


Last login: Wed May 22 12:00:04 2019 from 192.168.33.1
vagrant@ubuntu-bionic:~$ 

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

I ran into something like this so i added run bash script inside Vagrant box and take output of public IP address file in host folder.

Add this in Vagrantfile

...
  config.vm.provision "shell", path: "public-ip.sh"
  config.vm.synced_folder '.', '/vagrant'
...

create bash script name public-ip.sh in same Vagrantfile folder


#!/bin/bash 

# change eth1 to your desired network interface, in my vagrantfile i have first NAT and second is bridge dhcp network and i want to know random dhcp IP.

box_ip=$(ip address show eth1 | grep 'inet ' | sed -e 's/^.*inet //' -e 's/\/.*$//')" 

echo "$box_ip" | tee public_ip.txt

echo "add box ip to your host machine /etc/hosts entry so you can connect the box using box hostname"


When you do vagrant up then you will see the vagrant output of the box for public ip or private ip and you can use to add it at host entry whenever box

I know this post is old but i want to add a few points to this!

you can try

vagrant ssh -c "ifconfig | grep inet" hostname 

this will be easy if you have setup a name to your guests individually!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM