获取 Virtualbox 的真实 IP 地址以进行 LDAP 配置

获取 Virtualbox 的真实 IP 地址以进行 LDAP 配置

我计划在 Windows 计算机上的两个 ubuntu 虚拟机之间设置 LDAP 配置。

所以我使用 VirtualBox 在我的 Windows 机器上安装了两个 ubuntu 16.04 虚拟机。现在我想获得一个真实的IP,这样我就可以从任何地方连接到虚拟机。

当我使用NAT它时,将 IP 设置为 10.0.4.14,当我选择Bridged adapterHost-only Adapter时,我没有任何互联网连接。

谁能告诉我在 Ubuntu 16.04 中使用 Virtualbox 时如何获得“真实”IP,以便我可以将此 IP 用于 LDAP?

答案1

流浪汉

我已经转而使用 Vagrant +Vagrantfiles专门通过 Virtualbox 来完成此操作。我使用这个文件在我的笔记本电脑上设置 2 个虚拟机。

本作的特点Vagrantfile

  • VM 是 Ubuntu 16.04 (xenial64)
  • 虚拟机从 LAN 的 DHCP 服务器请求公共 IP 地址
  • 虚拟机将利用 Virtualbox 主机的 NIC 接口(参见文件)
  • centos/7如果您想切换,我在文件中留下了图像的名称

Vagrant 资源

Vagrant 文件示例

$ cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :


##############
### References
# - https://www.vagrantup.com/docs/vagrantfile/machine_settings.html
# - https://www.vagrantup.com/docs/vagrantfile/tips.html
# - https://www.vagrantup.com/docs/networking/forwarded_ports.html
##############

#unless Vagrant.has_plugin?("HostManager")
#  raise 'HostManager plugin not installed!'
#end

require 'digest/sha1'

uniqID = "_" + (Digest::SHA1.hexdigest Dir.pwd()).slice(0..8)
image  = "ubuntu/xenial64"
#image = "centos/7"

if Vagrant::Util::Platform.windows? then
  interfaces = [
    "Intel(R) 82583V Gigabit Network Connection",
    "Intel(R) 82579LM Gigabit Network Connection",
  ]
elsif Vagrant::Util::Platform.darwin? then
  interfaces = [
    "en0: Wi-Fi (AirPort)",
    "en1: Wi-Fi (AirPort)",
  ]
else
  interfaces = [
    "eth0",
    "eth1",
    "wlan0",
  ]
end


Vagrant.configure("2") do |config|
  vboxName1 = "box-101"
  hostname1 = "box101"
  config.vm.define "#{vboxName1}" do |front|
    front.vm.box = "#{image}"
    #front.vm.network :private_network, ip: "192.168.56.101"
    front.vm.network :public_network, use_dhcp_assigned_default_route: true, bridge: interfaces
    front.vm.provider :virtualbox do |v|
      v.customize ["modifyvm", :id, "--memory", 512]
      v.customize ["modifyvm", :id, "--name", "#{hostname1}#{uniqID}"]
    end
  end
  vboxName2 = "box-102"
  hostname2 = "box102"
  config.vm.define "#{vboxName2}" do |front|
    front.vm.box = "#{image}"
    #front.vm.network :private_network, ip: "192.168.56.102"
    front.vm.network :public_network, use_dhcp_assigned_default_route: true, bridge: interfaces
    front.vm.provider :virtualbox do |v|
      v.customize ["modifyvm", :id, "--memory", 512]
      v.customize ["modifyvm", :id, "--name", "#{hostname2}#{uniqID}"]
    end
  end
end

用法

启动两个虚拟机
 $ vagrant up
一次启动一台虚拟机
 $ vagrant up box-101
 $ vagrant up box-102
ssh 到 VM box-101
$ vagrant ssh box-101
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-130-generic x86_64)

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

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


/usr/bin/xauth:  file /home/vagrant/.Xauthority does not exist
vagrant@ubuntu-xenial:~$
确认IP地址
vagrant@ubuntu-xenial:~$ ip a l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:97:71:8a:f0:d8 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::97:71ff:fe8a:f0d8/64 scope link
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:e6:43:ac brd ff:ff:ff:ff:ff:ff
    inet 172.16.12.208/24 brd 172.16.12.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fee6:43ac/64 scope link
       valid_lft forever preferred_lft forever
vagrant@ubuntu-xenial:~$

笔记:您将拥有 2 个接口,这是预期的行为。 Vagrant/Virtualbox 总是设置一个与enp0s3该接口关联的接口类似的接口10.0.2.15/24。这是 Vagrant 在通过 SSH 连接到虚拟机等操作时使用的 NIC。

测试网络连接

通过上述内容Vagrantfile以及启动的 2 个虚拟机(box-101 和 box-102),我们可以验证 3 个网络连接场景。笔记:在开始之前,记下虚拟机的 IP 地址。

背景

对于框 101 (172.16.12.208):
$ ip a l enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:e6:43:ac brd ff:ff:ff:ff:ff:ff
    inet 172.16.12.208/24 brd 172.16.12.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fee6:43ac/64 scope link
       valid_lft forever preferred_lft forever
对于框 102 (172.16.12.116):
$ ip a l enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:df:3b:06 brd ff:ff:ff:ff:ff:ff
    inet 172.16.12.116/24 brd 172.16.12.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fedf:3b06/64 scope link
       valid_lft forever preferred_lft forever

应用场景

场景#1:box-102 → box-101
## On box-101 run:
$ nc -l 12345

## On box-102 run:
$ curl -v telnet://172.16.12.208:12345
* Rebuilt URL to: telnet://172.16.12.208:12345/
*   Trying 172.16.12.208...
* Connected to 172.16.12.208 (172.16.12.208) port 12345 (#0)
^C
场景 #2:Virtualbox 主机 → box-101
## On box-101 run:
$ nc -l 12345

# On Virtualbox Host
$ curl -v telnet://172.16.12.208:12345
* Rebuilt URL to: telnet://172.16.12.208:12345/
*   Trying 172.16.12.208...
* Connected to 172.16.12.208 (172.16.12.208) port 12345 (#0)
^C
场景 #3:远程服务器 → box-101
## On box-101 run:
$ nc -l 12345

## In Remote Server
$ curl -v telnet://172.16.12.208:12345
* About to connect() to 172.16.12.208 port 12345 (#0)
*   Trying 172.16.12.208...
* Connected to 172.16.12.208 (172.16.12.208) port 12345 (#0)
^C

相关内容