虚拟化KVM
什么是虚拟化
虚拟化技术是云计算的根基,在计算机技术中,虚拟化(技术)或虚拟技术(英语:Virtualization)是一种资源管理技术,是将计算机的各种实体资源(CPU、内存、磁盘空间、网络适配器等),予以抽象、转换后呈现出来并可供分割、组合为一个或多个电脑配置环境。由此,打破实体结构间的不可切割的障碍,使用户可以比原本的配置更好的方式来应用这些电脑硬件资源。这些资源的新虚拟部分是不受现有资源的架设方式,地域或物理配置所限制。一般所指的虚拟化资源包括计算(CPU+内存),网络,存储。
虚拟化通过模块的方式来进行分层,使得暴露出来的虚拟资源和物理资源相似。在定义中,只要遵循分层原则,通过这种原则能够抽象出虚拟资源,模块是资源的隔离方式,它保证了虚拟出来的资源不会互相影响。
kvm简介
KVM的全称是Kernel-based Virtual Machine,其是一种基于linux内核的采用硬件辅助虚拟化技术的全虚拟化解决方案。它最初由以色列的初创公司Qumranet开发,并在linux-2.6.20中开始被纳入在linux内核,成为内核源码的一部分。KVM自诞生之初就定位于基于硬件辅助的虚拟化来提供全虚拟化的支持,其以内核模块的形式被加载。加载KVM模块的linux内核相当于变成了一个Hypervisor,同时依赖linux内核提供的各种功能来实现硬件管理,拥有极高的兼容性及可扩展性。
上面提到KVM是作为一个内核模块出现的,所以它还得借助用户空间的程序来和用户进行交互,这就不得不提到大名鼎鼎的QEMU了。QEMU是一套由法布里斯·贝拉(Fabrice Bellard)所编写的以GPL许可证分发源码的模拟处理器,在GNU/Linux平台上使用广泛。其本身是一个纯软件的支持CPU虚拟化、内存虚拟化及I/O虚拟化等功能的用户空间程序。其借助KVM提供的虚拟化支持可以将CPU、内存等虚拟化工作交由KVM处理,自己则处理大多数I/O虚拟化的功能,可以实现极高的虚拟化效率。KVM及QEMU配合使用的整体接口如图1所示。
部署kvm
环境说明
主机名 | ip | 系统 |
---|---|---|
zwl | 192.168.159.134 | centos 8 |
//关闭防火墙和selinux
[root@zwl ~]# setenforce 0
[root@zwl ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@zwl ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//新建分区
[root@zwl ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 49G 0 part
├─cs-root 253:0 0 45.1G 0 lvm /
└─cs-swap 253:1 0 4G 0 lvm [SWAP]
sdb 8:16 0 200G 0 disk
sr0 11:0 1 10.3G 0 rom
[root@zwl ~]# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel
New disk label type? msdos
(parted) unit
Unit? [compact]? MiB
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 204800MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? xfs
Start? 10
End? 204790
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 204800MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 10.0MiB 204790MiB 204780MiB primary xfs lba
(parted) q
Information: You may need to update /etc/fstab.
[root@zwl ~]# udevadm settle
[root@zwl ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 49G 0 part
├─cs-root 253:0 0 45.1G 0 lvm /
└─cs-swap 253:1 0 4G 0 lvm [SWAP]
sdb 8:16 0 200G 0 disk
└─sdb1 8:17 0 200G 0 part
sr0 11:0 1 10.3G 0 rom
[root@zwl ~]#
//格式化并挂载
[root@zwl ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=13105920 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=0 inobtcount=0
data = bsize=4096 blocks=52423680, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=25597, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@zwl ~]# blkid /dev/sdb1
/dev/sdb1: UUID="0dce0ec3-bf10-407a-9eb5-e38d9a73a63a" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="ed8c267b-01"
[root@zwl ~]# mkdir /kvmdata
[root@zwl ~]# vim /etc/fstab
UUID=0dce0ec3-bf10-407a-9eb5-e38d9a73a63a /kvmdata xfs defaults 0 0
[root@zwl ~]# mount -a
[root@zwl ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs tmpfs 3.8G 9.0M 3.8G 1% /run
tmpfs tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/mapper/cs-root xfs 46G 2.0G 44G 5% /
/dev/sda1 xfs 1014M 211M 804M 21% /boot
tmpfs tmpfs 774M 0 774M 0% /run/user/0
/dev/sdb1 xfs 200G 1.5G 199G 1% /kvmdata
[root@zwl ~]#
//配置yum源
[root@zwl ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 15891 0 --:--:-- --:--:-- --:--:-- 15891
[root@zwl ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@zwl ~]# yum clean all
//安装所需软件包
[root@zwl ~]# yum -y install epel-release
[root@zwl ~]# yum -y install vim wget net-tools unzip zip gcc gcc-c++ qemu-kvm qemu-img virt-manager libvirt libvirt-client virt-install virt-viewer libguestfs-tools
[root@zwl ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/qemu-kvm-tools-1.5.3-175.el7.x86_64.rpm
--2022-10-07 16:22:39-- http://mirror.centos.org/centos/7/os/x86_64/Packages/qemu-kvm-tools-1.5.3-175.el7.x86_64.rpm
Resolving mirror.centos.org (mirror.centos.org)... 13.233.140.107, 2401:78c0::e00e
Connecting to mirror.centos.org (mirror.centos.org)|13.233.140.107|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 242540 (237K) [application/x-rpm]
Saving to: ‘qemu-kvm-tools-1.5.3-175.el7.x86_64.rpm’
qemu-kvm-tools-1.5.3-175.el7.x8 100%[=====================================================>] 236.86K 483KB/s in 0.5s
2022-10-07 16:22:40 (483 KB/s) - ‘qemu-kvm-tools-1.5.3-175.el7.x86_64.rpm’ saved [242540/242540]
[root@zwl ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libvirt-python-4.5.0-1.el7.x86_64.rpm
--2022-10-07 16:22:55-- http://mirror.centos.org/centos/7/os/x86_64/Packages/libvirt-python-4.5.0-1.el7.x86_64.rpm
Resolving mirror.centos.org (mirror.centos.org)... 13.233.140.107, 2401:78c0::e00e
Connecting to mirror.centos.org (mirror.centos.org)|13.233.140.107|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 351160 (343K) [application/x-rpm]
Saving to: ‘libvirt-python-4.5.0-1.el7.x86_64.rpm’
libvirt-python-4.5.0-1.el7.x86_ 100%[=====================================================>] 342.93K 547KB/s in 0.6s
2022-10-07 16:22:55 (547 KB/s) - ‘libvirt-python-4.5.0-1.el7.x86_64.rpm’ saved [351160/351160]
[root@zwl ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/bridge-utils-1.5-9.el7.x86_64.rpm
--2022-10-07 16:23:03-- http://mirror.centos.org/centos/7/os/x86_64/Packages/bridge-utils-1.5-9.el7.x86_64.rpm
Resolving mirror.centos.org (mirror.centos.org)... 13.233.140.107, 2401:78c0::e00e
Connecting to mirror.centos.org (mirror.centos.org)|13.233.140.107|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 32480 (32K) [application/x-rpm]
Saving to: ‘bridge-utils-1.5-9.el7.x86_64.rpm’
bridge-utils-1.5-9.el7.x86_64.r 100%[=====================================================>] 31.72K --.-KB/s in 0.1s
2022-10-07 16:23:04 (247 KB/s) - ‘bridge-utils-1.5-9.el7.x86_64.rpm’ saved [32480/32480]
[root@zwl ~]#
[root@zwl ~]# rpm -ivh --nodeps libvirt-python-4.5.0-1.el7.x86_64.rpm
[root@zwl ~]# rpm -ivh --nodeps qemu-kvm-tools-1.5.3-175.el7.x86_64.rpm
//验证cpu是否支持kvm,vmx是intel的 svm是AMD的
[root@zwl ~]# egrep -o 'vmx|svm' /proc/cpuinfo
svm
svm
svm
svm
[root@zwl ~]#
//安装kvm
[root@zwl ~]# yum -y install qemu-kvm \
> qemu-kvm-tools \
> qemu-img \
> virt-manager \
> libvirt \
> libvirt-python \
> libvirt-client \
> virt-install \
> virt-viewer \
> bridge-utils \
> libguestfs-tools
//配置网络
[root@zwl ~]# cd /etc/sysconfig/network-scripts/
[root@zwl network-scripts]# cp ifcfg-ens160 ifcfg-br0
[root@zwl network-scripts]# vi ifcfg-br0
TYPE=Bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.159.134
PREFIX=24
GATEWAY=192.168.159.2
DNS1=8.8.8.8
[root@zwl network-scripts]# vim ifcfg-ens160
[root@zwl network-scripts]# cat ifcfg-ens160
TYPE=Ethernet
BOOTPROTO=none
NAME=ens160
DEVICE=ens160
ONBOOT=yes
BRIDGE=br0
[root@zwl network-scripts]# nmcli c reload
[root@zwl network-scripts]# systemctl restart NetworkManager
[root@zwl network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP group default qlen 1000
link/ether 00:0c:29:70:77:e7 brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:0c:29:70:77:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.159.134/24 brd 192.168.159.255 scope global noprefixroute br0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe70:77e7/64 scope link dadfailed tentative
valid_lft forever preferred_lft forever
[root@zwl network-scripts]#
[root@zwl ~]# virsh --version
6.0.0
[root@zwl ~]# virt-install --version
2.2.1
[root@zwl ~]#
//将qemu-kvm这个命令做一个软链接到/usr/bin/qemu-kvm
[root@zwl ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@zwl ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 Oct 7 17:38 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
[root@zwl ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c297077e7 no ens160
KVM管理界面安装
Kvm的web界面是由webvirtmgr程序提供的
//安装依赖包
[root@zwl ~]# yum -y install git python2-pip supervisor nginx python2-devel
[root@zwl ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libxml2-python-2.9.1-6.el7.5.x86_64.rpm
//下载webvirtmgr
[root@zwl ~]# wget https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/p/python-websockify-0.6.0-2.el7.noarch.rpm
--2022-10-07 17:43:14-- https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/p/python-websockify-0.6.0-2.el7.noarch.rpm
Resolving download-ib01.fedoraproject.org (download-ib01.fedoraproject.org)... 152.19.134.145, 2600:2701:4000:5211:dead:beef:fe:fed6
Connecting to download-ib01.fedoraproject.org (download-ib01.fedoraproject.org)|152.19.134.145|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 63388 (62K) [application/x-rpm]
Saving to: ‘python-websockify-0.6.0-2.el7.noarch.rpm’
python-websockify-0.6.0-2 100%[==================================>] 61.90K 120KB/s in 0.5s
2022-10-07 17:43:16 (120 KB/s) - ‘python-websockify-0.6.0-2.el7.noarch.rpm’ saved [63388/63388]
[root@zwl ~]# rpm -ivh --nodeps libxml2-python-2.9.1-6.el7.5.x86_64.rpm
warning: libxml2-python-2.9.1-6.el7.5.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:libxml2-python-2.9.1-6.el7.5 ################################# [100%]
[root@zwl ~]# rpm -ivh --nodeps python-websockify-0.6.0-2.el7.noarch.rpm
warning: python-websockify-0.6.0-2.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:python-websockify-0.6.0-2.el7 ################################# [100%]
[root@zwl ~]#
//升级pip
[root@zwl ~]# pip2 install --upgrade pip
[root@zwl ~]# pip -V
pip 20.3.4 from /usr/lib/python2.7/site-packages/pip (python 2.7)
//从github上下载webvirtmgr代码
[root@zwl ~]# cd /usr/local/src/
[root@zwl src]# git clone http://github.com/retspen/webvirtmgr.git
Cloning into 'webvirtmgr'...
warning: redirecting to https://github.com/retspen/webvirtmgr.git/
remote: Enumerating objects: 5614, done.
remote: Total 5614 (delta 0), reused 0 (delta 0), pack-reused 5614
Receiving objects: 100% (5614/5614), 2.97 MiB | 471.00 KiB/s, done.
Resolving deltas: 100% (3606/3606), done.
[root@zwl src]# ls
webvirtmgr
[root@zwl src]#
//安装webvirtmgr
[root@zwl src]# cd webvirtmgr/
[root@zwl webvirtmgr]# pip install -r requirements.txt
//检查sqlite3是否安装
[root@zwl webvirtmgr]# python3
Python 3.6.8 (default, Jan 19 2022, 23:28:49)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
[root@zwl webvirtmgr]#
//初始化账号信息
[root@zwl ~]# cd /usr/local/src/webvirtmgr/
[root@zwl webvirtmgr]# python2 manage.py syncdb
WARNING:root:No local_settings file found.
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table servers_compute
Creating table instance_instance
Creating table create_flavor
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes //是否创建超级管理员账号
Username (leave blank to use 'root'): //指定超级管理员账号用户名,默认留空
Email address: 3012172530@qq.com
Password: //设置超级管理员密码
Password (again): //再次输入确认超级管理员密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
[root@zwl webvirtmgr]#
//拷贝web网页到指定目录
[root@zwl webvirtmgr]# mkdir /var/www
[root@zwl webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@zwl webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
[root@zwl webvirtmgr]#
//生成一对公钥与私钥,由于这里webvirtmgr和kvm服务部署在同一台主机中,所以这里本地信任。如果kvm部署在其他机器上的时候,那么就需要把公钥发送到kvm主机中
[root@zwl webvirtmgr]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:kzll/hllaAwxhtGeXWyi6gWbv5k+1DLrFkLKVzvBTLA root@zwl
The key's randomart image is:
+---[RSA 3072]----+
| o==. . |
| .oo+..+ |
| E=o+++o |
| o*O..o |
| . oS*.+. |
| o *oO..o |
| o = *o |
| . =o |
| +*o |
+----[SHA256]-----+
[root@zwl webvirtmgr]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.159.134
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.159.134 (192.168.159.134)' can't be established.
ECDSA key fingerprint is SHA256:w1jC4OTx6KkZbLbuZccFQUen4XI6ScQ+JLHjUalJHgk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.159.134's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.159.134'"
and check to make sure that only the key(s) you wanted were added.
[root@zwl webvirtmgr]#
//配置端口转发
[root@zwl webvirtmgr]# ssh 192.168.159.134 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
//配置nginx
[root@zwl ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[root@zwl ~]# vim /etc/nginx/nginx.conf
删除listen [::]:80;行
参数server_name行改成server_name localhost;
删除root /usr/share/nginx/html;行
server {
listen 80 ;
server_name localhost;
在include /etc/nginx/default.d/*.conf;行下添加
location / {
root html;
index index.html index.htm;
}
//配置nginx虚拟主机
[root@zwl ~]# vim /etc/nginx/conf.d/webvirtmgr.conf
[root@zwl ~]# cat /etc/nginx/conf.d/webvirtmgr.conf
server {
listen 80 default_server;
server_name $hostname;
#access_log /var/log/nginx/webvirtmgr_access_log;
location /static/ {
root /var/www/webvirtmgr/webvirtmgr;
expires max;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Proto $remote_addr;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
client_max_body_size 1024M;
}
}
[root@zwl ~]#
//确保bind绑定本机的8000端口
[root@zwl ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = '127.0.0.1:8000'
backlog = 2048
[root@zwl ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:8000 0.0.0.0:*
LISTEN 0 100 0.0.0.0:6080 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::1]:6080 [::]:*
LISTEN 0 128 [::1]:8000 [::]:*
//设置supervisor
//在文件最后添加如下信息
[root@zwl ~]# vim /etc/supervisord.conf
[program:webvirtmgr]
command=/usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
logfile=/var/log/supervisor/webvirtmgr.log
log_stderr=true
user=nginx
[program:webvirtmgr-console]
command=/usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
redirect_stderr=true
user=nginx
[root@zwl ~]# vim /etc/supervisord.conf
[root@zwl ~]# systemctl restart supervisord.service
[root@zwl ~]# systemctl enable supervisord.service
Created symlink /etc/systemd/system/multi-user.target.wants/supervisord.service → /usr/lib/systemd/system/supervisord.service.
[root@zwl ~]#
//配置nginx用户
[root@zwl ~]# su - nginx -s /bin/bash
[nginx@zwl ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/nginx/.ssh/id_rsa):
Created directory '/var/lib/nginx/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/nginx/.ssh/id_rsa.
Your public key has been saved in /var/lib/nginx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:E/ebx3idj64JaKmnPMClw1tF7RwzuLuA7Kdc1RK07Zo nginx@zwl
The key's randomart image is:
+---[RSA 3072]----+
| .o |
| .oo= |
| .++o+ |
| . o*o. |
| + + .S.o . |
| O o..B = ..|
| . =..E.. + +..|
| .ooooo . + ..|
| oo++ ooo .|
+----[SHA256]-----+
[nginx@zwl ~]$ touch ~/.ssh/config
[nginx@zwl ~]$ echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
[nginx@zwl ~]$ chmod 0600 ~/.ssh/config
[nginx@zwl ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.159.134
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/nginx/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Warning: Permanently added '192.168.159.134' (ECDSA) to the list of known hosts.
root@192.168.159.134's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.159.134'"
and check to make sure that only the key(s) you wanted were added.
[nginx@zwl ~]$
//验证基于密钥认证是否成功
[nginx@zwl ~]$ ssh root@192.168.159.134
Warning: Permanently added '192.168.159.134' (ECDSA) to the list of known hosts.
Last login: Fri Oct 7 18:03:24 2022 from 192.168.159.134
[root@zwl ~]# exit
logout
Connection to 192.168.159.134 closed.
[nginx@zwl ~]$ exit
logout
[root@zwl ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@zwl ~]# cat /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[Remote libvirt SSH access]
Identity=unix-user:root
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes
[root@zwl ~]#
[root@zwl ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@zwl ~]# systemctl restart nginx.service
[root@zwl ~]# systemctl restart libvirtd
[root@zwl ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:8000 0.0.0.0:*
LISTEN 0 100 0.0.0.0:6080 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::1]:6080 [::]:*
LISTEN 0 128 [::1]:8000 [::]:*
[root@zwl ~]#
web界面登录
[root@zwl ~]# yum -y install novnc
[root@zwl ~]# chmod +x /etc/rc.d/rc.local
[root@zwl ~]# echo "nohup novnc_server 192.168.100.100:5920 &" >> /etc/rc.d/rc.local
[root@zwl ~]# . /etc/rc.d/rc.local
[root@zwl ~]# nohup: ignoring input and appending output to 'nohup.out'