Hyper-V - Create a NAT Switch

  • 创建一个名为NAT Switch的交换机
$ New-VMSwitch -SwitchName "NAT Switch" -SwitchType Internal
Name       SwitchType NetAdapterInterfaceDescription
----       ---------- ------------------------------
NAT Switch Internal
  • 设置NAT Switch的IP地址
$ New-NetIPAddress -IPAddress 192.168.100.1 -PrefixLength 24 -InterfaceAlias "vEthernet (NAT Switch)" -AddressFamily IPv4

IPAddress         : 192.168.100.1
InterfaceIndex    : 76
InterfaceAlias    : vEthernet (NAT Switch)
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Tentative
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 192.168.100.1
InterfaceIndex    : 76
InterfaceAlias    : vEthernet (NAT Switch)
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Invalid
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : PersistentStore
  • 为该地址配置NAT网络
$ New-NetNAT -Name "NAT Network" -InternalIPInterfaceAddressPrefix 192.168.100.0/24

Name                             : NAT Network
ExternalIPInterfaceAddressPrefix :
InternalIPInterfaceAddressPrefix : 192.168.100.0/24
IcmpQueryTimeout                 : 30
TcpEstablishedConnectionTimeout  : 1800
TcpTransientConnectionTimeout    : 120
TcpFilteringBehavior             : AddressDependentFiltering
UdpFilteringBehavior             : AddressDependentFiltering
UdpIdleSessionTimeout            : 120
UdpInboundRefresh                : False
Store                            : Local
Active                           : True
  • 绑定虚拟机的网络至NAT Switch
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eth0:
            addresses: [192.168.100.100/24]
            dhcp4: no
            gateway4: 192.168.100.1
            nameservers:
                addresses: [114.114.114.114, 8.8.8.8, 8.8.4.4]
    version: 2
由于该交换机没有DHCP服务,所以需要手动配置IP地址
  • 应用配置
$ sudo netplan apply
  • 测试连接
$ ping www.baidu.com
PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
64 bytes from 115.239.210.27 (115.239.210.27): icmp_seq=1 ttl=54 time=5.32 ms
64 bytes from 115.239.210.27 (115.239.210.27): icmp_seq=3 ttl=54 time=4.24 ms