更新 PVE 制作虚拟机模板

This commit is contained in:
CallMeR
2023-06-26 20:29:47 +08:00
parent 525ece3b7b
commit 5d594998e3
+55 -71
View File
@@ -22,7 +22,7 @@
```bash ```bash
## 编辑 ssh 配置文件 ## 编辑 ssh 配置文件
sudo vim /etc/ssh/sshd_config.d/server_sshd.conf $ sudo vim /etc/ssh/sshd_config.d/server_sshd.conf
``` ```
在配置文件中添加以下配置项,并保存。 在配置文件中添加以下配置项,并保存。
@@ -40,7 +40,7 @@ UseDNS no
```bash ```bash
## 重启 sshd ## 重启 sshd
sudo systemctl restart ssh.service $ sudo systemctl restart ssh.service
``` ```
### 1.2.配置软件源 ### 1.2.配置软件源
@@ -55,27 +55,22 @@ Debian12 云镜像的软件源配置采用了 `DEB822` 格式,新版 `sources.
```bash ```bash
## 进入系统源配置文件目录 ## 进入系统源配置文件目录
cd /etc/apt/ $ cd /etc/apt/
## 查看镜像配置文件 ## 查看镜像配置文件
cat sources.list $ cat sources.list
## 配置文件输出
#### 配置文件示例输出
# See /etc/apt/sources.list.d/debian.sources # See /etc/apt/sources.list.d/debian.sources
``` ```
进一步查看新配置文件 `debian.sources` 的内容,其配置包含 `3` 个部分。 进一步查看新配置文件 `debian.sources` 的内容,其配置包含 `3` 个部分。
```bash ```bash
## 查看新版镜像配置文件 ## 查看新版镜像配置文件
cat /etc/apt/sources.list.d/debian.sources $ cat /etc/apt/sources.list.d/debian.sources
## 新版配置文件输出 (主体部分)
#### 新版配置文件示例输出 (主体部分)
Types: deb deb-src Types: deb deb-src
URIs: mirror+file:///etc/apt/mirrors/debian.list URIs: mirror+file:///etc/apt/mirrors/debian.list
Suites: bookworm bookworm-updates bookworm-backports Suites: bookworm bookworm-updates bookworm-backports
@@ -86,22 +81,16 @@ URIs: mirror+file:///etc/apt/mirrors/debian-security.list
Suites: bookworm-security Suites: bookworm-security
Components: main Components: main
## 查看关联的配置文件 ## 查看关联的配置文件
cat /etc/apt/mirrors/debian.list $ cat /etc/apt/mirrors/debian.list
## 关联配置文件输出 (关联部分 1 )
#### 关联配置文件示例输出 (关联部分 1 )
https://deb.debian.org/debian https://deb.debian.org/debian
## 查看关联的配置文件 ## 查看关联的配置文件
cat /etc/apt/mirrors/debian-security.list $ cat /etc/apt/mirrors/debian-security.list
## 关联配置文件输出 (关联部分 2 )
#### 关联配置文件示例输出 (关联部分 2 )
https://deb.debian.org/debian-security https://deb.debian.org/debian-security
``` ```
@@ -116,7 +105,7 @@ https://deb.debian.org/debian-security
```bash ```bash
## 编辑 debian.sources 配置文件 ## 编辑 debian.sources 配置文件
sudo vim /etc/apt/sources.list.d/debian.sources $ sudo vim /etc/apt/sources.list.d/debian.sources
``` ```
删除里面全部内容,添加以下配置项,并保存。 删除里面全部内容,添加以下配置项,并保存。
@@ -140,7 +129,7 @@ Components: main contrib non-free non-free-firmware
```bash ```bash
## 编辑 debian.list 配置文件 ## 编辑 debian.list 配置文件
sudo vim /etc/apt/mirrors/debian.list $ sudo vim /etc/apt/mirrors/debian.list
## 替换为 USTC 地址 ## 替换为 USTC 地址
@@ -153,7 +142,7 @@ http://mirrors.ustc.edu.cn/debian
```bash ```bash
## 编辑 debian-security.list 配置文件 ## 编辑 debian-security.list 配置文件
sudo vim /etc/apt/mirrors/debian-security.list $ sudo vim /etc/apt/mirrors/debian-security.list
## 替换为 USTC 地址 ## 替换为 USTC 地址
@@ -168,29 +157,29 @@ http://mirrors.ustc.edu.cn/debian-security
```bash ```bash
## 清理不必要的包 ## 清理不必要的包
sudo apt clean && sudo apt autoclean && sudo apt autoremove --purge $ sudo apt clean && sudo apt autoclean && sudo apt autoremove --purge
## 更新软件源 ## 更新软件源
sudo apt update $ sudo apt update
## 更新系统 ## 更新系统
sudo apt dist-upgrade $ sudo apt dist-upgrade
``` ```
接下来安装系统必要软件,安装 `iperf3` 后,系统将询问是否将其作为系统服务开机自启,选择 `no` 即可。 接下来安装系统必要软件,安装 `iperf3` 后,系统将询问是否将其作为系统服务开机自启,选择 `no` 即可。
```bash ```bash
## 安装系统软件 ## 安装系统软件
sudo apt install qemu-guest-agent zsh git htop lm-sensors tmux cron nftables sshguard $ sudo apt install qemu-guest-agent zsh git htop lm-sensors tmux cron nftables sshguard
## 安装系统自动更新工具 ## 安装系统自动更新工具
sudo apt install unattended-upgrades powermgmt-base python3-gi $ sudo apt install unattended-upgrades powermgmt-base python3-gi
## 安装网络工具 ## 安装网络工具
sudo apt install iperf iperf3 iftop lsof ldnsutils $ sudo apt install iperf iperf3 iftop lsof ldnsutils
## 写入磁盘 ## 写入磁盘
sudo sync $ sudo sync
``` ```
### 1.4.调整内核参数 ### 1.4.调整内核参数
@@ -201,7 +190,7 @@ sudo sync
```bash ```bash
## 编辑 内核参数 配置文件 ## 编辑 内核参数 配置文件
sudo vim /etc/sysctl.d/99-sysctl.conf $ sudo vim /etc/sysctl.d/99-sysctl.conf
``` ```
在配置文件末尾输入以下配置项,注意配置中间的空格。 在配置文件末尾输入以下配置项,注意配置中间的空格。
@@ -237,7 +226,7 @@ net.ipv6.conf.default.use_tempaddr = 0
```bash ```bash
## 让内核参数生效 ## 让内核参数生效
sudo sysctl -f $ sudo sysctl -f
``` ```
### 1.5.调整系统时间 ### 1.5.调整系统时间
@@ -246,16 +235,13 @@ sudo sysctl -f
```bash ```bash
## 设置系统时区 ## 设置系统时区
sudo timedatectl set-timezone Asia/Shanghai $ sudo timedatectl set-timezone Asia/Shanghai
## 检查系统时间 ## 检查系统时间
date -R $ date -R
## 参考输出
#### 系统时间示例输出
Mon, 26 Jun 2023 16:16:16 +0800 Mon, 26 Jun 2023 16:16:16 +0800
``` ```
Debian 云镜像默认使用 `systemd-timesyncd.service` 同步时间,且需要调整为使用国内 NTP 服务器。 Debian 云镜像默认使用 `systemd-timesyncd.service` 同步时间,且需要调整为使用国内 NTP 服务器。
@@ -264,10 +250,10 @@ Debian 云镜像默认使用 `systemd-timesyncd.service` 同步时间,且需
```bash ```bash
## 创建 NTP 配置文件的文件夹 ## 创建 NTP 配置文件的文件夹
sudo mkdir /etc/systemd/timesyncd.conf.d $ sudo mkdir /etc/systemd/timesyncd.conf.d
## 创建 NTP 配置文件 ## 创建 NTP 配置文件
sudo vim /etc/systemd/timesyncd.conf.d/server_ntp.conf $ sudo vim /etc/systemd/timesyncd.conf.d/server_ntp.conf
``` ```
在配置文件中添加以下配置项,并保存。 在配置文件中添加以下配置项,并保存。
@@ -284,17 +270,16 @@ NTP=ntp.tencent.com ntp.aliyun.com
```bash ```bash
## 重启 chrony 服务 ## 重启 chrony 服务
sudo systemctl restart systemd-timesyncd.service $ sudo systemctl restart systemd-timesyncd.service
## 检查系统 NTP 服务器 ## 检查系统 NTP 服务器
sudo systemctl status systemd-timesyncd.service $ sudo systemctl status systemd-timesyncd.service
``` ```
如果输出以下类似内容,则表示系统 NTP 服务设置正确。 如果输出以下类似内容,则表示系统 NTP 服务设置正确。
```bash ```bash
## NTP 服务示例输出 #### NTP 服务示例输出
● systemd-timesyncd.service - Network Time Synchronization ● systemd-timesyncd.service - Network Time Synchronization
Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; preset: enabled) Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; preset: enabled)
Active: active (running) since Mon 2023-06-26 16:16:00 CST; 16s ago Active: active (running) since Mon 2023-06-26 16:16:00 CST; 16s ago
@@ -311,7 +296,6 @@ Jun 26 16:48:00 DNST01 systemd[1]: Starting systemd-timesyncd.service - Network
Jun 26 16:48:00 DNST01 systemd[1]: Started systemd-timesyncd.service - Network Time Synchronization. Jun 26 16:48:00 DNST01 systemd[1]: Started systemd-timesyncd.service - Network Time Synchronization.
Jun 26 16:48:00 DNST01 systemd-timesyncd[18829]: Contacted time server 106.55.184.199:123 (ntp.tencent.com). Jun 26 16:48:00 DNST01 systemd-timesyncd[18829]: Contacted time server 106.55.184.199:123 (ntp.tencent.com).
Jun 26 16:48:00 DNST01 systemd-timesyncd[18829]: Initial clock synchronization to Mon 2023-06-26 16:16:16.000000 CST. Jun 26 16:48:00 DNST01 systemd-timesyncd[18829]: Initial clock synchronization to Mon 2023-06-26 16:16:16.000000 CST.
``` ```
### 1.6.配置自动更新 ### 1.6.配置自动更新
@@ -322,29 +306,26 @@ Jun 26 16:48:00 DNST01 systemd-timesyncd[18829]: Initial clock synchronization t
```bash ```bash
## 检查系统定时器 ## 检查系统定时器
sudo systemctl status apt-daily-upgrade.timer $ sudo systemctl status apt-daily-upgrade.timer
``` ```
配置系统自动更新策略,执行以下命令,使用键盘 `左右方向键` 进行选择,`回车键` 进行确认。 配置系统自动更新策略,执行以下命令,使用键盘 `左右方向键` 进行选择,`回车键` 进行确认。
```bash ```bash
## 配置自动更新策略 ## 配置自动更新策略
sudo dpkg-reconfigure -plow unattended-upgrades $ sudo dpkg-reconfigure -plow unattended-upgrades
## 选择 “是” (“YES”) ## 选择 “是” (“YES”)
#### 系统自动更新示例输出
## 示例输出
Creating config file /etc/apt/apt.conf.d/20auto-upgrades with new version Creating config file /etc/apt/apt.conf.d/20auto-upgrades with new version
``` ```
接下来调整 apt 的 `20auto-upgrades` 配置文件。 接下来调整 apt 的 `20auto-upgrades` 配置文件。
```bash ```bash
## 编辑 20auto-upgrades 配置文件 ## 编辑 20auto-upgrades 配置文件
sudo vim /etc/apt/apt.conf.d/20auto-upgrades $ sudo vim /etc/apt/apt.conf.d/20auto-upgrades
``` ```
删除里面全部内容,添加以下配置项,并保存。 删除里面全部内容,添加以下配置项,并保存。
@@ -365,7 +346,7 @@ APT::Periodic::CleanInterval "1";
```bash ```bash
## 编辑 50unattended-upgrades 配置文件 ## 编辑 50unattended-upgrades 配置文件
sudo vim /etc/apt/apt.conf.d/50unattended-upgrades $ sudo vim /etc/apt/apt.conf.d/50unattended-upgrades
``` ```
根据 “注释” 中相关说明,调整配置文件。 根据 “注释” 中相关说明,调整配置文件。
@@ -377,7 +358,6 @@ sudo vim /etc/apt/apt.conf.d/50unattended-upgrades
"origin=Debian,codename=${distro_codename}-updates"; "origin=Debian,codename=${distro_codename}-updates";
## 在配置文件末尾增加以下配置项,代表启用,并调整参数 ## 在配置文件末尾增加以下配置项,代表启用,并调整参数
Unattended-Upgrade::AutoFixInterruptedDpkg "true"; Unattended-Upgrade::AutoFixInterruptedDpkg "true";
@@ -398,7 +378,7 @@ Unattended-Upgrade::Automatic-Reboot-Time "04:30";
```bash ```bash
## 配置系统定时器 ## 配置系统定时器
sudo systemctl edit apt-daily-upgrade.timer $ sudo systemctl edit apt-daily-upgrade.timer
``` ```
根据配置文件中的提示,在中间空白处填入以下配置项。 根据配置文件中的提示,在中间空白处填入以下配置项。
@@ -419,10 +399,10 @@ RandomizedDelaySec=0
```bash ```bash
## 重启触发器 ## 重启触发器
sudo systemctl restart apt-daily-upgrade.timer $ sudo systemctl restart apt-daily-upgrade.timer
## 再次检查触发器状态 ## 再次检查触发器状态
sudo systemctl status apt-daily-upgrade.timer $ sudo systemctl status apt-daily-upgrade.timer
``` ```
### 1.7.配置定时任务 ### 1.7.配置定时任务
@@ -431,13 +411,16 @@ sudo systemctl status apt-daily-upgrade.timer
```bash ```bash
## 查看系统定时任务 ## 查看系统定时任务
sudo crontab -l $ sudo crontab -l
## 编辑系统定时任务,编辑器选择 nano ## 编辑系统定时任务,编辑器选择 nano
sudo crontab -e $ sudo crontab -e
```
在配置文件末尾,增加以下配置项。
## 在配置文件末尾增加以下配置项 ```bash
## 定时任务配置项
0 6 8,24 * * /usr/sbin/reboot 0 6 8,24 * * /usr/sbin/reboot
@@ -449,16 +432,17 @@ sudo crontab -e
```bash ```bash
## 返回 home 目录 ## 返回 home 目录
cd $ cd
## 使用 curl 安装 oh-my-zsh ## 使用 curl 安装 oh-my-zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" $ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
## 或者使用 wget 安装 oh-my-zsh ## 或者使用 wget 安装 oh-my-zsh
sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)" $ sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
## 询问是否切换默认 shell,输入 Y ## 询问是否切换默认 shell,输入 Y
## 示例输出
#### 示例输出
Time to change your default shell to zsh: Time to change your default shell to zsh:
Do you want to change your default shell to zsh? [Y/n] y Do you want to change your default shell to zsh? [Y/n] y
``` ```
@@ -471,19 +455,19 @@ Debian 模板虚拟机已经配置完成,在将其转换为模板前需要对
```bash ```bash
## 清理系统软件包 ## 清理系统软件包
sudo apt clean && sudo apt autoclean && sudo apt autoremove --purge $ sudo apt clean && sudo apt autoclean && sudo apt autoremove --purge
## 清理系统缓存 ## 清理系统缓存
sudo rm -rvf /var/cache/apt/* /var/lib/apt/lists/* /tmp/* $ sudo rm -rvf /var/cache/apt/* /var/lib/apt/lists/* /tmp/*
## 清理系统日志 ## 清理系统日志
sudo find /var/log/ -type f | xargs sudo rm -rvf $ sudo find /var/log/ -type f | xargs sudo rm -rvf
## 清理命令历史记录文件 ## 清理命令历史记录文件
rm -rvf ~/.bash_history ~/.zsh_history $ rm -rvf ~/.bash_history ~/.zsh_history
## 清理命令历史 ## 清理命令历史
history -c $ history -c
``` ```
## 2.虚拟机转为模板 ## 2.虚拟机转为模板