宿主机CentOS Linux release 7.2.1511 (Core),内核3.10.0-327.el7.x86_64
1、配置宿主机网络桥接
想让虚拟机有自己的ip且外网可访问,需要在安装虚拟机前配置宿主机网络(桥接BRIDGE):
在目录/etc/sysconfig/network-scripts下,将原始的ifcfg-enp2s0重命名ifcfg-enp2s0.old当备份,然后新建ifcfg-enp2s0内容如下:
- TYPE=Ethernet
- IPV4_FAILURE_FATAL=yes
- IPV6INIT=no
- PROXY_METHOD=none
- BROWSER_ONLY=no
- BOOTPROTO=none
- DEFROUTE=yes
- NAME=enp2s0
- DEVICE=enp2s0
- ONBOOT=yes
- BRIDGE=br0
- NM_CONTROLLED=no
再新建ifcgf-br0内容如下:
- TYPE=Bridge
- PROXY_METHOD=none
- BROWSER_ONLY=no
- BOOTPROTO=static
- DEFROUTE=yes
- IPV4_FAILURE_FATAL=no
- IPV6INIT=yes
- IPV6_AUTOCONF=yes
- IPV6_DEFROUTE=yes
- IPV6_FAILURE_FATAL=no
- IPV6_ADDR_GEN_MODE=stable-privacy
- NAME=br0
- DEVICE=br0
- NM_CONTROLLED=no
- ONBOOT=yes
- IPADDR=223.129.0.xxx
- PREFIX=25
- GATEWAY=223.129.0.xxx
- DNS1=8.8.8.8
- IPV6_PRIVACY=no
- ZONE=public
重启网络:systemctl restart network
有时候会报错:
Restarting network (via systemctl): Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
可以通过journalctl -xe查看具体原因,我这里和该文http://blog.51cto.com/addam/1839518问题类似,所以通过:systemctl stop NetworkManager,然后再重启网络解决了...
ifconfig看到如下信息(要是不对可以尝试重启机器...)
- br0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
- inet 223.129.0.xxx netmask 255.255.255.xxx broadcast 223.129.0.xxx
- inet6 fe80::4a5b:39ff:fea4:3ef0 prefixlen scopeid 0x20<link>
- ether :5b::a4:3e:f0 txqueuelen (Ethernet)
- RX packets bytes (362.0 MiB)
- RX errors dropped overruns frame
- TX packets bytes (10.3 GiB)
- TX errors dropped overruns carrier collisions
- enp2s0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
- inet6 fe80::4a5b:39ff:fea4:3ef0 prefixlen scopeid 0x20<link>
- ether :5b::a4:3e:f0 txqueuelen (Ethernet)
- RX packets bytes (755.1 MiB)
- RX errors dropped overruns frame
- TX packets bytes (10.3 GiB)
- TX errors dropped overruns carrier collisions
2、命令行安装kvm虚拟机
安装必要的软件这里就不说了...
ubuntu下(前两条用于安装,第三条用于启动):
- 1 qemu-img create -f qcow2 /home/kvm/centos7-1511.img 20G
- 2 qemu-system-x86_64 -m 2048 -enable-kvm -hda /home/kvm/centos7-1511.img -cdrom /home/isos/CentOS-7-x86_64-DVD-1511.iso -boot d
- 3 qemu-system-x86_64 -enable-kvm -m 1024 -boot menu=on /home/kvm/centos7-1511.img
centos下:
- 1 qemu-img create -f qcow2 /home/kvm/centos7-1511.img 20G
- 2 /usr/libexec/qemu-kvm -m 2048 -enable-kvm -hda /home/kvm/centos7-1708.img -cdrom /home/isos/CentOS-7-x86_64-DVD-1511.iso -boot d
- 3 /usr/libexec/qemu-kvm -enable-kvm -m 1024 -boot menu=on /home/kvm/centos7-1511.img
如果用ubuntu下的命令2在centos下运行,会出现问题:
1、bash: qemu-system-x86_64: 未找到命令...
解决:qemu-system-x86_64是在安装qemu(注意不是qemu-kvm)时生成的命令,而centos下默认安装的是qemu-kvm包,对应的命令是qemu-kvm。通过rpm -ql qemu-kvm查看输出:/usr/libexec/qemu-kvm,用此替换得到centos下正确的命令2。
正确命令2运行后,输出信息是:
2、VNC server running on `::1:5900'
解决:在xshell下运行:vncviewer :5900,会出现
....
Can't open display:
所以用vncviewer连接到目标主机,然后在图形界面下运行vncviewer :5900,即会跳出安装centos的界面,顺着安装即可(记得在SOFTWARE SELECTION选择要最小安装还是图形界面)
3、让virt-manager可以管理刚才新建的虚拟机
运行完上述的命令1和2后,如果不想每次都通过命令3启动虚拟机,可以通过libvirt管理(这样以后直接virsh start 虚拟机名,即可开启虚拟机)
libvirt(包括virsh)使用xml文件对虚拟机进行配置,其中包括虚拟机名称、分配内存、vcpu等多种信息
下面是一个名为centos7-1511.xml的xml文件,其中定义了一个名为centos7-1511的kvm虚拟机(如果是32位要写i686)
- <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
- <name>centos7-1511</name>
- <uuid>af939aef-c0eb-4b83-96ca-c23aecd3e057</uuid>
- <memory unit='KiB'>1048576</memory>
- <currentMemory unit='KiB'>1048576</currentMemory>
- <vcpu placement='static'>1</vcpu>
- <os>
- <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
- <boot dev='hd'/>
- <bootmenu enable='yes'/>
- <bios useserial='yes' rebootTimeout='0'/>
- </os>
- <features>
- <acpi/>
- <apic/>
- </features>
- <cpu mode='custom'>
- <model fallback='allow'></model>
- </cpu>
- <clock offset='utc'>
- <timer name='rtc' tickpolicy='catchup'/>
- <timer name='pit' tickpolicy='delay'/>
- <timer name='hpet' present='no'/>
- </clock>
- <on_poweroff>destroy</on_poweroff>
- <on_reboot>restart</on_reboot>
- <on_crash>restart</on_crash>
- <pm>
- <suspend-to-mem enabled='no'/>
- <suspend-to-disk enabled='no'/>
- </pm>
- <devices>
- <emulator>/usr/libexec/qemu-kvm</emulator>
- <disk type='file' device='disk'>
- <driver name='qemu' type='qcow2'/>
- <source file='/home/kvm/centos7-1511.img'/>
- <target dev='vda' bus='virtio'/>
- <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
- </disk>
- <disk type='block' device='cdrom'>
- <driver name='qemu' type='raw'/>
- <target dev='hda' bus='ide'/>
- <readonly/>
- <address type='drive' controller='0' bus='0' target='0' unit='0'/>
- </disk>
- <controller type='usb' index='0'>
- <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
- </controller>
- <controller type='pci' index='0' model='pci-root'/>
- <controller type='ide' index='0'>
- <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
- </controller>
- <interface type='network'>
- <mac address='48:5b:39:a4:3e:f1'/>
- <source network='default'/>
- <model type='e1000'/>
- <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
- </interface>
- <serial type='pty'>
- <target port='0'/>
- </serial>
- <console type='pty'>
- <target type='serial' port='0'/>
- </console>
- <input type='tablet' bus='usb'/>
- <input type='mouse' bus='ps2'/>
- <input type='keyboard' bus='ps2'/>
- <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
- <listen type='address' address='0.0.0.0'/>
- </graphics>
- <video>
- <model type='cirrus' vram='16384' heads='1'/>
- <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
- </video>
- <memballoon model='virtio'>
- <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
- </memballoon>
- </devices>
- </domain>
然后运行下面的命令即可通过virt-manager管理名为centos7-1511的kvm虚拟机
- 1 [root@localhost kvm]# virsh define centos7-1511.xml
- 定义域 centos7-1511(从 centos7-1511.xml)
- 2 [root@localhost kvm]# virsh list --all
- Id 名称 状态
- ----------------------------------------------------
- - centos7-1511 关闭
- 3 [root@localhost kvm]# virsh start centos7-1511
- 域 centos7-1511 已开始
命令3可能会有问题:
- [root@localhost kvm]# virsh start centos7-1511
- 错误:开始域 centos7-1511 失败
- 错误:internal error: process exited while connecting to monitor: qemu-kvm: -machine pc-i440fx-2.0,accel=kvm,usb=off,dump-guest-core=off: Unsupported machine type
- Use -machine help to list supported machines!
通过以下命令:
- [root@localhost kvm]# /usr/libexec/qemu-kvm -machine help
- Supported machines are:
- none empty machine
- pc RHEL 7.0.0 PC (i440FX + PIIX, 1996) (alias of pc-i440fx-rhel7.0.0)
- pc-i440fx-rhel7.0.0 RHEL 7.0.0 PC (i440FX + PIIX, 1996) (default)
- rhel6.6.0 RHEL 6.6.0 PC
- rhel6.5.0 RHEL 6.5.0 PC
- rhel6.4.0 RHEL 6.4.0 PC
- rhel6.3.0 RHEL 6.3.0 PC
- rhel6.2.0 RHEL 6.2.0 PC
- rhel6.1.0 RHEL 6.1.0 PC
- rhel6.0.0 RHEL 6.0.0 PC
将xml文件中machine部分替换,然后通过下面的undefine命令取消刚才的虚拟机,再重新运行上面的define即可
可通过:
virsh undefine centos7-1511取消定义虚拟机,之后系统无法识别centos7-1511(此步骤与virsh define centos7-1511.xml对应)
virsh destroy centos7-1511直接销毁虚拟机,取消定义,之后无法执行undefine
注意:用virt-manager第一次启动虚拟机时别忘了修改为桥接,否则即使你在虚拟机里配置了ip等信息,ping网关啥的都会出现:Destination Host Unreachable
4、虚拟机网络配置
修改网络配置文件:
1、ubuntu虚拟机中修改网络配置文件(因为有时候图形界面操作时,会出现无法save的情况)
修改/etc/network/interfaces文件为:
- # eth0是你虚拟机的网卡,可以通过ifconfig查看
- auto eth0
- iface eth0 inet static
- #ip
- address 223.129.0.xxx
- # 子网和网关
- netmask 255.255.255.xxx
- gateway 223.129.0.xxx
修改/etc/resolvconf/resolv.conf.d/base文件,设置dns
- nameserver 8.8.8.8
重启虚拟机,通过ifconfig看到信息:
- eth0 Link encap:Ethernet HWaddr :::7c:da:c9
- inet addr:223.129.0.xxx Bcast:223.129.0.xxx Mask:255.255.255.xxx
- inet6 addr: fe80:::ff:fe7c:dac9/ Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU: Metric:
- RX packets: errors: dropped: overruns: frame:
- TX packets: errors: dropped: overruns: carrier:
- collisions: txqueuelen:
- RX bytes: (31.3 MB) TX bytes: (761.3 KB)
- lo Link encap:Local Loopback
- inet addr:127.0.0.1 Mask:255.0.0.0
- inet6 addr: ::/ Scope:Host
- UP LOOPBACK RUNNING MTU: Metric:
- RX packets: errors: dropped: overruns: frame:
- TX packets: errors: dropped: overruns: carrier:
- collisions: txqueuelen:
- RX bytes: (5.6 KB) TX bytes: (5.6 KB)
此时用xshell连接,会出现connection failed
因为ubuntu虚拟机中必须安装openssh-server软件,这样才可以通过ssh连接
安装命令:
- sudo apt-get install openssh-server
- sudo /etc/init.d/ssh restart //重启ssh服务
安装好后,用以下命令查看22端口是否处于LISTEN状态,如果是,则说明已经配置好了ubuntu的ssh服务
- root@jin:/home/jin# netstat -ntlp
- Active Internet connections (only servers)
- Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
- tcp 0.0.0.0: 0.0.0.0:* LISTEN /sshd
2、centos下
这个没试,下面是通过图形界面配置后,相应文件内容
- [root@localhost network-scripts]# pwd
- /etc/sysconfig/network-scripts
- [root@localhost network-scripts]# cat ifcfg-ens3
- TYPE="Ethernet"
- BOOTPROTO="none"
- DEFROUTE="yes"
- IPV4_FAILURE_FATAL="no"
- IPV6INIT="yes"
- IPV6_AUTOCONF="yes"
- IPV6_DEFROUTE="yes"
- IPV6_FAILURE_FATAL="no"
- NAME="ens3"
- UUID="3b886635-d2a9-4569-9763-7468890a64d8"
- DEVICE="ens3"
- ONBOOT="yes"
- DNS1="8.8.8.8"
- IPADDR=223.129.0.xxx
- PREFIX=25
- GATEWAY=223.129.0.xxx
- IPV6_PEERDNS=yes
- IPV6_PEERROUTES=yes
- IPV6_PRIVACY=no
补充
1、桥接和NAT
。。。。
2、创建虚拟机时各命令含义
。。。。占坑之后写
3、libvirt
。。。