AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1541599
Accepted
Wlad
Wlad
Asked: 2020-04-14 09:49:51 +0800 CST2020-04-14 09:49:51 +0800 CST 2020-04-14 09:49:51 +0800 CST

如何为 Hyper-V VM(Ubuntu 19.10 快速创建)配置静态 IP 地址以避免每次重启后更新 ssh 配置?

  • 772

我正在使用 VS-Code 远程开发插件在我的笔记本电脑上的 Windows 10 上运行的 hyper-v VM(这是一个 Ubuntu 19.10 - 快速创建)上编辑代码。VM 使用 hyper-v 的默认开关进行网络连接。VS-Code 远程开发插件允许编辑 ssh 配置文件 (C:\Users\username\ssh\config),这使得连接到 VM 变得容易。这是我的 ssh 配置的样子:

Host hypervubuntu
    HostName 172.18.10.76
    User my_UbuntuVM_username

我的 hyper-v 默认开关的设置(看起来它设置为使用静态 IP,但实际上每次重新启动 Windows 后此设置都会更改): 在此处输入图像描述

问题在于,在每次 Windows 重新启动时,VM 的 IP 地址(以及 hyper-v 的默认开关)都会更改,需要编辑 ssh 配置以允许 VS-Code 再次连接到 VM。IP 的更改还会导致更多问题,例如需要重新启动 VM 并在每个新的 ssh 连接上确认“新”主机的真实性。

我尝试在 VM 的网络设置中设置静态 IP,但这似乎不会持续存在,因为它们在每次重新启动 VM 后都变回“自动 (DHCP)”。

正如在另一篇文章中所建议的那样,我尝试创建一个带有静态 IP 的新虚拟交换机,因为 Hyper-V 的默认交换机似乎并不意味着具有静态 IP。但这是我根本无法工作的地方。

即使在重新启动 Windows 或 VM 后,必须配置哪些部分才能使 VS-Code 顺利重新连接?

免责声明:我的网络技能水平 == noob :\

编辑:

在我的 Hyper-V Ubuntu 19.10 桌面虚拟机中设置静态 IP 从@AlexKrauss 得到很好的回答后,我需要执行的步骤:

一、找到并打开网络配置文件

cd /etc/netplan/
sudo nano 01-network-manager-all.yaml

二、替换它的内容如下

network:
  version: 2
  renderer: networkd  
  ethernets:
    eth0:
      addresses:
      - 192.168.0.2/24
      gateway4: 192.168.0.1
      nameservers:
        addresses:
        - 8.8.8.8
        - 8.8.4.4
      dhcp4: no

三、应用和检查的更改

sudo netplan apply
ifconfig -a

四。调整 VSCode 使用的 ssh 配置中的 IP 地址

Host hypervubuntu
    HostName 192.168.0.2
    User my_UbuntuVM_username
ssh ubuntu
  • 3 3 个回答
  • 7586 Views

3 个回答

  • Voted
  1. Best Answer
    Alex Krauss
    2020-07-12T07:20:06+08:002020-07-12T07:20:06+08:00

    我遇到了同样的问题,并找到了Microsoft 的分步指南,用于设置带有 NAT 的内部交换机,并将 VM 连接到它。

    它由 PowerShell 中的以下步骤组成

    New-VMSwitch -SwitchName "SwitchName" -SwitchType Internal
    Get-NetAdapter       // (note down ifIndex of the newly created switch as INDEX)
    New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex <INDEX>
    New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24
    

    这使用 192.168.0.0/24 作为虚拟交换机的子网,其中 192.168.0.1 是主机的 IP,充当网关。

    现在,VM 可以连接到 Hyper-V 管理器中的新交换机。

    请注意,与默认交换机不同,它没有通过 DHCP 自动配置网络,因此在 VM 内部,您必须在 VM 中配置静态 IP(例如 192.168.0.2)。

    • 13
  2. Callum
    2021-04-30T21:58:43+08:002021-04-30T21:58:43+08:00

    你现在要做的就是

    ssh <MULTPASS_VM_NAME>.mshome.net
    

    你就完成了!

    • 1
  3. Jason Song
    2021-06-03T18:07:13+08:002021-06-03T18:07:13+08:00
    # https://4sysops.com/archives/native-nat-in-windows-10-hyper-v-using-a-nat-virtual-switch/
    
    $name = "VMSwitch"
    New-VMSwitch -SwitchName $name -SwitchType Internal
    #(note down ifIndex of the newly created switch as INDEX)
    $index = (Get-NetAdapter |? Name -Like "*$name*").ifIndex
    New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex $index
    New-NetNat -Name "VM_NAT" -InternalIPInterfaceAddressPrefix 192.168.0.0/24
    
    # set switch instead of the default switch for the vm in hyyper-v
    
    # need to set IP manually on VM, because the switch do not DHCP (default switch has DHCP)
    <#
    ip:     192.168.0.2
    mask:   255.255.255.0
    gateway: 192.168.0.1
    
    dns: 8.8.8.8
    #>
    
    # get ipconfig info in vm
    get-vm work |select -ExpandProperty NetworkAdapters
    
    <# use //192.168.0.2 to access shared files from vm#>
    
    <#
    This uses 192.168.0.0/24 as the subnet for the virtual switch, where 192.168.0.1 is the IP of the host, which acts as a gateway.
    
    Now, the VM can be connected to the new switch in Hyper-V Manager.
    
    Note that unlike the Default Switch, there is no automatic network configuration via DHCP, so inside the VM, you will have to configure a static IP (e.g., 192.168.0.2) in the VM.
    #>
    
    • 0

相关问题

  • apache2 可以在没有 conf 文件的情况下工作吗?

  • vmwared 共享文件夹不工作

  • 需要 LDAP 身份验证 * 和 * ssh 身份验证

  • 加载密钥“ec256.pem”:尝试从私钥生成公钥时抛出无效格式

  • 为什么 chown 600 id_rsa 修复权限问题?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve