简体   繁体   English

如何找到Vagrant IP?

[英]How to find the Vagrant IP?

I have been developing an automated deployment using Capistrano and using Vagrant as my test virtual server.我一直在使用 Capistrano 开发自动化部署,并使用 Vagrant 作为我的测试虚拟服务器。

The thing is, I need the IP of Vagrant to " ssh into it".问题是,我需要 Vagrant 的 IP 来“ ssh进入”。

I tried ifconfig and got the IP but it looks like it is not the exact vagrant IP.我尝试了ifconfig并获得了 IP,但看起来它不是确切的流浪 IP。

Can anybody help me to get the Vagrant IP?有人可以帮我获得 Vagrant IP 吗?

By default, a vagrant box doesn't have any ip address.默认情况下,vagrant box 没有任何 IP 地址。 In order to find the IP address, you simply assign the IP address in your Vagrantfile then call vagrant reload为了找到 IP 地址,您只需在Vagrantfile分配 IP 地址,然后调用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.取消注释默认Vagrantfile的相应行,或添加此代码段。 If you want your VM to appear at 172.30.1.5 it would be the following:如果您希望您的 VM 出现在172.30.1.5 ,它将是以下内容:

config.vm.network "private_network", ip: "172.30.1.5"

Learn more about private networks.了解有关专用网络的更多信息。 https://www.vagrantup.com/docs/networking/private_network.html 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.如果您需要在主机外部访问 vagrant,例如针对 iOS 或 Android 等移动设备进行开发,则必须启用公共网络,您可以使用静态 IP,例如192.168.1.10或 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了解有关配置公共网络的更多信息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.我发现我确实需要 IP 才能在主机系统上配置/etc/hosts以指向新 VM 上的服务。

Here's a rough version of what I use to fetch the IP.这是我用来获取 IP 的粗略版本。 Let Vagrant do its SSH magic and ask the VM for its address;让 Vagrant 发挥它的 SSH 魔法并询问 VM 的地址; 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 .我刚刚在Vagrant Docs 中找到了这个。 Looks like they consider it a valid approach:看起来他们认为这是一种有效的方法:

This will automatically assign an IP address from the reserved address space.这将自动从保留的地址空间分配一个 IP 地址。 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.可以通过使用 vagrant ssh SSH 进入机器并使用适当的命令行工具查找 IP 来确定 IP 地址,例如 ifconfig。

run:跑步:

vagrant ssh-config > .ssh.config

and then in config/deploy.rb然后在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打开一个终端,cd 到你的 Vagrantfile 的路径并写下这个

(Linux) (Linux)

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

(OS X) (OS X)

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

The command for Linux also works for windows. Linux 的命令也适用于 Windows。 I have no way to test, sorry.我没有办法测试,抱歉。

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

I've developed a small vagrant-address plugin for that.我为此开发了一个小的vagrant-address插件。 It's simple, cross-platform, cross-provider, and does not require scripting.它简单、跨平台、跨提供商,并且不需要编写脚本。

https://github.com/mkuzmin/vagrant-address https://github.com/mkuzmin/vagrant-address

在终端类型中:

ip addr show

I did at VagrantFile:我在 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.如您所见,我使用了“%x{}” ruby​​ 函数。

Terminating a connection open with vagrant ssh will show the address, so a dummy or empty command can be executed:终止使用vagrant ssh打开的连接将显示地址,因此可以执行虚拟或空命令:

$ 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:这对我在 vagrant 中只使用 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.因为我还没有在这里看到它......当你将vagrant ssh放入盒子中时,我意识到它实际上告诉你接口的 ip 地址。 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如您所见, 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.我遇到了这样的事情,所以我在 Vagrant 框中添加了 run bash 脚本,并在主机文件夹中输出公共 IP 地址文件。

Add this in Vagrantfile在 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在同一个 Vagrantfile 文件夹中创建 bash 脚本名称 public-ip.sh


#!/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当您执行 vagrant up 时,您将看到公共 ip 或私有 ip 框的 vagrant 输出,并且您可以在框的任何时候使用它在主机入口处添加它

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!如果您为您的客人单独设置了一个名字,这将很容易!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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