為CentOS虛擬機器新增第二塊網絡卡
阿新 • • 發佈:2019-01-22
使用virt-install安裝了一個CentOS虛擬機器,安裝過程未指定網路引數,預設使用內建虛擬NAT的方式上網,現在希望給虛擬機器另外安裝一塊虛擬的網絡卡,然後給其分配一個外網IP地址(與宿主機處於同一網段)。
安裝CentOS
$ sudo virt-install --connect qemu:///system --noautoconsole \ -n centos -r 4096 --vcpus=4 --arch=x86_64 --os-type=linux \ --os-variant=rhel6 --accelerate -v -l http://mirrors.163.com/centos/6.3/os/x86_64/ \ --disk path=/var/lib/libvirt/images/centos.img,size=200 \ --graphics vnc,password=foobar,listen=0.0.0.0,port=5901
為宿主機新增虛擬網橋
$ vim /etc/network/interfaces
修改如下
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface iface em1 inet manual auto br0 iface br0 inet static bridge_ports em1 bridge_stp on bridge_maxwait 0 bridge_fd 0 address 192.168.18.130 netmask 255.255.255.0 network 192.168.18.0 broadcast 192.168.18.255 gateway 192.168.18.1 dns-nameservers 192.168.18.1
新增虛擬網橋配置br0,並且把主網絡卡em1(大多數情況下主網絡卡為eth0)配置由dhcp或者static改成manual。
然後重啟網路服務
$ sudo service networking restart
為虛擬機器新增一張額外的虛擬網絡卡
首先關閉VM
$ virsh destory centos
然後編輯VM配置檔案
$ virsh edit centos
在之後加入如下xml片段
<interface type='bridge'> <mac address='00:16:3e:1a:b3:4a'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </interface>
然後儲存退出。
啟動剛才修改過的VM虛擬節點
$ virsh start centos
為CentOS新增新網絡卡配置
參看/sys/class/net
可以看到新新增的網絡卡eth1
# ls /sys/class/net/
eth0 eth1 lo
通過ifconfig獲得該網絡卡的MAC地址
# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:16:3E:1A:B3:4A
inet addr:192.168.18.210 Bcast:192.168.18.255 Mask:255.255.255.0
inet6 addr: fe80::216:3eff:fe1a:b34a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3343 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:216373 (211.3 KiB) TX bytes:1364 (1.3 KiB)
執行uuidgen生成一個隨機的UUID
# uuidgen
e5281311-f265-4afa-a7e1-4d76718f7de0
然後完事具備只欠東方了,
# cd /etc/sysconfig/network-scripts
# cp ifcfg-eth0 ifcfg-eth1
# vim ifcfg-eth1
結合上面的MAC地址和UUID,修改內容如下
DEVICE="eth1"
BOOTPROTO="dhcp"
HWADDR="00:16:3E:1A:B3:4A"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
UUID="e5281311-f265-4afa-a7e1-4d76718f7de0"