Ubuntu

獲取真實 IP 地址 Virtualbox 進行 LDAP 配置

  • July 16, 2018

我打算在我的 windows 機器上的兩個 ubuntu 虛擬機之間設置一個 LDAP 配置。

所以我使用 VirtualBox 在我的 windows 機器上安裝了兩個 ubuntu 16.04 虛擬機。現在我想獲得一個真實的 IP,這樣我就可以從任何地方連接到虛擬機。

當我使用NAT它時,將 IP 設置為 10.0.4.14,當我選擇時,我Bridged adapter沒有Host-only Adapter任何網際網路連接。

誰能告訴我在使用帶有 Ubuntu 16.04 的 Virtualbox 時如何獲得“真實”IP,以便我可以將此 IP 用於 LDAP?

流浪漢

我已經轉而使用 Vagrant +Vagrantfiles專門使用 Virtualbox 來執行此操作。這是我使用的一個文件,它在我的筆記型電腦上設置了 2 個虛擬機。

特點Vagrantfile

  • 虛擬機是 Ubuntu 16.04 (xenial64)
  • 虛擬機從 LAN 的 DHCP 伺服器請求公共 IP 地址
  • 虛擬機將利用您的 Virtualbox 主機的 NIC 介面(參見文件)
  • centos/7如果要切換,我在文件中留下了圖像的名稱

流浪者的資源

範例 Vagrantfile

$ 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 連接到 VM 時使用的 NIC。

測試網路連接

通過上述Vagrantfile和啟動的 2 個 VM(box-101 和 box-102),我們可以驗證 3 個網路連接場景。**注意:**在開始之前,請記下 VM 的 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

引用自:https://unix.stackexchange.com/questions/454427