宿主机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内容如下:

  1. TYPE=Ethernet
  2. IPV4_FAILURE_FATAL=yes
  3. IPV6INIT=no
  4. PROXY_METHOD=none
  5. BROWSER_ONLY=no
  6. BOOTPROTO=none
  7. DEFROUTE=yes
  8. NAME=enp2s0
  9. DEVICE=enp2s0
  10. ONBOOT=yes
  11. BRIDGE=br0
  12. NM_CONTROLLED=no

再新建ifcgf-br0内容如下:

  1. TYPE=Bridge
  2. PROXY_METHOD=none
  3. BROWSER_ONLY=no
  4. BOOTPROTO=static
  5. DEFROUTE=yes
  6. IPV4_FAILURE_FATAL=no
  7. IPV6INIT=yes
  8. IPV6_AUTOCONF=yes
  9. IPV6_DEFROUTE=yes
  10. IPV6_FAILURE_FATAL=no
  11. IPV6_ADDR_GEN_MODE=stable-privacy
  12. NAME=br0
  13. DEVICE=br0
  14. NM_CONTROLLED=no
  15. ONBOOT=yes
  16. IPADDR=223.129.0.xxx
  17. PREFIX=25
  18. GATEWAY=223.129.0.xxx
  19. DNS1=8.8.8.8
  20. IPV6_PRIVACY=no
  21. 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看到如下信息(要是不对可以尝试重启机器...)

  1. br0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
  2. inet 223.129.0.xxx netmask 255.255.255.xxx broadcast 223.129.0.xxx
  3. inet6 fe80::4a5b:39ff:fea4:3ef0 prefixlen scopeid 0x20<link>
  4. ether :5b::a4:3e:f0 txqueuelen (Ethernet)
  5. RX packets bytes (362.0 MiB)
  6. RX errors dropped overruns frame
  7. TX packets bytes (10.3 GiB)
  8. TX errors dropped overruns carrier collisions
  9. enp2s0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
  10. inet6 fe80::4a5b:39ff:fea4:3ef0 prefixlen scopeid 0x20<link>
  11. ether :5b::a4:3e:f0 txqueuelen (Ethernet)
  12. RX packets bytes (755.1 MiB)
  13. RX errors dropped overruns frame
  14. TX packets bytes (10.3 GiB)
  15. TX errors dropped overruns carrier collisions  

2、命令行安装kvm虚拟机

安装必要的软件这里就不说了...

ubuntu下(前两条用于安装,第三条用于启动):

  1. 1 qemu-img create -f qcow2 /home/kvm/centos7-1511.img 20G
  2. 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. 3 qemu-system-x86_64 -enable-kvm -m 1024 -boot menu=on /home/kvm/centos7-1511.img

centos下:

  1. 1 qemu-img create -f qcow2 /home/kvm/centos7-1511.img 20G
  2. 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. 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)

  1. <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
  2. <name>centos7-1511</name>
  3. <uuid>af939aef-c0eb-4b83-96ca-c23aecd3e057</uuid>
  4. <memory unit='KiB'>1048576</memory>
  5. <currentMemory unit='KiB'>1048576</currentMemory>
  6. <vcpu placement='static'>1</vcpu>
  7. <os>
  8. <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
  9. <boot dev='hd'/>
  10. <bootmenu enable='yes'/>
  11. <bios useserial='yes' rebootTimeout='0'/>
  12. </os>
  13. <features>
  14. <acpi/>
  15. <apic/>
  16. </features>
  17. <cpu mode='custom'>
  18. <model fallback='allow'></model>
  19. </cpu>
  20. <clock offset='utc'>
  21. <timer name='rtc' tickpolicy='catchup'/>
  22. <timer name='pit' tickpolicy='delay'/>
  23. <timer name='hpet' present='no'/>
  24. </clock>
  25. <on_poweroff>destroy</on_poweroff>
  26. <on_reboot>restart</on_reboot>
  27. <on_crash>restart</on_crash>
  28. <pm>
  29. <suspend-to-mem enabled='no'/>
  30. <suspend-to-disk enabled='no'/>
  31. </pm>
  32. <devices>
  33. <emulator>/usr/libexec/qemu-kvm</emulator>
  34. <disk type='file' device='disk'>
  35. <driver name='qemu' type='qcow2'/>
  36. <source file='/home/kvm/centos7-1511.img'/>
  37. <target dev='vda' bus='virtio'/>
  38. <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
  39. </disk>
  40. <disk type='block' device='cdrom'>
  41. <driver name='qemu' type='raw'/>
  42. <target dev='hda' bus='ide'/>
  43. <readonly/>
  44. <address type='drive' controller='0' bus='0' target='0' unit='0'/>
  45. </disk>
  46. <controller type='usb' index='0'>
  47. <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
  48. </controller>
  49. <controller type='pci' index='0' model='pci-root'/>
  50. <controller type='ide' index='0'>
  51. <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
  52. </controller>
  53. <interface type='network'>
  54. <mac address='48:5b:39:a4:3e:f1'/>
  55. <source network='default'/>
  56. <model type='e1000'/>
  57. <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
  58. </interface>
  59. <serial type='pty'>
  60. <target port='0'/>
  61. </serial>
  62. <console type='pty'>
  63. <target type='serial' port='0'/>
  64. </console>
  65. <input type='tablet' bus='usb'/>
  66. <input type='mouse' bus='ps2'/>
  67. <input type='keyboard' bus='ps2'/>
  68. <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
  69. <listen type='address' address='0.0.0.0'/>
  70. </graphics>
  71. <video>
  72. <model type='cirrus' vram='16384' heads='1'/>
  73. <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
  74. </video>
  75. <memballoon model='virtio'>
  76. <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
  77. </memballoon>
  78. </devices>
  79. </domain>

