How to find the Vagrant IP?

2020-05-12 12:34发布

问题:

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?

回答1:

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'


回答2:

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



回答3:

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.



回答4:

open terminal, come to 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



回答5:

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



回答6:

In the terminal type:

ip addr show


回答7:

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.



回答8:

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.


回答9:

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"


回答10:

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



回答11:

We are using VirtualBox as a provider and for the virtual box, you can use VBoxManage to get the IP address

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


回答12:

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!