gnome_boxs配置端口转发

总结摘要
gnome_boxs配置端口转发

gnome boxs 虚拟机网络默认采用 nat 模式,如果宿主机需要访问虚拟机的端口,有两种方式,一种是端口转发,一种是采用网桥模式

端口转发

  • 需要安装 passt 工具
1
sudo pacman -S passt
  • 需要在 首选项 - 资源 - 配置 里,编辑xml配置

之前配置如下:

1
2
3
4
5
<interface type="user">
  <mac address="52:54:00:ce:6b:1d"/>
  <model type="virtio"/>
  <address type="pci" domain="0x0000" bus="0x01" slot="0x00" function="0x0"/>
</interface>

修改后配置如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<interface type="user">
  <mac address="52:54:00:ce:6b:1d"/>
  <model type="virtio"/>
  <address type="pci" domain="0x0000" bus="0x01" slot="0x00" function="0x0"/>
  <!-- 新增配置 -->
  <backend type='passt'/>
  <portForward proto='tcp'>
    <range start='4022' to='22'/>
  </portForward>
</interface>
  • 重启虚拟机即可

网桥模式

创建网桥

使用 virsh 创建网桥

  • 创建一个名为vbr的虚拟网络

vim /etc/libvirt/qemu/networks/vbr.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<network>
  <!-- #vbr为虚拟网络的名字 -->
  <name>vbr</name>
  <bridge name="vbr"/>
  <forward mode="nat"/>
  <!-- #ip为192.168.1.254,相当于虚拟机网关 -->
  <ip address="192.168.1.254" netmask="255.255.255.0">
    <!-- DHCP自动分配ip范围是100-200 -->
    <dhcp>
      <range start="192.168.1.100" end="192.168.1.200"/>
    </dhcp>
  </ip>
</network>
  • 启动vbr虚拟网络并用ifconfig验证
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 定义vbr虚拟网络
virsh net-define /etc/libvirt/qemu/networks/vbr.xml
# 启动vbr虚拟网络
virsh net-start vbr
# 设置vbr虚拟网络开机自启动
virsh net-autostart vbr
# 列出虚拟网络
virsh net-list --all
# 开启libvirtd服务
systemctl start libvirtd
# 设置libvirtd服务自启动
systemctl start libvirtd
# 查看虚拟网络是否生效
ifconfg

配置虚拟机

  • 需要在 首选项 - 资源 - 配置 里,编辑xml配置

之前配置如下:

1
2
3
4
5
<interface type="user">
  <mac address="52:54:00:ce:6b:1d"/>
  <model type="virtio"/>
  <address type="pci" domain="0x0000" bus="0x01" slot="0x00" function="0x0"/>
</interface>

修改后配置如下:

1
2
3
4
5
6
<interface type="bridge">
  <source bridge="vbr"/>
  <mac address="52:54:00:ce:6b:1d"/>
  <model type="virtio"/>
  <address type="pci" domain="0x0000" bus="0x01" slot="0x00" function="0x0"/>
</interface>
  • 重启虚拟机即可

参考