1、基于NAT的虚拟网络
连接虚拟机,查看虚拟机ip地址信息,并测试是否可以连接外网:
[root@localhost ~]# virsh console test-os1
查看虚拟机的配置文件:
[root@localhost ~]# vim /etc/libvirt/qemu/test-os1.xml
查看default.xml配置文件:
[root@localhost ~]# vim /etc/libvirt/qemu/networks/default.xml
2、基于网桥的虚拟网络
拷贝一个配置文件作为虚拟交换机(网桥),并编辑其配置文件:
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-virbr1
[root@localhost network-scripts]# mkdir ifcfgback
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfgback/
[root@localhost network-scripts]# vim ifcfg-virbr1
BRIDGE=virbr1 网桥名
TYPE=Bridge 类型为网桥
DEVICE=virbr1 设备名字
BOOTPROTO=static
NAME=virbr1
ONBOOT=yes
IPADDR=192.168.2.100
GATEWAY=192.168.2.2
DNS1=114.114.114.114
DNS2=8.8.8.8
再次编辑本地的网卡文件:
[root@localhost network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=virbr1 网桥名
检查网卡配置文件与网桥配置文件是否配置无误:
[root@localhost network-scripts]# cat ifcfg-ens33
[root@localhost network-scripts]# cat ifcfg-virbr1
重启network服务,并查看ip地址信息:
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ip a
查看虚拟机的网卡信息:
[root@localhost network-scripts]# virsh domifaddr test-os1
[root@localhost network-scripts]# virsh domiflist test-os1
[root@localhost network-scripts]# virsh domifstat test-os1 vnet0
在虚拟机中添加一个网卡,并查看虚拟机的ip地址信息:
[root@localhost network-scripts]# virsh attach-interface test-os1 --type bridge --source virbr1
[root@localhost network-scripts]# virsh attach-interface test-os1 --type bridge --source virbr1 --config
在虚拟机搭建httpd服务,然后在外网访问查看是否成功:
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl restart network