步骤一:查看处理器型号是 Intel 还是 AMD
# lscpu | grep -E 'vendor ID|Model name'
步骤二:查看有没有开启虚拟机嵌套
如果是 Intel 的话:
# cat /sys/module/kvm_intel/parameters/nested
如果是 AMD 的话:
# cat /sys/module/kvm_amd/parameters/nested
(补充:如果这里的输出结果是 N 则代表虚拟机嵌套没有开启)
# lscpu | grep -E 'vendor ID|Model name'
如果是 Intel 的话:
# cat /sys/module/kvm_intel/parameters/nested
如果是 AMD 的话:
# cat /sys/module/kvm_amd/parameters/nested
(补充:如果这里的输出结果是 N 则代表虚拟机嵌套没有开启)
例如 KVM 虚拟机所处的网络是 Routed 模式而是 NAT 模式的话,则无论如何也无法被其他物理机访问。
参考步骤:停止所有虚拟机 –> 记录原虚拟网络的名称和网段信息 –> 修改原虚拟网络的名称 –> 用原虚拟网络的名称和网段信息创建新的虚拟网络 –> 开启所有虚拟机
# echo 1 > /proc/sys/net/ipv4/ip_forward
# sysctl -w net.ipv4.ip_forward=1
# vi /etc/sysctl.conf
添加以下内容:
......
net.ipv4.ip_forward = 1
# sysctl -p
KVM 虚拟化以及基于 KVM 的技术例如 OpenStack 和 OpenShift 云平台上的虚拟机网卡使用的是 virtio 类型
virtio 类型是一种半虚拟化 (Para-virtualization) 技术。其没有速度限制,会随着宿主机的网络、处理器、内存的实际情况实时发生变化,能多快就跑多快
使用类似 ethtool 的命令查看 virtio 类型的网卡时不会显示网卡速度
在创建 KVM 虚拟机之前要先安装 KVM 并创建 KVM 虚拟网络
在 Rocky Linux 官网上下载安装系统所需要的镜像:
https://rockylinux.org/download
主要用于批量克隆出新的 KVM 机器,节约创建新虚拟机的时间
(只在真机上执行以下步骤)
# qemu-img create -f qcow2 /var/lib/libvirt/images/rockylinux9.qcow2 10G
(补充:这里以创建 10G 大小的 rockylinux9.qcow2 硬盘文件为例)
(只在真机上执行以下步骤)
# ls /var/lib/libvirt/images/ | grep rockylinux9.qcow2
(补充:这里以显示 rockylinux9.qcow2 硬盘文件为例)
(只在真机上执行以下步骤)
# virt-manager
(只在真机上执行以下步骤)
(步骤略)
(只在真机上执行以下步骤)

(图:1)
(只在真机上执行以下步骤)

(图:2)
(补充:这里以使用 Rocky-9.6-x86_6-dvd1.iso 系统镜像为例)
(只在真机上执行以下步骤)

(图:3)
(补充:这里以设置 2048 MiB 内容和 2 核 CPU 为例)
(只在真机上执行以下步骤)

(图:4)
(补充:这里以使用 rockylinux9.qcow2 硬盘文件为例)
(只在真机上执行以下步骤)
(注意:虚拟网络必须提前创建好)

(图:5)
(补充:这里以将虚拟机命名为 rockylinux9 并使用 network 网络为例)
(只在真机上执行以下步骤)

(图:6)
(只在真机上执行以下步骤)

(图:7)
(只在真机上执行以下步骤)
需要手动配置的地方有四个:
1) “INSTALLATION DESTINATION”
2) “KDUMP”
3) “SOFTWARE SELECTION”
4) “Root Password”
分别点击以后就可以配置了
(只在真机上执行以下步骤)
(补充:完成后点击左上角的 “DONE”)
(注意:只分一个分区,只设置一个挂载点挂载到根 “/”,使用标准硬盘类型,硬盘格式设置为 XFS)

(图:8)

(图:9)
(只在真机上执行以下步骤)
(补充:完成后点击左上角的 “DONE”)

(图:10)
(只在真机上执行以下步骤)
(补充:完成后点击左上角的 “DONE”)

(图:11)
(只在真机上执行以下步骤)

(图:12)
(只在真机上执行以下步骤)

(图:13)
(只在真机上执行以下步骤)

