一、配置KVM虚拟机NAT网络
1、创建脚本执行权限
下面是NAT启动脚本
# vi /etc/qemu-ifup-NAT
赋予权限
# chmod +x /etc/qemu-ifup-NAT
下载镜像文件
cirror project:为cliud环境测试vm提供的微缩版linux
镜像名称: cirros-0.3.4-x86_64-disk.img
镜像下载地址:http://download.cirros-cloud.net/0.3.4/
把镜像放到默认位置
# wget http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img
# mv cirros-0.3.4-x86_64-disk.img /var/lib/libvirt/images/
通过命令启动虚拟机
# qemu-kvm -m 1024 -drive file=cirros-0.3.4-x86_64-disk.img,if=virtio -net nic,model=virtio -net tap,script=/etc/qemu-ifup-NAT -nographic -vnc :2
实验失败,创建的虚拟机登录不进去,启动的时候报错
二、配置KVM虚拟机Bridge网络
1、安装工具
安装brctl和tunctl命令行工具
# yum install bridge-utils tunctl -y
查看tun模块是否加载
[root@localhost ~]# lsmod | grep tuntun 17095 2 vhost_net
2、创建Bridge
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:2a:09:c7 brd ff:ff:ff:ff:ff:ff inet 192.168.1.55/24 brd 192.168.1.255 scope global eth0 inet6 fe80::20c:29ff:fe2a:9c7/64 scope link valid_lft forever preferred_lft forever3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether 52:54:00:dc:9c:cd brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr04: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500 link/ether 52:54:00:dc:9c:cd brd ff:ff:ff:ff:ff:ff[root@localhost ~]# [root@localhost ~]# [root@localhost ~]# brctl addbr br0[root@localhost ~]# [root@localhost ~]# brctl addif br0 eth0Xshell断开连接,在主机内部进行操作
网卡配置分别配置如下
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0TYPE=EthernetONBOOT=yesNM_CONTROLLED=noBOOTPROTO=noneDRIDGE=br0[root@localhost ~]# [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0TYPE=bridgeONBOOT=yesNM_CONTROLLED=noBOOTPROTO=staticIPADDR=192.168.1.55NETMASK=255.255.255.0GATEWAY=192.168.1.1DNS1=8.8.8.8
重启网卡配置生效
[root@localhost ~]# service network restartShutting down interface eth0: [ OK ]Shutting down loopback interface: [ OK ]Bringing up loopback interface: [ OK ]Bringing up interface br0: Determining if ip address 192.168.1.55 is already in use for device br0... [ OK ]Bringing up interface eth0: [ OK ]
Xshell连接
查看网络信息
[root@localhost ~]# ip addr list1: lo:mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:2a:09:c7 brd ff:ff:ff:ff:ff:ff inet6 fe80::20c:29ff:fe2a:9c7/64 scope link valid_lft forever preferred_lft forever3: virbr0: mtu 1500 qdisc noqueue state UNKNOWN link/ether 52:54:00:dc:9c:cd brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr04: virbr0-nic: mtu 1500 qdisc noop state DOWN qlen 500 link/ether 52:54:00:dc:9c:cd brd ff:ff:ff:ff:ff:ff6: br0: mtu 1500 qdisc noqueue state UNKNOWN link/ether 00:0c:29:2a:09:c7 brd ff:ff:ff:ff:ff:ff inet 192.168.1.55/24 brd 192.168.1.255 scope global br0 inet6 fe80::20c:29ff:fe2a:9c7/64 scope link valid_lft forever preferred_lft forever
查看路由表
[root@localhost ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 br0192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 1006 0 0 br00.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 br0
查看桥接信息
[root@localhost ~]# brctl showbridge name bridge id STP enabled interfacesbr0 8000.000c292a09c7 no eth0virbr0 8000.525400dc9ccd yes virbr0-nic
查看br0桥
[root@localhost ~]# brctl show br0bridge name bridge id STP enabled interfacesbr0 8000.000c292a09c7 no eth0