飞牛虚拟机开启NAT

-
2026-01-07

/etc/libvirt/qemu 目录中,找到虚拟机配置文件进行编辑。

原始配置

<interface type='bridge'>
  <mac address='ba:c6:8a:28:bb:f4'/>
  <source bridge='enp9s0-ovs'/>
  <virtualport type='openvswitch'>
    <parameters interfaceid='dca46eda-15a9-456e-937e-ebda1078b6ba'/>
  </virtualport>
  <model type='virtio'/>
  <address type='pci' domain='0x0000' bus='0x07' slot='0x00' function='0x0'/>
</interface>

修改步骤

1. 修改接口类型

将桥接接口改为网络接口:

- <interface type='bridge'>
+ <interface type='network'>

2. 移除桥接配置

移除指定桥接网络接口的行:

- <source bridge='enp9s0-ovs'/>

3. 移除 Open vSwitch 配置

移除虚拟端口相关配置(NAT 网络不需要):

- <virtualport type='openvswitch'>
-   <parameters interfaceid='dca46eda-15a9-456e-937e-ebda1078b6ba'/>
- </virtualport>

4. 添加 NAT 网络源

添加默认 NAT 网络配置:

+ <source network='default'/>

修改后的配置

<interface type='network'>
  <mac address='0c:a7:f1:44:57:a2'/>
  <source network='default'/>
  <model type='e1000'/>
  <address type='pci' domain='0x0000' bus='0x06' slot='0x01' function='0x0'/>
</interface>

配置文件位置

  • 虚拟机配置文件/etc/libvirt/qemu/vm_name.xml
  • 默认 NAT 网络配置文件/etc/libvirt/qemu/networks/default.xml

应用配置更改

定义编辑后的配置文件,告诉 libvirt 使用更新后的配置:

virsh define /etc/libvirt/qemu/vm_name.xml

启用 NAT 网络(飞牛OS)

检查网络状态

在飞牛OS中,默认的 NAT 网络可能未启用:

root@fnOS:/home/yourname# virsh net-list --all
 Name      State      Autostart   Persistent
----------------------------------------------
 default   inactive   no          yes

启用 NAT 网络

  1. 启动默认网络

    virsh net-start default
  2. 设置自动启动

    virsh net-autostart default

验证

执行上述命令后,再次检查网络状态:

virsh net-list --all

应该显示:

 Name      State    Autostart   Persistent
-------------------------------------------
 default   active   yes         yes

目录