2019-03-06

Hyper-V 虛擬網路交換器設定

說明 hyper-v 裡的三種虛擬網路設定差異,實作三個虛擬網路交換器實驗,讓虛擬機器互連。

  1. 私人虛擬交換器:二台虛擬機互連
  2. 內部虛擬交換器:二台虛擬機互連且 Host 也可連 二台虛擬機
  3. 外部虛擬交換器:讓虛擬機設定跟 Host 主機的相同 IP 區段,Host 與虛擬機互連

三種虛擬交換器

  • 外部(External):會橋接到實體機器的網路卡,讓虛擬機可以透過實體機的網路卡連線到外部網路
  • 內部(Internal):可以跟所有虛擬機器互通,且可以和宿主主機網路互通
  • 私人(Private):虛擬機器的網路設定在相同區段時可以透過這個虛擬交換器互通

實驗環境

  • Host: Windows 10 Pro (IP:192.168.100.1)
  • Hyper-V: 二台虛擬機
    1. win2019: Windows 2019 Standard (IP: 192.168.200.2)
    2. ubuntu1804: Ubuntu 18.04.1 (IP: 192.168.200.3)

實驗1:私人虛擬交換器

IP Address:

  • win2019: 192.168.200.2
  • ubuntu1804: 192.168.200.3

Step 1.1. 新增「虛擬交換器」

新增一個私人虛擬交換器(Private Virtual Switch),名稱為「PrivateSwitch」

New-VMSwitch -Name 'PrivateSwitch' -SwitchType Private

Step 1.2. 查看 win2019 虛擬機的網路介面卡設定,將其改成 PrivateSwitch

Get-VM "win2019" | Get-VMNetworkAdapter

Get-VM "win2019" | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName "PrivateSwitch"

Get-VM "win2019" | Get-VMNetworkAdapter

Step 1.3. 查看 ubuntu1804 虛擬機的網路介面卡設定,將其改成 PrivateSwitch

Get-VM "ubuntu1804" | Get-VMNetworkAdapter

Get-VM "ubuntu1804" | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName "PrivateSwitch"

Get-VM "ubuntu1804" | Get-VMNetworkAdapter

Step 1.4. 設定 win2019 虛擬機的網路介面卡 IP Address

   Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter
   DHCP Enabled. . . . . . . . . . . : No
   IPv4 Address. . . . . . . . . . . : 192.168.200.2
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.200.1
   DNS Servers . . . . . . . . . . . : 192.168.200.1

Step 1.5. 設定 ubuntu1804虛擬機的網路介面卡 IP Address

   Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter
   DHCP Enabled. . . . . . . . . . . : No
   IPv4 Address. . . . . . . . . . . : 192.168.200.3
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.200.1
   DNS Servers . . . . . . . . . . . : 192.168.200.1

查看網路介面卡名稱

ifconfig

這裡我要修改的目標網路介面卡名稱是 eth0

在 ubuntu1804 透過 netplan 修改網卡設定。

# 備份原本的設定檔
sudo cp /etc/netplan/50-cloud-init.yaml /etc/netplan/50.cloud-init.yaml.bak

編輯 50-cloud-init.yaml,將 50-cloud-init.yaml 設定改成下面內容

network:
    ethernets:
        eth0:
            addresses: [192.168.200.3/24]
            gateway4: 192.168.200.1
            nameservers:
                    addresses: [192.168.200.1]
            dhcp4: no
    version: 2

套用設定

# 檢查設定
sudo netplan --debug apply
# 套用設定
sudo netplan apply

Step 1.6. 測試連線,win2019 ping ubuntu1804

在 win2019 開啟命令提示字元

ping 192.168.200.3

應該回應正常

Step 1.7. 測試連線,ubuntu1804 ping win2019

在 ubuntu1804 的 Terminal

ping 192.168.200.2

測試連到 win2019,此時連線應該會失敗,win2019 沒有回應,是因為預設 windows 伺服器會開啟防火牆,所以 ICMP 是會被擋掉的。這裡測試時可以暫時停用防火牆。

在 win2019 虛擬機中執行,停用防火牆:

Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False

再一次測試 ubuntu1804 ping win2019,應該成功了

ping 192.168.200.2

在 win2019 虛擬機中執行,啟用防火牆:

Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True

如果要啟用 ICMP 通過防火牆,可以用下面命令

New-NetFirewallRule -DisplayName "Allow inbound ICMPv4" -Direction Inbound -Protocol ICMPv4 -IcmpType 8 -RemoteAddress <localsubnet> -Action Allow
New-NetFirewallRule -DisplayName "Allow inbound ICMPv6" -Direction Inbound -Protocol ICMPv6 -IcmpType 8 -RemoteAddress <localsubnet> -Action Allow

#IPv4
netsh advfirewall firewall add rule name="ICMP Allow incoming V4 echo request" protocol="icmpv4:8,any" dir=in action=allow

