利用初始化好的虚拟机当作模板,用于克隆
第一步:先拷贝个虚拟机当作模板
[root@ubunt images]# virt-clone -o rocky8 -f /var/lib/libvirt/images/rocky8-template.qcow2 -n rocky8-template
Allocating 'rocky8-template.qcow2'                                                                 |  20 GB  00:00:05     

Clone 'rocky8-template' created successfully.
[root@ubunt images]# virsh list --all
 Id   Name              State
----------------------------------
 5    centos7.9         running
 7    rocky8-2          running
 -    rocky8            shut off
 -    rocky8-3          shut off
 -    rocky8-template   shut off

第二步:修改配置
[root@ubunt images]# virsh start rocky8-template (启动模板虚拟机)
Domain rocky8-template started
[root@ubunt images]# virt-manager (打开图形化页面,进入模板虚拟机命令行)
查看ip并用宿主机链接
[root@ubunt images]# ssh 192.168.122.195
[root@rocky8 ~]# 
配置yum仓并把旧的文件放到bak目录中
[root@rocky8 ~]# cd /etc/yum.repos.d/
[root@rocky8 yum.repos.d]# mkdir bak
[root@rocky8 yum.repos.d]# mv Rocky-* bak/
[root@rocky8 yum.repos.d]# ls
bak
配置新的yum仓
[root@rocky8 yum.repos.d]# cat base.repo 
[BaseOS]
name=BaseOS
baseurl=http://mirrors.163.com/rocky/$releasever/BaseOS/x86_64/os/
        https://mirrors.nju.edu.cn/rocky/$releasever/BaseOS/x86_64/os/
        https://mirrors.sjtug.sjtu.edu.cn/rocky/$releasever/BaseOS/x86_64/os/
        http://mirrors.sdu.edu.cn/rocky/$releasever/BaseOS/x86_64/os/       
gpgcheck=0

[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/rockylinux/$releasever/AppStream/x86_64/os/
        http://mirrors.163.com/rocky/$releasever/AppStream/x86_64/os/
        https://mirrors.nju.edu.cn/rocky/$releasever/AppStream/x86_64/os/
        https://mirrors.sjtug.sjtu.edu.cn/rocky/$releasever/AppStream/x86_64/os/
        http://mirrors.sdu.edu.cn/rocky/$releasever/AppStream/x86_64/os/
gpgcheck=0

[extras]
name=extras
baseurl=https://mirrors.aliyun.com/rockylinux/$releasever/extras/$basearch/os
        http://mirrors.163.com/rocky/$releasever/extras/$basearch/os
        https://mirrors.nju.edu.cn/rocky/$releasever/extras/$basearch/os
        https://mirrors.sjtug.sjtu.edu.cn/rocky/$releasever/extras/$basearch/os
        http://mirrors.sdu.edu.cn/rocky/$releasever/extras/$basearch/os 
       
gpgcheck=0
enabled=1

[PowerTools]
name=CentOS-$releasever - PowerTools
baseurl=https://mirrors.aliyun.com/rockylinux/$releasever/PowerTools/$basearch/os/
        http://mirrors.163.com/rocky/$releasever/PowerTools/$basearch/os/
        http://mirrors.sdu.edu.cn/rocky/$releasever/PowerTools/$basearch/os/
        https://mirrors.sjtug.sjtu.edu.cn/rocky/$releasever/PowerTools/$basearch/os/
        http://mirrors.sdu.edu.cn/rocky/$releasever/PowerTools/$basearch/os/
gpgcheck=0
enabled=0


[epel]
name=EPEL
baseurl=https://mirror.tuna.tsinghua.edu.cn/epel/$releasever/Everything/$basearch
        https://mirrors.cloud.tencent.com/epel/$releasever/Everything/$basearch
        https://mirrors.huaweicloud.com/epel/$releasever/Everything/$basearch
        https://mirrors.aliyun.com/epel/$releasever/Everything/$basearch
gpgcheck=0
enabled=1

 

 
 
第三步:下载常用的包
yum -y install vim-enhanced tcpdump lrzsz tree telnet bash-completion net-tools wget bzip2 lsof tmux man-pages zip unzip nfs-utils

 

 
 
第四步:关闭防火墙
[root@rocky8 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@rocky8 ~]# systemctl status firewalld (查看防火墙状态)

 

 
 
第五步:修改网卡名
[root@rocky8 ~]# vi /etc/default/grub
倒数第三行在swap后加net.ifnames=0
GRUB_CMDLINE_LINUX="resume=/dev/mapper/rl-swap rd.lvm.lv=rl/root rd.lvm.lv=rl/swap net.ifnames=0"
[root@rocky8 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg 
修改网卡文件名(把ifcfg-enp1s0修改成ifcfg-eth0)
[root@rocky8 ~]# cd /etc/sysconfig/network-scripts/
[root@rocky8 network-scripts]# ls
ifcfg-enp1s0
[root@rocky8 network-scripts]# mv ifcfg-enp1s0 ifcfg-eth0
[root@rocky8 network-scripts]# ls
ifcfg-eth0
进入网卡配置文件修改网卡名
[root@rocky8 network-scripts]# vi ifcfg-eth0
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
IPADDR=192.168.122.100
PREFIX=24
GATEWAY=192.168.122.1    #ronte -n 查看网关
DNS1=192.168.122.1       #(指向自己)
DNS2=223.5.5.5           #(指向阿里)
DNS3=180.76.76.76        #(指向百度)
ONBOOT=yes

更改后重启reboot

 

 
 
第六步:关闭selinux
[root@rocky8 ~]# vi /etc/selinux/config 
SELINUX=disabled

 

 
 
第七步:优化内核参数
sysctl.conf
/etc/security/limits.conf修改资源限制

 

 
第八步:修改时区

 

 
 
第九步:打通GUK验证,用于宿主机和虚拟机进行连接
[root@ubuntu ~]#ssh-keygen
[root@ubuntu ~]#ssh-copy-id 192.168.122.100

 

 
 

模板做好,可以用脚本批量创建虚拟机

关闭模板虚拟机

[root@rocky8 ~]# poweroff 
Connection to 192.168.122.100 closed by remote host.
Connection to 192.168.122.100 closed.