diff --git a/05.PVE制作虚拟机模板.md b/05.PVE制作虚拟机模板.md index 5a104f1..85d4c35 100644 --- a/05.PVE制作虚拟机模板.md +++ b/05.PVE制作虚拟机模板.md @@ -14,25 +14,26 @@ 在虚拟机的命令行界面,使用 `vim` 编辑器编辑 `sshd` 服务的配置文件,执行以下命令。 -`vim` 编辑器常用操作如下。 +`vim` 编辑器常用操作如下: -- 在编辑文件时,按下键盘 `i` 键,进入编辑模式。 +- 编辑文件,按下键盘 `i` 键,进入编辑模式。 -- 在保存文件时,按下键盘 `Esc` 键,退出编辑模式,再输入组合键 `:wq` 即可保存。 +- 保存文件,按下键盘 `Esc` 键,退出编辑模式,再输入组合键 `:wq` 即可保存。 ```bash ## 编辑 ssh 配置文件 sudo vim /etc/ssh/sshd_config.d/server_sshd.conf ``` -在编辑器对话框中输入以下内容并保存,注意命令中间的空格。 +在配置文件中添加以下配置项,并保存。 ```bash +## SSH 配置项 + PasswordAuthentication yes - PermitEmptyPasswords no - UseDNS no + ``` 修改完成后,需要重启 SSH 服务。 @@ -104,8 +105,13 @@ https://deb.debian.org/debian-security ```bash ## 编辑 debian.sources 配置文件 sudo vim /etc/apt/sources.list.d/debian.sources +``` + +删除里面全部内容,添加以下配置项,并保存。 + +```bash +## 系统软件源配置项 -## 删除里面全部内容并填写以下内容 Types: deb URIs: mirror+file:///etc/apt/mirrors/debian.list Suites: bookworm bookworm-updates bookworm-backports @@ -115,9 +121,10 @@ Types: deb URIs: mirror+file:///etc/apt/mirrors/debian-security.list Suites: bookworm-security Components: main contrib non-free non-free-firmware + ``` -使用 `vim` 编辑器编辑 `debian.list` 和 `debian-security.list` 中镜像源地址。 +使用 `vim` 编辑器编辑 `debian.list` 中镜像源地址,并保存。 ```bash ## 编辑 debian.list 配置文件 @@ -125,7 +132,11 @@ sudo vim /etc/apt/mirrors/debian.list ## 替换为 USTC 地址 http://mirrors.ustc.edu.cn/debian +``` +使用 `vim` 编辑器编辑 `debian-security.list` 中镜像源地址,并保存。 + +```bash ## 编辑 debian-security.list 配置文件 sudo vim /etc/apt/mirrors/debian-security.list @@ -152,7 +163,7 @@ sudo apt dist-upgrade ```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 @@ -175,7 +186,7 @@ sudo sync sudo vim /etc/sysctl.d/99-sysctl.conf ``` -在配置文件末尾输入以下内容,注意命令中间的空格。 +在配置文件末尾输入以下配置项,注意配置中间的空格。 ```bash # 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.default.use_tempaddr = 0 - ``` 保存该配置文件后,重启系统或者执行以下命令让配置生效。 @@ -214,56 +224,49 @@ sudo sysctl -f ### 1.5.调整系统时间 -默认情况下的 Debian 云镜像的系统时间可能不正确,执行以下命令将系统时区设置为中国时区。 +默认情况下的 Debian 云镜像的系统时间需要调整,执行以下命令将系统时区设置为中国时区。 ```bash ## 设置系统时区 sudo timedatectl set-timezone Asia/Shanghai -``` -设置完成后,执行以下命令检查系统时间。 - -```bash -## 检查系统时区 +## 检查系统时间 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` 进行配置。 - -执行以下命令对 `chrony` 的配置文件进行修改。 +调整 NTP 服务器参数,逐行执行以下命令。 ```bash -## 编辑 chrony 配置文件 -sudo nano /etc/chrony/chrony.conf +## 创建 NTP 配置文件的文件夹 +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 -## chrony 服务配置文件示例 +## NTP 配置项 -# Use Debian vendor zone. -# pool 2.debian.pool.ntp.org iburst ## 在这行前面增加注释符 # 来注释 +[Time] +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 ## 重启 chrony 服务 -sudo systemctl restart chrony.service -``` +sudo systemctl restart systemd-timesyncd.service -再检查系统 NTP 服务器是否被正确修改。 - -```bash ## 检查系统 NTP 服务器 -chronyc sources -V +sudo systemctl status systemd-timesyncd.service ``` 如果输出以下类似内容,则表示系统 NTP 服务设置正确。 @@ -271,15 +274,28 @@ chronyc sources -V ```bash ## NTP 服务示例输出 -MS Name/IP address Stratum Poll Reach LastRx Last sample -=============================================================================== -^+ 139.199.215.251 2 10 177 244 -1669us[-1757us] +/- 67ms -^* 203.107.6.88 2 10 377 105 -1005us[-1094us] +/- 19ms +● systemd-timesyncd.service - Network Time Synchronization + 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 + 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.配置自动更新 -配置 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` 配置文件。 ```bash -## 进入 apt 的配置目录 -cd /etc/apt/apt.conf.d - ## 编辑 20auto-upgrades 配置文件 -sudo nano 20auto-upgrades - -## 删除里面全部内容并填写以下内容 -APT::Periodic::Update-Package-Lists "1"; -APT::Periodic::Unattended-Upgrade "5"; -APT::Periodic::AutocleanInterval "1"; -APT::Periodic::CleanInterval "1"; +sudo vim /etc/apt/apt.conf.d/20auto-upgrades ``` -其中,用来控制系统更新周期的为 `APT::Periodic::Unattended-Upgrade` 这行内容,其中参数 `"5"` 表示更新周期为 `5` 天。 +删除里面全部内容,添加以下配置项,并保存。 -再调整 apt 的 `50unattended-upgrades` 配置文件,所有修改项目汇聚如下。 +配置文件中,用来控制更新周期的参数为 `APT::Periodic::Unattended-Upgrade` ,`3` 表示更新周期为 `3` 天。 ```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 配置文件 -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"; -## 删除以下行前面的注释符,代表启用,并调整参数 + +## 在配置文件末尾增加以下配置项,代表启用,并调整参数 + Unattended-Upgrade::AutoFixInterruptedDpkg "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-Time "04:30"; + ``` -因为该配置文件很长,文章中留下一份 Debian 中已配置好的文件 [debian_50unattended_upgrades.conf](./src/debian_50unattended_upgrades.conf) ,以便对比。 - -两个配置文件均修改完成后,需要重设系统自动更新触发器,执行以下命令。 +系统自动更新配置文件修改完成后,需要重设自动更新定时器,执行以下命令。 ```bash -## 重设自动更新触发器时间为凌晨02:00 +## 配置系统定时器 sudo systemctl edit apt-daily-upgrade.timer +``` + +根据配置文件中的提示,在文件中间空白处填入一下配置项。 + +```bash +## 定时器配置项 -## 根据文件中的提示,在中间空白处填入以下内容 [Timer] OnCalendar= OnCalendar=02:00 RandomizedDelaySec=0 + ``` -重启触发器,并检查其状态,执行以下命令。 +设置完成后,重启自动更新定时器并检查其状态,执行以下命令。 + +在输出结果中看到系统自动更新的触发时间为凌晨 `02:00` 则表示设置正确。 ```bash ## 重启触发器 @@ -368,8 +400,6 @@ sudo systemctl restart apt-daily-upgrade.timer sudo systemctl status apt-daily-upgrade.timer ``` -在输出结果中看到系统自动更新的触发时间为凌晨 `02:00` 则表示设置正确。 - ### 1.7.配置定时任务 本步骤为可选操作,主要设置定时重启系统。 diff --git a/src/debian_20auto-upgrades.conf b/src/debian_20auto-upgrades.conf new file mode 100644 index 0000000..3553ff1 --- /dev/null +++ b/src/debian_20auto-upgrades.conf @@ -0,0 +1,5 @@ +APT::Periodic::Update-Package-Lists "1"; +APT::Periodic::Unattended-Upgrade "3"; +APT::Periodic::AutocleanInterval "1"; +APT::Periodic::CleanInterval "1"; + diff --git a/src/debian_50unattended_upgrades.conf b/src/debian_50unattended_upgrades.conf index 3750991..81d7953 100644 --- a/src/debian_50unattended_upgrades.conf +++ b/src/debian_50unattended_upgrades.conf @@ -68,7 +68,7 @@ Unattended-Upgrade::Package-Blacklist { // unattended-upgrades will automatically run // dpkg --force-confold --configure -a // 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 // 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 // (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 -Unattended-Upgrade::Remove-New-Unused-Dependencies "true"; +//Unattended-Upgrade::Remove-New-Unused-Dependencies "true"; // Do automatic removal of unused packages after the upgrade // (equivalent to apt-get autoremove) -Unattended-Upgrade::Remove-Unused-Dependencies "true"; +//Unattended-Upgrade::Remove-Unused-Dependencies "false"; // Automatically reboot *WITHOUT CONFIRMATION* if // 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 // 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 // time instead of immediately // 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 // speed to 70kb/sec @@ -161,4 +161,17 @@ Unattended-Upgrade::Automatic-Reboot-Time "04:30"; // Disabling the fallback speeds up unattended-upgrades when there are // uninstallable packages at the expense of rarely keeping back packages which // could be upgraded or installed. -// Unattended-Upgrade::Allow-APT-Mark-Fallback "true"; \ No newline at end of file +// 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"; + diff --git a/src/debian_dhcpcd_ipv6.conf b/src/debian_dhcpcd_ipv6.conf deleted file mode 100644 index f5486d9..0000000 --- a/src/debian_dhcpcd_ipv6.conf +++ /dev/null @@ -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