#IPv6
netsh advfirewall firewall add rule name="ICMP Allow incoming V6 echo request" protocol="icmpv6:8,any" dir=in action=allow

實驗二:內部虛擬交換器

IP Address:

  • Host: 192.168.100.1, vEthernet (InternalSwitch): 192.168.200.1
  • win2019: 192.168.200.2
  • ubuntu1804: 192.168.200.3

Step 2.1. 新增內部虛擬交換器

New-VMSwitch -Name 'InternalSwitch' -SwitchType Internal

執行後會新增一個名稱為vEthernet (InternalSwitch)的網路介面卡

Step 2.2. 設定 vEthernet (InternalSwitch) 的 IP Address

New-NetIPAddress -IPAddress 192.168.200.1 -PrefixLength 24 -InterfaceAlias vEthernet (InternalSwitch)”

然後配置將在虛擬交換器上運行的NAT網路的網路地址; 這是虛擬機將在抽象虛擬交換機中使用的私有地址範圍。請注意,上一步中的IPv4地址必須在此範圍內。

New-NetNAT -Name "NATNetwork" -InternalIPInterfaceAddressPrefix 192.168.200.0/24

Step 2.3. 查看 win2019 虛擬機的網路介面卡設定,將其改成 InternalSwitch

Get-VM "win2019" | Get-VMNetworkAdapter

Get-VM "win2019" | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName "InternalSwitch"

Get-VM "win2019" | Get-VMNetworkAdapter

Step 2.4. 查看 ubuntu1804 虛擬機的網路介面卡設定,將其改成 InternalSwitch

Get-VM "ubuntu1804" | Get-VMNetworkAdapter

Get-VM "ubuntu1804" | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName "InternalSwitch"

Get-VM "ubuntu1804" | Get-VMNetworkAdapter

Step 2.5. 設定 win2019 虛擬機的網路介面卡 IP Address

   Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter
   DHCP Enabled. . . . . . . . . . . : No
   IPv4 Address. . . . . . . . . . . : 192.168.200.2
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.200.1
   DNS Servers . . . . . . . . . . . : 192.168.200.1

這裡 Gateway 和 DNS 是指定 vEthernet (InternalSwitch)的網路介面卡 IP

Step 2.6. 設定 ubuntu1804虛擬機的網路介面卡 IP Address(設定方式參考 Step 1.5)

   Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter
   DHCP Enabled. . . . . . . . . . . : No
   IPv4 Address. . . . . . . . . . . : 192.168.200.3
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.200.1
   DNS Servers . . . . . . . . . . . : 192.168.200.1

這裡 Gateway 和 DNS 是指定 vEthernet (InternalSwitch)的網路介面卡 IP

測試連線

先暫時停用 host 和 win2019 的防火牆

Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False

測試連線:win2019 ping ubuntu1804

ping 192.168.200.3

測試連線:win2019 ping host

ping 192.168.100.1

測試連線:ubuntu1804 ping win2019

ping 192.168.200.2

測試連線:ubuntu1804 ping host

ping 192.168.100.1

測試連線:host ping ubuntu1804

ping 192.168.200.3

測試連線:host ping win2019

ping 192.168.200.2

測試完,記得再啟用防火牆

Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True

實驗三:外部虛擬交換器

此範例只示範設定 win2019 使用外部虛擬交換器連線

IP Address:

  • Host: 被橋接後原設定套用到 vEthernet (ExternalSwitch), vEthernet (ExternalSwitch): 192.168.100.1
  • win2019: 192.168.100.2

Step 3.1. 新增外部虛擬交換器

New-VMSwitch -Name 'ExternalSwitch' -NetAdapterName 'Ethernet' -AllowManagementOS $true

-Name: 要新增的虛擬交換器名稱 ExternalSwitch -NetAdapterName: 透過Ethernet 的網路卡橋接連線 -AllowManagementOS: 允許

Step 3.2. 查看 win2019 虛擬機的網路介面卡設定,將其改成 ExternalSwitch

Get-VM "win2019" | Get-VMNetworkAdapter

Get-VM "win2019" | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName "ExternalSwitch"

Get-VM "win2019" | Get-VMNetworkAdapter

Step 3.2. 修改 win2019 的 IP Address

如果外部 DHCP 配發 IP ,則 win2019 虛擬機中網路介面卡的 IP 與 DNS 可自動取得。

若要指定IP,則依Host 主機的網路介面卡設定方式設定,例如:

   Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter
   DHCP Enabled. . . . . . . . . . . : No
   IPv4 Address. . . . . . . . . . . : 192.168.100.2
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.100.254
   DNS Servers . . . . . . . . . . . : 168.95.1.1

這裡的範例 DNS Servers 是指定成 Hinet 的 168.95.1.1。也可以指定成你慣用或自架的 DNS。


參考:

沒有留言:

adsense