更新 PVE 制作虚拟机模板

This commit is contained in:
CallMeR
2023-06-26 17:38:44 +08:00
parent 6e01bd7a6b
commit 2cc4e7a81c
4 changed files with 121 additions and 97 deletions
+96 -66
View File
@@ -14,25 +14,26 @@
在虚拟机的命令行界面,使用 `vim` 编辑器编辑 `sshd` 服务的配置文件,执行以下命令。 在虚拟机的命令行界面,使用 `vim` 编辑器编辑 `sshd` 服务的配置文件,执行以下命令。
`vim` 编辑器常用操作如下 `vim` 编辑器常用操作如下
- 编辑文件,按下键盘 `i` 键,进入编辑模式。 - 编辑文件,按下键盘 `i` 键,进入编辑模式。
- 保存文件,按下键盘 `Esc` 键,退出编辑模式,再输入组合键 `:wq` 即可保存。 - 保存文件,按下键盘 `Esc` 键,退出编辑模式,再输入组合键 `:wq` 即可保存。
```bash ```bash
## 编辑 ssh 配置文件 ## 编辑 ssh 配置文件
sudo vim /etc/ssh/sshd_config.d/server_sshd.conf sudo vim /etc/ssh/sshd_config.d/server_sshd.conf
``` ```
编辑器对话框中输入以下内容并保存,注意命令中间的空格 配置文件中添加以下配置项,并保存
```bash ```bash
## SSH 配置项
PasswordAuthentication yes PasswordAuthentication yes
PermitEmptyPasswords no PermitEmptyPasswords no
UseDNS no UseDNS no
``` ```
修改完成后,需要重启 SSH 服务。 修改完成后,需要重启 SSH 服务。
@@ -104,8 +105,13 @@ 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
```
删除里面全部内容,添加以下配置项,并保存。
```bash
## 系统软件源配置项
## 删除里面全部内容并填写以下内容
Types: deb Types: deb
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
@@ -115,9 +121,10 @@ Types: deb
URIs: mirror+file:///etc/apt/mirrors/debian-security.list URIs: mirror+file:///etc/apt/mirrors/debian-security.list
Suites: bookworm-security Suites: bookworm-security
Components: main contrib non-free non-free-firmware Components: main contrib non-free non-free-firmware
``` ```
使用 `vim` 编辑器编辑 `debian.list``debian-security.list` 中镜像源地址。 使用 `vim` 编辑器编辑 `debian.list` 中镜像源地址,并保存
```bash ```bash
## 编辑 debian.list 配置文件 ## 编辑 debian.list 配置文件
@@ -125,7 +132,11 @@ sudo vim /etc/apt/mirrors/debian.list
## 替换为 USTC 地址 ## 替换为 USTC 地址
http://mirrors.ustc.edu.cn/debian http://mirrors.ustc.edu.cn/debian
```
使用 `vim` 编辑器编辑 `debian-security.list` 中镜像源地址,并保存。
```bash
## 编辑 debian-security.list 配置文件 ## 编辑 debian-security.list 配置文件
sudo vim /etc/apt/mirrors/debian-security.list sudo vim /etc/apt/mirrors/debian-security.list
@@ -152,7 +163,7 @@ sudo apt dist-upgrade
```bash ```bash
## 安装系统软件 ## 安装系统软件
sudo apt install qemu-guest-agent zsh git htop lm-sensors tmux nftables sshguard sudo apt install qemu-guest-agent zsh git htop lm-sensors tmux cron nftables sshguard
## 安装系统自动更新工具 ## 安装系统自动更新工具
sudo apt install unattended-upgrades apt-listchanges powermgmt-base python3-gi sudo apt install unattended-upgrades apt-listchanges powermgmt-base python3-gi
@@ -175,7 +186,7 @@ sudo sync
sudo vim /etc/sysctl.d/99-sysctl.conf sudo vim /etc/sysctl.d/99-sysctl.conf
``` ```
在配置文件末尾输入以下内容,注意命令中间的空格。 在配置文件末尾输入以下配置项,注意配置中间的空格。
```bash ```bash
# This configuration file is customized by fox # This configuration file is customized by fox
@@ -202,7 +213,6 @@ net.ipv4.tcp_syncookies = 1
net.ipv6.conf.all.use_tempaddr = 0 net.ipv6.conf.all.use_tempaddr = 0
net.ipv6.conf.default.use_tempaddr = 0 net.ipv6.conf.default.use_tempaddr = 0
``` ```
保存该配置文件后,重启系统或者执行以下命令让配置生效。 保存该配置文件后,重启系统或者执行以下命令让配置生效。
@@ -214,56 +224,49 @@ sudo sysctl -f
### 1.5.调整系统时间 ### 1.5.调整系统时间
默认情况下的 Debian 云镜像的系统时间可能不正确,执行以下命令将系统时区设置为中国时区。 默认情况下的 Debian 云镜像的系统时间需要调整,执行以下命令将系统时区设置为中国时区。
```bash ```bash
## 设置系统时区 ## 设置系统时区
sudo timedatectl set-timezone Asia/Shanghai sudo timedatectl set-timezone Asia/Shanghai
```
设置完成后,执行以下命令检查系统时间 ## 检查系统时间
```bash
## 检查系统时区
date -R date -R
## 参考输出
Mon, 26 Jun 2023 16:16:16 +0800
``` ```
Debian 普通镜像常`systemd-timesyncd.service` 同步时间,而 Debian 云镜像使用 `chrony.service` 来同步时间 Debian 云镜像默认使`systemd-timesyncd.service` 同步时间,且需要调整为使用国内 NTP 服务器
为了使用国内的 NTP 服务器,需要对 `chrony.service` 进行配置 调整 NTP 服务器参数,逐行执行以下命令
执行以下命令对 `chrony` 的配置文件进行修改。
```bash ```bash
## 编辑 chrony 配置文件 ## 创建 NTP 配置文件的文件夹
sudo nano /etc/chrony/chrony.conf sudo mkdir /etc/systemd/timesyncd.conf.d
## 创建 NTP 配置文件
sudo vim /etc/systemd/timesyncd.conf.d/server_ntp.conf
``` ```
编辑器对话框中,将 `pool 2.debian.pool.ntp.org iburst` 这行内容 “注释” 掉,并添加国内的 NTP 服务器,参考如下内容 配置文件中添加以下配置项,并保存
```bash ```bash
## chrony 服务配置文件示例 ## NTP 配置项
# Use Debian vendor zone. [Time]
# pool 2.debian.pool.ntp.org iburst ## 在这行前面增加注释符 # 来注释 NTP=ntp.tencent.com ntp.aliyun.com
# Use Custom vendor zone.
pool ntp.tencent.com iburst
pool ntp.aliyun.com iburst
``` ```
保存该配置文件后,重启 `chrony` 服务 保存该配置文件后,重启 `systemd-timesyncd.service` 服务,并再次检查系统 NTP 服务器地址
```bash ```bash
## 重启 chrony 服务 ## 重启 chrony 服务
sudo systemctl restart chrony.service sudo systemctl restart systemd-timesyncd.service
```
再检查系统 NTP 服务器是否被正确修改。
```bash
## 检查系统 NTP 服务器 ## 检查系统 NTP 服务器
chronyc sources -V sudo systemctl status systemd-timesyncd.service
``` ```
如果输出以下类似内容,则表示系统 NTP 服务设置正确。 如果输出以下类似内容,则表示系统 NTP 服务设置正确。
@@ -271,15 +274,28 @@ chronyc sources -V
```bash ```bash
## NTP 服务示例输出 ## NTP 服务示例输出
MS Name/IP address Stratum Poll Reach LastRx Last sample ● systemd-timesyncd.service - Network Time Synchronization
=============================================================================== Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; preset: enabled)
^+ 139.199.215.251 2 10 177 244 -1669us[-1757us] +/- 67ms Active: active (running) since Mon 2023-06-26 16:16:00 CST; 16s ago
^* 203.107.6.88 2 10 377 105 -1005us[-1094us] +/- 19ms Docs: man:systemd-timesyncd.service(8)
Main PID: 18829 (systemd-timesyn)
Status: "Contacted time server 106.55.184.199:123 (ntp.tencent.com)."
Tasks: 2 (limit: 2355)
Memory: 1.4M
CPU: 37ms
CGroup: /system.slice/systemd-timesyncd.service
└─18829 /lib/systemd/systemd-timesyncd
Jun 26 16:48:00 DNST01 systemd[1]: Starting 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]: Initial clock synchronization to Mon 2023-06-26 16:16:16.000000 CST.
``` ```
### 1.6.配置自动更新 ### 1.6.配置自动更新
配置 Debian 模板虚拟机的系统自动更新,与配置 PVE 系统自动更新方法基本一致,参阅 [03.PVE系统调整](./03.PVE系统调整.md) 。 配置 Debian 云镜像的系统自动更新,与配置 PVE 系统自动更新方法基本一致,参阅 [03.PVE系统调整](./03.PVE系统调整.md) 。
配置系统更新之前,先检查当前系统定时器状态。 配置系统更新之前,先检查当前系统定时器状态。
@@ -302,34 +318,43 @@ Creating config file /etc/apt/apt.conf.d/20auto-upgrades with new version
接下来调整 apt 的 `20auto-upgrades` 配置文件。 接下来调整 apt 的 `20auto-upgrades` 配置文件。
```bash ```bash
## 进入 apt 的配置目录
cd /etc/apt/apt.conf.d
## 编辑 20auto-upgrades 配置文件 ## 编辑 20auto-upgrades 配置文件
sudo nano 20auto-upgrades sudo vim /etc/apt/apt.conf.d/20auto-upgrades
## 删除里面全部内容并填写以下内容
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "5";
APT::Periodic::AutocleanInterval "1";
APT::Periodic::CleanInterval "1";
``` ```
其中,用来控制系统更新周期的为 `APT::Periodic::Unattended-Upgrade` 这行内容,其中参数 `"5"` 表示更新周期为 `5` 删除里面全部内容,添加以下配置项,并保存
再调整 apt 的 `50unattended-upgrades` 配置文件,所有修改项目汇聚如下 配置文件中,用来控制更新周期的参数为 `APT::Periodic::Unattended-Upgrade` `3` 表示更新周期为 `3`
```bash ```bash
## 进入 apt 的配置目录 ## 系统更新周期配置项
cd /etc/apt/apt.conf.d
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "3";
APT::Periodic::AutocleanInterval "1";
APT::Periodic::CleanInterval "1";
```
进一步调整 apt 的 `50unattended-upgrades` 配置文件。
```bash
## 编辑 50unattended-upgrades 配置文件 ## 编辑 50unattended-upgrades 配置文件
sudo nano 50unattended-upgrades sudo vim /etc/apt/apt.conf.d/50unattended-upgrades
```
根据 “注释” 中相关说明,调整配置文件。
因为该配置文件很长,完整的配置文件可查看 [debian_50unattended_upgrades.conf](./src/debian_50unattended_upgrades.conf) 以便对比。
```bash
## 删除以下行前面的注释符 // ,代表启用 ## 删除以下行前面的注释符 // ,代表启用
"origin=Debian,codename=${distro_codename}-updates"; "origin=Debian,codename=${distro_codename}-updates";
## 删除以下行前面的注释符,代表启用,并调整参数
## 在配置文件末尾增加以下配置项,代表启用,并调整参数
Unattended-Upgrade::AutoFixInterruptedDpkg "true"; Unattended-Upgrade::AutoFixInterruptedDpkg "true";
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true"; Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
@@ -341,24 +366,31 @@ Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "true"; Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:30"; Unattended-Upgrade::Automatic-Reboot-Time "04:30";
``` ```
因为该配置文件很长,文章中留下一份 Debian 中已配置好的文件 [debian_50unattended_upgrades.conf](./src/debian_50unattended_upgrades.conf) ,以便对比 系统自动更新配置文件修改完成后,需要重设自动更新定时器,执行以下命令
两个配置文件均修改完成后,需要重设系统自动更新触发器,执行以下命令。
```bash ```bash
## 重设自动更新触发器时间为凌晨02:00 ## 配置系统定时器
sudo systemctl edit apt-daily-upgrade.timer sudo systemctl edit apt-daily-upgrade.timer
```
根据配置文件中的提示,在文件中间空白处填入一下配置项。
```bash
## 定时器配置项
## 根据文件中的提示,在中间空白处填入以下内容
[Timer] [Timer]
OnCalendar= OnCalendar=
OnCalendar=02:00 OnCalendar=02:00
RandomizedDelaySec=0 RandomizedDelaySec=0
``` ```
重启触发器,并检查其状态,执行以下命令。 设置完成后,重启自动更新定时器并检查其状态,执行以下命令。
在输出结果中看到系统自动更新的触发时间为凌晨 `02:00` 则表示设置正确。
```bash ```bash
## 重启触发器 ## 重启触发器
@@ -368,8 +400,6 @@ sudo systemctl restart apt-daily-upgrade.timer
sudo systemctl status apt-daily-upgrade.timer sudo systemctl status apt-daily-upgrade.timer
``` ```
在输出结果中看到系统自动更新的触发时间为凌晨 `02:00` 则表示设置正确。
### 1.7.配置定时任务 ### 1.7.配置定时任务
本步骤为可选操作,主要设置定时重启系统。 本步骤为可选操作,主要设置定时重启系统。
+5
View File
@@ -0,0 +1,5 @@
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "3";
APT::Periodic::AutocleanInterval "1";
APT::Periodic::CleanInterval "1";
+19 -6
View File
@@ -68,7 +68,7 @@ Unattended-Upgrade::Package-Blacklist {
// unattended-upgrades will automatically run // unattended-upgrades will automatically run
// dpkg --force-confold --configure -a // dpkg --force-confold --configure -a
// The default is true, to ensure updates keep getting installed // The default is true, to ensure updates keep getting installed
Unattended-Upgrade::AutoFixInterruptedDpkg "true"; //Unattended-Upgrade::AutoFixInterruptedDpkg "true";
// Split the upgrade into the smallest possible chunks so that // Split the upgrade into the smallest possible chunks so that
// they can be interrupted with SIGTERM. This makes the upgrade // they can be interrupted with SIGTERM. This makes the upgrade
@@ -101,18 +101,18 @@ Unattended-Upgrade::AutoFixInterruptedDpkg "true";
// Remove unused automatically installed kernel-related packages // Remove unused automatically installed kernel-related packages
// (kernel images, kernel headers and kernel version locked tools). // (kernel images, kernel headers and kernel version locked tools).
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true"; //Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
// Do automatic removal of newly unused dependencies after the upgrade // Do automatic removal of newly unused dependencies after the upgrade
Unattended-Upgrade::Remove-New-Unused-Dependencies "true"; //Unattended-Upgrade::Remove-New-Unused-Dependencies "true";
// Do automatic removal of unused packages after the upgrade // Do automatic removal of unused packages after the upgrade
// (equivalent to apt-get autoremove) // (equivalent to apt-get autoremove)
Unattended-Upgrade::Remove-Unused-Dependencies "true"; //Unattended-Upgrade::Remove-Unused-Dependencies "false";
// Automatically reboot *WITHOUT CONFIRMATION* if // Automatically reboot *WITHOUT CONFIRMATION* if
// the file /var/run/reboot-required is found after the upgrade // the file /var/run/reboot-required is found after the upgrade
Unattended-Upgrade::Automatic-Reboot "true"; //Unattended-Upgrade::Automatic-Reboot "false";
// Automatically reboot even if there are users currently logged in // Automatically reboot even if there are users currently logged in
// when Unattended-Upgrade::Automatic-Reboot is set to true // when Unattended-Upgrade::Automatic-Reboot is set to true
@@ -121,7 +121,7 @@ Unattended-Upgrade::Automatic-Reboot "true";
// If automatic reboot is enabled and needed, reboot at the specific // If automatic reboot is enabled and needed, reboot at the specific
// time instead of immediately // time instead of immediately
// Default: "now" // Default: "now"
Unattended-Upgrade::Automatic-Reboot-Time "04:30"; //Unattended-Upgrade::Automatic-Reboot-Time "02:00";
// Use apt bandwidth limit feature, this example limits the download // Use apt bandwidth limit feature, this example limits the download
// speed to 70kb/sec // speed to 70kb/sec
@@ -162,3 +162,16 @@ Unattended-Upgrade::Automatic-Reboot-Time "04:30";
// uninstallable packages at the expense of rarely keeping back packages which // uninstallable packages at the expense of rarely keeping back packages which
// could be upgraded or installed. // could be upgraded or installed.
// Unattended-Upgrade::Allow-APT-Mark-Fallback "true"; // Unattended-Upgrade::Allow-APT-Mark-Fallback "true";
Unattended-Upgrade::AutoFixInterruptedDpkg "true";
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-New-Unused-Dependencies "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:30";
-24
View File
@@ -1,24 +0,0 @@
# This configuration file is customized by fox
# Optimize dhcpcd parameters for dns server
# Only configure IPv6
ipv6only
# Inform the DHCP server of our hostname for DDNS.
hostname
# A list of options to request from the DHCP server.
nooption domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Respect the network MTU.
option interface_mtu
# A ServerID is required by RFC2131.
require dhcp_server_identifier
# Generate Stable Private IPv6 Addresses based from the DUID
slaac private
# Don't send any ARP requests.
noarp