(只在真机上执行以下步骤)
1) 一定要使用刚刚创建的 /var/lib/libvirt/images/rockylinux8.qcow2 作为安装虚拟机的硬件文件
2) 虚拟机网络 “0” 要提前创建好
3) 只分一个分区,只设置一个挂载点挂载到根 “/”,使用标准硬盘,硬盘格式是 XFS
4) 取消 “KDUMP”
5) 选择最小化安装系统
6) 设置 root 密码
# vi /etc/ssh/sshd_config
在此行下面:
......
#PermitRootLogin prohibit-password
......
添加:
......
PermitRootLogin yes
......
# systemctl restart ssd
(只在虚拟机上执行以下步骤)
# vi /etc/NetworkManager/system-connections/enp1s0.nmconnection
将全部内容修改如下:
[connection]
id=enp1s0
type=ethernet
autoconnect-priority=-999
interface-name=enp1s0
timestamp=1755516857
[ethernet]
[ipv4]
method=auto
(只在虚拟机上执行以下步骤)
# reboot
(只在虚拟机上执行以下步骤)
# vi /etc/selinux/config
将全部内容修改如下:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
# See also:
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/using_selinux/changing-selinux-states-and-modes_using-selinux#changing-selinux-modes-at-boot-time_changing-selinux-states-and-modes
#
# NOTE: Up to RHEL 8 release included, SELINUX=disabled would also
# fully disable SELinux during boot. If you need a system with SELinux
# fully disabled instead of SELinux running with no policy loaded, you
# need to pass selinux=0 to the kernel command line. You can use grubby
# to persistently set the bootloader to boot with selinux=0:
#
# grubby --update-kernel ALL --args selinux=0
#
# To revert back to SELinux enabled:
#
# grubby --update-kernel ALL --remove-args selinux
#
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
(只在虚拟机上执行以下步骤)
# vi /etc/sysconfig/network
将全部内容修改如下:
# Created by anaconda
NOZEROCONF="yes"
(只在虚拟机上执行以下步骤)
# vi /etc/default/grub
将全部内容修改如下:
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_SERIAL_COMMAND="serial --unit=1 --speed=115200"
GRUB_CMDLINE_LINUX="biosdevname=0 net.ifnames=0 console=tty0 console=ttyS0,115200n8"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true
(只在虚拟机上执行以下步骤)
# grub2-mkconfig -o grub
(只在虚拟机上执行以下步骤)
# blkid
/dev/vda1: UUID="682d9854-9ca6-4272-9adb-dc9bee3098f6" TYPE="xfs" PARTUUID="bf6af362-01"
(补充:这里的 UUID 是: 682d9854-9ca6-4272-9adb-dc9bee3098f6)
(只在虚拟机上执行以下步骤)
# vi /etc/fstab
将以下内容:
......
UUID=682d9854-9ca6-4272-9adb-dc9bee3098f6 / xfs defaults 0 0
(补充:这里的 UUID 是: 682d9854-9ca6-4272-9adb-dc9bee3098f6)
修改为:
/dev/sda1 / xfs defaults 0 0
(只在虚拟机上执行以下步骤)
# yum -y remove firewalld-* python-firewall
(只在虚拟机上执行以下步骤)
# yum -y update
(只在虚拟机上执行以下步骤)
# yum install -y cloud-utils-growpart
# chmod 755 /etc/rc.local
(只在虚拟机上执行以下步骤)
# vi /etc/rc.local
添加以下内容:
......
/usr/bin/growpart /dev/sda1
/usr/sbin/xfs_growfs /
(只在虚拟机上执行以下步骤)
# vi /etc/hostname
将全部内容修改如下:
rockylinux9
(只在虚拟机上执行以下步骤)
# systemctl start serial-getty@ttyS0
# systemctl enable serial-getty@ttyS0
(只在虚拟机上执行以下步骤)
# history -c
(只在虚拟机上执行以下步骤)
# poweroff
(只在真机上执行以下步骤)
# sudo virt-sysprep -d rockylinux9
(补充:这里以清理 rockylinux9 虚拟机为例)
(
注意:如果此命令不存在
1) Rocky Linux 系统的话需要安装 libguestfs-tools
2) openSUSE 系统的话需要安装 guestfs-tools
)
(只在真机上执行以下步骤)
(步骤略)
# nmcli connection up enp1s0
Error: Connection activation failed: No suitable device found for this connection (device eth0 not available because profile is not compatible with device (mismatching interface name)).
(步骤略)
# nmcli connection show
# nmcli connection delete enp1s0
# nmcli connection delete Wired\ connection\ 1
(补充:这里以删除网卡配置 enp1s0 和 Wired\ connection\ 1 为例)
# nmcli connection show
(注意:确保这条命令输入后没有输出结果)
# nmcli device show
(注意:这里需要记住网卡硬件的名称,因为下个步骤需要使用)
# nmcli connection add con-name eth0 ifname eth0 type ethernet
(补充:这里以给 eth0 网卡添加个名为 eth0 的 NetworkManager 网卡配置为例)
(注意:这里的 ifname 后面的名称,必须和上个步骤输出命令后显示的网卡硬件名称一致)
# nmcli device connect eth0
(补充:这里以启动 eth0 网卡为例)
(注意:这里的 eth0 是在上个步骤中使用 con-name 参数配置的网卡配置名)
# nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.100.8/24 ipv4.gateway 192.168.100.1 autoconnect yes ipv4.dns 1.1.1.1
(补充:这里以给 eth0 网卡添加静态 IP 地址 192.168.100.8/24,网关 IP 地址 192.168.100.1,DNS 1.1.1.1 并开机自启为例)
(注意:这里的 eth0 是在前面的步骤中使用 con-name 参数配置的网卡配置名)
# nmcli connection up eth0
(补充:这里以让 eth0 网卡上配置的 IP 地址生效为例)
(注意:这里的 eth0 是在前面的步骤中使用 con-name 参数配置的网卡配置名)