KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)

1. Install VNC

Turn off the firewall and selinux, modify the selinux config file:
[root @ localhost ~] # vim / etc / selinux / config to
view the selinux status
[root @ localhost ~] # getenforce
temporarily shut down the selinux service
[root @ localhost ~] # setenforce 0

KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)
[root @ localhost ~] # systemctl stop firewalld Turn off the firewall
[root @ localhost ~] # systemctl disable firewalld Disable the firewall from starting up and
do not want to turn off the firewall. You can add the VNC service to the firewall
[root @ localhost ~] # firewall-cmd --permanent- add-service vnc-server
(Note: VNC needs to open port 5901, this is just a test, for security reasons, it is recommended to manually add ports in the production environment, it is not recommended to close the firewall.)
Install the main program:
[root @ localhost ~] # yum install -y vnc- *

[root@localhost ~]# cp /lib/systemd/system/[email protected] /etc/systemd/system/vncserver@:1.service

Modify the vncserver @: 1.service file
[root @ localhost ~] # vim /etc/systemd/system/vncserver@:1.service
will
ExecStart = / usr / sbin / runuser -l <user> -c "/ usr / bin / vncserver% i "
PIDFile = / home / <user> /. vnc /% H% i.pid
modified to
ExecStart = / usr / sbin / runuser -l root -c" / usr / bin / vncserver% i "
PIDFile = /home/root/.vnc/%H%i.pid
KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)
(Note: modify according to the user defined by yourself, modify <user> to a custom login user name)
Set the VNC login password
[root @ localhost ~] # vncpasswd
KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)
( Note: You need to use it when logging in remotely with the VNC client)
Modify the qemu.conf configuration file
[root @ localhost ~] # vim /etc/libvirt/qemu.conf
Modify the following content: Remove the "#" of the following options
vnc_password = "123456 "Is the password used when setting vncpassword
vnc_listen =" 0.0.0.0 "
KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)
[root @ localhost ~] # systemctl daemon-reload reload

启动vncserver@:1.service
[root@localhost ~]# systemctl start vncserver@:1.service

Set vncserver @: 1.service to start at boot
[root @ localhost ~] # systemctl enable vncserver @: 1.service

Connection test:
KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)
KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)
KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)

The VNC installation test is complete. Next, we create a KVM virtual machine.

2. Virtual machine creation

Create a system image storage directory, use the tool to upload the ISO system installation package to the / kvm / iso directory,
[root @ localhost /] # mkdir -p / kvm / iso
[root @ localhost /] # ls / kvm / iso /
CentOS -7.2-x86_64-DVD-1511.iso
creates a 20G virtual disk, / var / lib / libvirt / images / is the default storage path of the virtual disk
[root @ localhost /] # qemu-img create -f qcow2 / var / lib /libvirt/images/kvmtest.qcow2 20G
Tip:
When using a virtual machine to install and test KVM, you need to turn on the CPU virtualization function.
KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)
If you install KVM on a physical device , you need to turn on the CPU virtualization function in the BIOS, otherwise you cannot create it when creating a virtual machine. .
Create a virtual machine:
[root @ localhost ~] # virt-install --virt-type kvm --name kvmtest --ram 1024 --vcpus 2 --cdrom = / kvm / iso / CentOS-7.2-x86_64-DVD-1511 .iso --disk path = / var / lib / libvirt / images / kvmtest.qcow2 --network bridge = br0 --graphics vnc, listen = 0.0.0.0 --noautoconsole --autostart
command analysis:
--virt-type kvm #Specify the virtual machine type "kvm, qemu, xen" (default is KVM)
--name kvmtest #Specify the name of the virtual machine
--ram 1024 #Specify the memory size
--vcpus 2 # Specify the number of cores of the virtual machine CPU
--cdrom = / kvm / iso / CentOS-7.2-x86_64-DVD-1511.iso #Specify ISO image storage location
--disk path = / var / lib / libvirt / images / kvmtest.qcow2 #Specify the storage location of the disk file
--network bridge = br0 #Specify the virtual machine network: "--network" ("bridge network in the enterprise "Most used", "NAT network is the default network, use" default "to indicate" use depending on the situation; which kind of network to use need to know their names)
--graphics vnc, listen = 0.0.0.0 #Specify vnc, vnc listen 0.0. 0.0 address: "--graphics" (usually use "VNC", disadvantages: virtual machines can not be copied; desktop virtualization uses "spice", which requires high configuration, especially graphics cards; both methods of port can be specified)
- -noautoconsole #Do not automatically connect to the console
--autostart #Specify "after the host is restarted", whether the virtual machine is automatically powered on (optional, generally set to automatically boot)

More command resolutions can be viewed using virt-install -h.

VNC connection test:
the port of the virtual machine is 5900, the default is -1 port, you can view the port through the virsh edit kvmtest command,
[root @ localhost ~] # virsh edit kvmtest
KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)

If you need to re-customize the vnc connection port, you can modify it through virsh edit kvmtest. Modify the port and autoport content. For example:
<graphics type = 'vnc' port = '-1' autoport = 'yes' listen = '0.0.0.0'>
<listen type = 'address' address = '0.0.0.0' />
</ graphics>
modified to
<graphics type = 'vnc' port = '<custom port>' autoport = 'no' listen = '0.0.0.0' >
<listen type = 'address' address = '0.0.0.0' />
</ graphics>

VNC connection test:
Enter the host IP: 5900 in the vnc address bar and
KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)
press Enter. The following interface is displayed. Click "Continue" to continue. The
KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)
virtual machine is connected normally, and the system installation will not be introduced. At this point, the KVM virtual machine uses VNC connection settings to complete the settings.


  1. How to set up using VNC connection to expand multiple virtual machines! !
    As we have said above, you only need to modify the virtual machine configuration file of the newly created project system and rename the vnc port number. It is best to use the port after 5900 (personal habit) when specifying the port number. The specific operations are as follows:
    [root @ localhost ~] # virsh edit <virtual machine name>
    modify the following:
    <graphics type = 'vnc' port = '-1' autoport = 'yes' listen = '0.0.0.0'>
    <listen type = 'address' address = '0.0.0.0' />
    </ graphics>
    modified to
    <graphics type = 'vnc' port = '<custom port>' autoport = 'no' listen = '0.0.0.0'>
    <listen type = 'address' address = '0.0.0.0' />
    </ graphics>
    KVM text mode to create virtual machines and use VNC connection to control virtual machines (non-console)
    Reminder: After customizing the port, if the firewall is running, remember to open the port! ! ! ^ _ ^
    Multi-virtual machine using VNC connection setting is completely over here! !
    In the next article, we will continue to introduce the use of console to connect virtual machines! !

Guess you like

Origin blog.51cto.com/3001441/2489127