当前位置: 首页 >> 虚拟化 >> 【设置】为KVM虚拟机增加网卡 >> 正文

【设置】为KVM虚拟机增加网卡

2021年02月18日 17:29:04    作者:Jiaozn    分类:虚拟化     评论(0)

用virt install 完成虚拟机的安装后,发现里面只有一块网卡,有些时候我们需要两块或多块网卡怎么办? 可以通过virsh attach-iterface来实现

例如原来的ubuntu虚拟机是这样的, 只有一块网卡:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
test@test-Lenovo-Product:~$ virsh domiflist ubuntu1604-test-vm1
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     virbr0     virtio      52:54:00:34:06:26
 
test@ubuntu1604-test-vm1:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:34:06:26 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global ens3
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe34:626/64 scope link
       valid_lft forever preferred_lft forever

添加网卡有两种途径:

1 动态的增加网卡,当时生效,但是虚拟机重启以后就没有了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
test@test-Lenovo-Product:~$ virsh attach-interface ubuntu1604-test-vm1 --type bridge --source virbr0
Interface attached successfully
 
test@test-Lenovo-Product:~$ virsh domiflist
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     virbr0     virtio      52:54:00:34:06:26
vnet1      bridge     virbr0     rtl8139     52:54:00:9c:c4:dd
 
test@ubuntu1604-test-vm1:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:34:06:26 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global ens3
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe34:626/64 scope link
       valid_lft forever preferred_lft forever
3: ens7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 52:54:00:9c:c4:dd brd ff:ff:ff:ff:ff:ff

 

2 方式二,通过修改配置文件的方式永久的增加网卡

1
2
3
4
5
6
7
test@test-Lenovo-Product:~$ virsh attach-interface ubuntu1604-test-vm1 --type bridge --source virbr0 --config
Interface attached successfully
 
test@test-Lenovo-Product:~$ virsh domiflist ubuntu1604-test-vm1
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     virbr0     virtio      52:54:00:34:06:26

 

虽然这时没有virsh domiflist没有列出来新增加的interface,登陆进虚拟机也没看到ip, 但是 virsh edit ubuntu1604-test-vm1 可以看到多了一个网卡

1
2
3
4
5
6
7
8
9
10
11
12
<interface type='bridge'>
      <mac address='52:54:00:34:06:26'/>
      <source bridge='virbr0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <interface type='bridge'>
      <mac address='52:54:00:ab:2d:39'/>
      <source bridge='virbr0'/>
      <model type='rtl8139'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </interface>

经过重启后,可以看到网卡已被永久加上:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
virsh shutdown ubuntu1604-test-vm1
 
virsh start domiflist ubuntu1604-test-vm1
 
test@test-Lenovo-Product:~$ virsh domiflist ubuntu1604-test-vm1
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     virbr0     virtio      52:54:00:34:06:26
vnet1      bridge     virbr0     rtl8139     52:54:00:ab:2d:39
 
test@ubuntu1604-test-vm1:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:34:06:26 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global ens3
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe34:626/64 scope link
       valid_lft forever preferred_lft forever
3: ens7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 52:54:00:ab:2d:39 brd ff:ff:ff:ff:ff:ff
  

3 如果网卡不用了想删掉,就用下面命令, 重启后就可生效

1
2
virsh detach-interface ubuntu1604-test-vm1 --type bridge --mac 52:54:00:ab:2d:39 --config
Interface detached successfully


除非注明,发表在“Jiaozn的博客”的文章『【设置】为KVM虚拟机增加网卡』版权归Jiaozn所有。 转载请注明出处为“本文转载于『Jiaozn的博客』原地址https://jiaozn.com/reed/635.html

评论

发表评论   

昵称*

E-mail*

网站