我有一台 Windows 主机,我想将客户端虚拟机的 IP 绑定到主机的 127.0.0.2 地址

我有一台 Windows 主机,我想将客户端虚拟机的 IP 绑定到主机的 127.0.0.2 地址

我有一台 Windows 10 电脑。我添加了一个客户端 Linux 虚拟机,我希望能够从主机连接到 127.0.0.2:22 处的客户端。我需要使用 127.0.0.* 范围,因为我的 VPN 在激活时禁用了对 192.* 和 10.* 的访问。

我安装了 Windows 环回适配器,并已确定与环回网络相对应的网络。但我不知道如何配置环回网络。我想我会禁用除 IPv4 项之外的所有项,但我无法明确绑定到 127。

答案1

解决方案可能是创建一个不绑定的备用环回接口127.x.x.x,并为其提供 VPN 不会阻止的 IP 地址(如果存在)。

请注意,两个环回接口在相互通信时可能会出现问题。

有关使用 PowerShell 执行此操作的说明,请参阅文章
使用 PowerShell 创建环回接口

链接的文章很好地解释了这个过程。我只在下面包含了简单的 PowerShell 命令。将 IP 地址替换为未被阻止的 IP 地址:

# The name for the loopback adapter interface that will be created.
$loopback_name = 'Loopback'

# The name for the servers main network interface. This will be updated to allow weak host send/recieve which is most likely required for the traffic to work for the loopback interface.
$primary_interface = 'Ethernet'

# The IPv4 address that you would like to assign to the loopback interface along with the prefix length (eg. if the IP is routed to the server usually you would set the prefix length to 32).
$loopback_ipv4 = '10.254.1.3'
$loopback_ipv4_length = '32'

# The IPv6 address that you would like to assign to the loopback interface along with the prefix length (eg. if the IP is routed to the server usually you would set the prefix length to 128). If you are not adding an IPv6 address do not set these variables.
# $loopback_ipv6 = 'fffa::1'
# $loopback_ipv6_length = '128'

Install-Module -Name LoopbackAdapter -MinimumVersion 1.2.0.0 -Force
Import-Module -Name LoopbackAdapter

New-LoopbackAdapter -Name $loopback_name -Force

$interface_loopback = Get-NetAdapter -Name $loopback_name
$interface_main = Get-NetAdapter -Name $primary_interface

Set-NetIPInterface -InterfaceIndex $interface_loopback.ifIndex -InterfaceMetric "254" -WeakHostReceive Enabled -WeakHostSend Enabled -DHCP Disabled
Set-NetIPInterface -InterfaceIndex $interface_main.ifIndex -WeakHostReceive Enabled -WeakHostSend Enabled
Set-NetIPAddress -InterfaceIndex $interface_loopback.ifIndex -SkipAsSource $True

Get-NetAdapter $loopback_name | Set-DNSClient –RegisterThisConnectionsAddress $False

# Set the IPv4 address
New-NetIPAddress -InterfaceAlias $loopback_name -IPAddress $loopback_ipv4 -PrefixLength $loopback_ipv4_length -AddressFamily ipv4

# Set the IPv6 address - Uncomment this if required
# New-NetIPAddress -InterfaceAlias $loopback_name -IPAddress $loopback_ipv6 -PrefixLength $loopback_ipv6_length -AddressFamily ipv6

Disable-NetAdapterBinding -Name $loopback_name -ComponentID ms_msclient
Disable-NetAdapterBinding -Name $loopback_name -ComponentID ms_pacer
Disable-NetAdapterBinding -Name $loopback_name -ComponentID ms_server
Disable-NetAdapterBinding -Name $loopback_name -ComponentID ms_lltdio
Disable-NetAdapterBinding -Name $loopback_name -ComponentID ms_rspndr

答案2

不要这样做。

在几乎所有系统上,127.0.0.0/8 网络都连接到环回接口。连接到此接口的唯一系统是系统本身。而不是虚拟机。您的 Linux 虚拟机有自己的环回接口,有自己的 127.0.0.0/8 网络。这两个网络彼此不连接。

尽管可以将 127.0.0.0/8 网络放在环回接口以外的其他接口上,但某些软件(例如 VirtualBox)会中断。您必须非常清楚自己在做什么。请注意,仅在 Windows 主机上执行此操作是不够的;您还必须在 Linux 虚拟机上执行此操作。

相反,尝试使用子网 100.64.0.0/10 或 172.16.0.0/12,正如用户 1686 在评论中所建议的那样。

相关内容