1.配置桥接
1.在宿主机上,修改文件
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# mv ifcfg-ens33 ifcfg-ens33.bak [root@localhost network-scripts]# vi ifcfg-br0 YPE=Bridge NAME=br0 DEVICE=br0 ONBOOT="yes" BOOTPROTO=static IPADDR=192.168.1.101 GATEWAY=192.168.1.1 NETMASK=255.255.255.0 DNS1=8.8.8.8 [root@localhost network-scripts]# vi ifcfg-ens33 DEVICE=ens33 ONBOOT=yes BRIDGE=br0
2.重启服务
[root@localhost network-scripts]# systemctl restart network [root@localhost network-scripts]# systemctl restart libvirtd
3.虚拟机添加网卡即可
右击虚拟机-》控制台--》添加硬件--》添加网卡
2.配置NAT网络
1.修改配置文件
[root@localhost network-scripts]# cd /etc/libvirt/qemu/networks/ [root@qfedu.com ~]# vim /etc/libvirt/qemu/networks/nat3.xml <network> <name>nat3</name> <uuid>4d8b9b5c-748f-4e16-a509-848202b9c83b</uuid> <forward mode='nat'/> //和隔离模式的区别 <bridge name='virbr4' stp='on' delay='0'/> <mac address='52:57:00:62:0c:d4'/> <domain name='nat3'/> <ip address='192.168.104.1' netmask='255.255.255.0'> <dhcp> <range start='192.168.104.128' end='192.168.104.254'/> </dhcp> </ip> </network> 重启服务: [root@qfedu.com ~]# systemctl restart libvirtd
3.配置isolated网络
1.配置文件
<network> <name>isolate1</name> <uuid>6341d3a6-7330-4e45-a8fe-164a6a68929a</uuid> <bridge name='virbr2' stp='on' delay='0'/> <mac address='52:54:00:6b:39:0c'/> <domain name='isolate1'/> <ip address='192.168.101.1' netmask='255.255.255.0'> <dhcp> <range start='192.168.101.128' end='192.168.101.254'/> </dhcp> </ip> </network>
2.其他网络管理命令
查看所有的⽹络: [root@qfedu.com ~]# virsh net-list 启动⽹络: [root@qfedu.com ~]# virsh net-start isolated200 开机⾃启动: [root@qfedu.com ~]# virsh net-autostart isolated200 ⽹络相关基本命令 查看⼀个guest主机的⽹络接⼝信息: [root@qfedu.com ~]# virsh domiflist vm1 接⼝ 类型 源 型号 MAC --------------------------------------- vnet0 network default virtio 52:54:00:94:a7:a1
4.脚本
#!/bin/bash #KVM batch create vm tool #version: 0.1 #author: wing #需要事先准备模板镜像和配置⽂件模板 echo "1.创建⾃定义配置单个虚拟机 2.批量创建⾃定义配置虚拟机 3.批量创建默认配置虚拟机 4.删除虚拟机" #扩展功能: [root@qfedu.com ~]# 查看现在虚拟机 [root@qfedu.com ~]# 查看某个虚拟机的配置 [root@qfedu.com ~]# 升配/降配 [root@qfedu.com ~]# 添加/删除⽹络 read -p "选取你的操作(1/2/3):" op batch_self_define() { KVMname=`openssl rand -hex 5` sourceimage=/var/lib/libvirt/images/vmmodel.img sourcexml=/etc/libvirt/qemu/vmmodel.xml newimg=/var/lib/libvirt/images/${KVMname}.img newxml=/etc/libvirt/qemu/${KVMname}.xml cp $sourceimage $newimg cp $sourcexml $newxml KVMuuid=`uuidgen` KVMmem=${1}000000 KVMcpu=$2 KVMimg=$newimg KVMmac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'` sed -i "s@KVMname@$KVMname@;s@KVMuuid@$KVMuuid@;s@KVMmem@$KVMmem@;s@KVMcpu@$KVMcpu @;s@KVMimg@$KVMimg@;s@KVMmac@$KVMmac@" $newxml virsh define $newxml virsh list --all } self_define() { read -p "请输⼊新虚机名称:" newname read -p "请输⼊新虚机内存⼤⼩(G):" newmem read -p "请输⼊新虚机cpu个数:" newcpu sourceimage=/var/lib/libvirt/images/vmmodel.img sourcexml=/etc/libvirt/qemu/vmmodel.xml newimg=/var/lib/libvirt/images/${newname}.img newxml=/etc/libvirt/qemu/${newname}.xml cp $sourceimage $newimg cp $sourcexml $newxml KVMname=$newname KVMuuid=`uuidgen` KVMmem=${newmem}000000 KVMcpu=$newcpu KVMimg=$newimg KVMmac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'` sed -i "s@KVMname@$KVMname@;s@KVMuuid@$KVMuuid@;s@KVMmem@$KVMmem@;s@KVMcpu@$KVMcpu @;s@KVMimg@$KVMimg@;s@KVMmac@$KVMmac@" $newxml virsh define $newxml virsh list --all } case $op in 1)self_define;; 2) read -p "请输⼊要创建的虚拟机的个数:" num read -p "请输⼊新虚机内存⼤⼩(G):" newmem read -p "请输⼊新虚机cpu个数:" newcpu for((i=1;i<=$num;i++)) do batch_self_define $newmem $newcpu done;; 3) read -p "请输⼊要创建的虚拟机的个数:" num for((i=1;i<=$num;i++)) do batch_self_define 1 1 done;; *) echo "输⼊错误,请重新执⾏脚本" exit;; esac