然后运行下面的命令即可通过virt-manager管理名为centos7-1511的kvm虚拟机

  1. 1 [root@localhost kvm]# virsh define centos7-1511.xml
  2. 定义域 centos7-1511(从 centos7-1511.xml
  3. 2 [root@localhost kvm]# virsh list --all
  4. Id 名称 状态
  5. ----------------------------------------------------
  6. - centos7-1511 关闭
  7. 3 [root@localhost kvm]# virsh start centos7-1511
  8. centos7-1511 已开始

命令3可能会有问题:

  1. [root@localhost kvm]# virsh start centos7-1511
  2. 错误:开始域 centos7-1511 失败
  3. 错误: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
  4. Use -machine help to list supported machines!

通过以下命令:

  1. [root@localhost kvm]# /usr/libexec/qemu-kvm -machine help
  2. Supported machines are:
  3. none empty machine
  4. pc RHEL 7.0.0 PC (i440FX + PIIX, 1996) (alias of pc-i440fx-rhel7.0.0)
  5. pc-i440fx-rhel7.0.0 RHEL 7.0.0 PC (i440FX + PIIX, 1996) (default)
  6. rhel6.6.0 RHEL 6.6.0 PC
  7. rhel6.5.0 RHEL 6.5.0 PC
  8. rhel6.4.0 RHEL 6.4.0 PC
  9. rhel6.3.0 RHEL 6.3.0 PC
  10. rhel6.2.0 RHEL 6.2.0 PC
  11. rhel6.1.0 RHEL 6.1.0 PC
  12. 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文件为:

  1. # eth0是你虚拟机的网卡,可以通过ifconfig查看
  2. auto eth0
  3. iface eth0 inet static
  4. #ip
  5. address 223.129.0.xxx
  6. # 子网和网关
  7. netmask 255.255.255.xxx
  8. gateway 223.129.0.xxx

修改/etc/resolvconf/resolv.conf.d/base文件,设置dns

  1. nameserver 8.8.8.8

重启虚拟机,通过ifconfig看到信息:

  1. eth0 Link encap:Ethernet HWaddr :::7c:da:c9
  2. inet addr:223.129.0.xxx Bcast:223.129.0.xxx Mask:255.255.255.xxx
  3. inet6 addr: fe80:::ff:fe7c:dac9/ Scope:Link
  4. UP BROADCAST RUNNING MULTICAST MTU: Metric:
  5. RX packets: errors: dropped: overruns: frame:
  6. TX packets: errors: dropped: overruns: carrier:
  7. collisions: txqueuelen:
  8. RX bytes: (31.3 MB) TX bytes: (761.3 KB)
  9.  
  10. lo Link encap:Local Loopback
  11. inet addr:127.0.0.1 Mask:255.0.0.0
  12. inet6 addr: ::/ Scope:Host
  13. UP LOOPBACK RUNNING MTU: Metric:
  14. RX packets: errors: dropped: overruns: frame:
  15. TX packets: errors: dropped: overruns: carrier:
  16. collisions: txqueuelen:
  17. RX bytes: (5.6 KB) TX bytes: (5.6 KB)

此时用xshell连接,会出现connection failed

因为ubuntu虚拟机中必须安装openssh-server软件,这样才可以通过ssh连接

安装命令:

  1. sudo apt-get install openssh-server
  2. sudo /etc/init.d/ssh restart //重启ssh服务

安装好后,用以下命令查看22端口是否处于LISTEN状态,如果是,则说明已经配置好了ubuntu的ssh服务

  1. root@jin:/home/jin# netstat -ntlp
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0.0.0.0: 0.0.0.0:* LISTEN /sshd

2、centos下

这个没试,下面是通过图形界面配置后,相应文件内容

  1. [root@localhost network-scripts]# pwd
  2. /etc/sysconfig/network-scripts
  3. [root@localhost network-scripts]# cat ifcfg-ens3
  4. TYPE="Ethernet"
  5. BOOTPROTO="none"
  6. DEFROUTE="yes"
  7. IPV4_FAILURE_FATAL="no"
  8. IPV6INIT="yes"
  9. IPV6_AUTOCONF="yes"
  10. IPV6_DEFROUTE="yes"
  11. IPV6_FAILURE_FATAL="no"
  12. NAME="ens3"
  13. UUID="3b886635-d2a9-4569-9763-7468890a64d8"
  14. DEVICE="ens3"
  15. ONBOOT="yes"
  16. DNS1="8.8.8.8"
  17. IPADDR=223.129.0.xxx
  18. PREFIX=25
  19. GATEWAY=223.129.0.xxx
  20. IPV6_PEERDNS=yes
  21. IPV6_PEERROUTES=yes
  22. IPV6_PRIVACY=no

补充

1、桥接和NAT

。。。。

2、创建虚拟机时各命令含义

。。。。占坑之后写

3、libvirt

。。。