首先virsh和brctl都是需要安装libvirt才有这两个命令
yum install libvirt -y
systemctl start libvirtd
[root@init ~]# cat testxml
<network>
<name>test</name> #这里修改
<uuid>168f6909-715c-4333-a34b-f74584d26328</uuid> #这里修改
<forward mode='nat'/>
<bridge name='virbr1' stp='on' delay='0'/> #这里修改
<mac address='52:54:00:48:3f:0c'/> #这里修改
<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>
[root@init ~]#
[root@init ~]# virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
[root@init ~]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.525400e93e9c yes virbr0-nic
[root@init ~]# virsh net-list -- #自动补全,看到如下的参数,安装了bash-completion和bash-completion-extras的用途
--all --inactive --no-autostart --table --uuid
--autostart --name --persistent --transient
[root@init ~]#
[root@init ~]# virsh net-define testxml
Network test defined from testxml
[root@init ~]# virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
[root@init ~]# virsh net-list --all
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
test inactive no yes
[root@init ~]# virsh net-autostart test
Network test marked as autostarted
[root@init ~]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.525400e93e9c yes virbr0-nic
[root@init ~]# virsh net-start test
Network test started
[root@init ~]# virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
test active yes yes
[root@init ~]# brctl show #可以看到第二个nat已经完成
bridge name bridge id STP enabled interfaces
virbr0 8000.525400e93e9c yes virbr0-nic
virbr1 8000.525400483f0c yes virbr1-nic
[root@init ~]# ip -4 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.0.32/24 brd 192.168.0.255 scope global noprefixroute dynamic ens33
valid_lft 5696sec preferred_lft 5696sec
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
6: virbr1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
inet 192.168.123.1/24 brd 192.168.123.255 scope global virbr1
valid_lft forever preferred_lft forever
[root@init ~]#
参考
- 建立nat参考:https://kashyapc.fedorapeople.org/virt/create-a-new-libvirt-bridge.txt
- 桥接和nat参考:https://linuxconfig.org/how-to-use-bridged-networking-with-libvirt-and-kvm
版本
- 2022-11-21 第一版