Hyper-v 默认交换机静态 IP

Vla*_*mir 15 networking hyper-v

是否可以为 Hyper-V 的默认交换机适配器设置静态 IP 地址?每次我重新启动我的电脑时,地址都会改变。

har*_*ymc 7

您可以 通过右键单击 vEthernet 开关在控制面板 > 网络连接 > 更改适配器设置中设置静态地址 ,但 Windows 会在重新启动后将其重置为随机地址,并且无法禁用该操作。

一种解决方案是使用netsh 命令 或其 PowerShell 替代方法将其始终重置为相同的值 。Netsh 需要以管理员身份运行。

命令语法为:

netsh interface ip set address [name=]InterfaceName [source=]{dhcp | static [addr=]IPAddress[mask=]SubnetMask [gateway=]{none | DefaultGateway [[gwmetric=]GatewayMetric]}}
Run Code Online (Sandbox Code Playgroud)

将交换机设置为静态 IP of 192.168.100.1, mask255.255.255.0和 gateway none的示例命令是:

netsh interface ip set address name="vEthernet (Default Switch)" static 192.168.100.1 255.255.255.0 none
Run Code Online (Sandbox Code Playgroud)

如有必要,您可以设置一个包含在 Windows 启动或用户登录时运行的命令的脚本。

另一种解决方案是创建一个新的交换机,与默认交换机不同,它的 IP 地址将保留。


Axe*_*lly 6

我知道这可能有点偏离主题,但由于我们设置此 IP 是为了通过 SSH 连接到我们的 Hyper-V 实例,因此可以分配静态 MAC 地址,然后编写一些"self-discovery"脚本,因此我们不必assign ip to vEthernet每次都使用

分配静态MAC:

右键单击 Hyper-V 实例 -> 设置 > 网卡 > 高级功能 -> 选择静态 MAC 和填充 MAC

在此输入图像描述

就我而言,它是 powershell 脚本,它根据静态 MAC 从 arp 发现中提取 IP,然后通过 SSH 连接到它

$str = ((arp -a | findstr /i 00-15-5D-01-83-0B)[0]); 
$ip = $str.Split(" ")[2].Trim()
ssh root@$ip
Run Code Online (Sandbox Code Playgroud)

说明:

ARP

Displays and modifies the IP-to-Physical address translation tables used by
address resolution protocol (ARP).

ARP -s inet_addr eth_addr [if_addr]
ARP -d inet_addr [if_addr]
ARP -a [inet_addr] [-N if_addr] [-v]

  -a            Displays current ARP entries by interrogating the current
                protocol data.  If inet_addr is specified, the IP and Physical
                addresses for only the specified computer are displayed.  If
                more than one network
Run Code Online (Sandbox Code Playgroud)

arp -a | 查找str /i 00-15-5D-01-83-0B

/i= 忽略大小写

返回:

 192.168.1.31          00-15-5d-01-83-0b     dynamic
 192.168.43.170        00-15-5d-01-83-0b     static
Run Code Online (Sandbox Code Playgroud)

[0] 索引

精选:

192.168.1.31          00-15-5d-01-83-0b     dynamic
Run Code Online (Sandbox Code Playgroud)

$str.Split(" ")[2].Trim()

返回:

192.168.1.31
Run Code Online (Sandbox Code Playgroud)

然后 ssh 连接到它