kvm-网络操作:创建、关闭、开启、删除
1、基础知识
1.1、需求
如果我们不想用默认的网络的话,可以自己定义一个专用的网络。
1.2、命令解析
1.2.1、创建网络
临时:virsh net-create <xml配置文件>
永久:virsh net-define <xml配置文件>
1.2.2、关闭网络
virsh net-destroy <网络名称>
1.2.3、开启网络
virsh net-start <网络名称>
1.2.4、删除网络
virsh net-undefine <网络名称>
2、实战
2.1、创建临时网络
2.1.1、复制default网络配置
cd /etc/libvirt/qemu/networks && cp default.xml copy_nat.xml
2.1.2、修改copy_nat.xml配置
cat << 'CAT_END' > copy_nat.xml <network> <name>copy_nat</name> <uuid>178c4147-c955-4252-827d-202305221002</uuid> <forward mode='nat'/> <bridge name='virbr1' stp='on' delay='0'/> <mac address='52:54:00:eb:07:53'/> <ip address='192.168.123.1' netmask='255.255.255.0'> <dhcp> <range start='192.168.123.2' end='192.168.123.254'/> </dhcp> </ip> </network> CAT_END
2.1.3、创建临时网络
virsh net-create /etc/libvirt/qemu/networks/copy_nat.xml
2.1.4、查看效果
# virsh net-list --transient Name State Autostart Persistent --------------------------------------------- copy_nat active no no
2.2、创建永久网络
2.2.1、复制default网络配置
cd /etc/libvirt/qemu/networks && cp default.xml copy_nat_keep.xml
2.2.2、修改copy_nat.xml配置
cat << 'CAT_END' > copy_nat_keep.xml
<network>
<name>copy_nat_keep</name>
<uuid>178c4147-c955-4252-827d-202305221006</uuid>
<forward mode='nat'/>
<bridge name='virbr2' stp='on' delay='0'/>
<mac address='52:54:00:eb:07:54'/>
<ip address='192.168.124.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.124.2' end='192.168.124.254'/>
</dhcp>
</ip>
</network>
CAT_END
2.2.3、创建永久网络
virsh net-define /etc/libvirt/qemu/networks/copy_nat_keep.xml
2.2.4、查看效果
# virsh net-list --persistent --all Name State Autostart Persistent ---------------------------------------------------- copy_nat_keep inactive no yes default active yes yes
2.3、开启网络
# virsh net-start copy_nat_keep Network copy_nat_keep started # virsh net-list --persistent --all Name State Autostart Persistent -------------------------------------------------- copy_nat_keep active no yes default active yes yes
2.4、关闭网络
# virsh net-destroy copy_nat_keep Network copy_nat_keep destroyed # virsh net-list --persistent --all Name State Autostart Persistent ---------------------------------------------------- copy_nat_keep inactive no yes
2.5、删除网络
# 注意:删除网络需要有两步:关闭网络+取消注册 # 关闭网络 ls /etc/libvirt/qemu/networks virsh net-destroy copy_nat_keep ifconfig # 取消注册 virsh net-undefine copy_nat_keep virsh net-list --all