From db464c6e83eaac2ef6cfcf22ca46259de49587f4 Mon Sep 17 00:00:00 2001 From: CallMeR <9463297+callmer@user.noreply.gitee.com> Date: Mon, 26 Jun 2023 15:21:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20PVE=20=E5=88=B6=E4=BD=9C?= =?UTF-8?q?=E8=99=9A=E6=8B=9F=E6=9C=BA=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.PVE制作虚拟机模板.md | 305 +++++--------------------------------- 06.PVE用模板克隆虚拟机.md | 52 ++++++- 2 files changed, 92 insertions(+), 265 deletions(-) diff --git a/05.PVE制作虚拟机模板.md b/05.PVE制作虚拟机模板.md index c6c7c11..5a104f1 100644 --- a/05.PVE制作虚拟机模板.md +++ b/05.PVE制作虚拟机模板.md @@ -1,4 +1,4 @@ -## 1.配置Debian系统 +## 1.配置系统 在上一篇文章 [04.PVE创建模板虚拟机](./04.PVE创建模板虚拟机.md) 中,已经创建好了用于制作虚拟机模板的模板虚拟机。 @@ -6,7 +6,7 @@ 此时将模板虚拟机开机后,使用 `Cloud-Init` 中设置的账号密码进行登录。 -### 1.1.启用SSH密码登录 +### 1.1.配置 SSH 因为 Debian 的云镜像默认使用 SSH 秘钥登录,因此切换到左侧菜单的 `控制台` 进行登录。 @@ -44,13 +44,13 @@ sudo systemctl restart ssh.service ### 1.2.配置软件源 -使用 SSH 工具登录模板虚拟机,常用 SSH 工具可以看之前文章中的 [SSH 工具](./01.PVE系统安装.md) 。 +使用 SSH 工具登录模板虚拟机,常用 SSH 工具请参阅 [01.PVE系统安装](./01.PVE系统安装.md) 。 -首先需要对 Debian 系统的软件源进行修改,这里使用 USTC 镜像站作为演示。 +首先需要对 Debian 系统软件源进行修改,这里使用 [USTC](http://mirrors.ustc.edu.cn/help/debian.html) 镜像站作为演示。 当系统版本发生变化时,请参考使用 snullp 大叔开发的 [配置生成器](https://mirrors.ustc.edu.cn/repogen/) 。 -Debian12 采用了不同的软件源配置方法,常规 `sources.list` 配置文件内容如下。 +Debian12 云镜像的软件源配置采用了 `DEB822` 格式,新版 `sources.list` 配置文件内容如下。 ```bash ## 进入系统源配置文件目录 @@ -133,7 +133,9 @@ sudo vim /etc/apt/mirrors/debian-security.list http://mirrors.ustc.edu.cn/debian-security ``` -软件源设置完成后,需要更新系统,逐条执行以下命令。 +### 1.3.安装软件 + +软件源设置完成后,需要更新系统,逐行执行以下命令。 ```bash ## 清理不必要的包 @@ -146,9 +148,7 @@ sudo apt update sudo apt dist-upgrade ``` -接下来安装系统必要软件,逐条执行以下命令。 - -安装 `iperf3` 后,系统将询问是否将其作为系统服务开机自启,选择 `no` 即可。 +接下来安装系统必要软件,安装 `iperf3` 后,系统将询问是否将其作为系统服务开机自启,选择 `no` 即可。 ```bash ## 安装系统软件 @@ -158,127 +158,24 @@ sudo apt install qemu-guest-agent zsh git htop lm-sensors tmux nftables sshguard sudo apt install unattended-upgrades apt-listchanges powermgmt-base python3-gi ## 安装网络工具 -sudo apt install iperf iperf3 iftop lsof ldnsutils dhcpcd +sudo apt install iperf iperf3 iftop lsof ldnsutils ## 写入磁盘 sudo sync ``` -Adguard Home 将采用 `snap` 形式安装,Adguard Home 安装完成后,先停止其服务: +### 1.4.调整内核参数 -```bash -## 安装 snap -sudo apt install snapd +由于该 Debian 虚拟机模板将用于克隆内网 DNS 服务器,因此需要调整内核参数来简单优化性能。 -## 安装 Adguard Home -sudo snap install adguard-home - -## 停止 Adguard Home -sudo snap stop adguard-home -``` - -### 1.3.配置网卡IPv4 - -使用 `Cloud-Init` 并给虚拟机分配了静态 IPv4 地址后,虚拟机的网卡还会从路由器的 DHCP 再获取一个 IPv4 地址。 - -这个功能在给虚拟机添加网卡设备时很有用,但本 Debian 虚拟机模板的使用场景只需要一个静态 IPv4 地址,因此需要禁用网卡自动获取 IPv4 地址的功能。 - -执行以下命令对网卡的配置文件进行修改: - -```bash -## 修改 Debian 网卡配置文件 -sudo nano /etc/network/interfaces -``` - -在 `nano` 编辑器对话框中,将 `source-directory /run/network/interfaces.d` 这行内容 “注释” 掉,参考如下内容: - -```bash -## 网卡配置文件示例 - -# Cloud images dynamically generate config fragments for newly -# attached interfaces. See /etc/udev/rules.d/75-cloud-ifupdown.rules -# and /etc/network/cloud-ifupdown-helper. Dynamically generated -# configuration fragments are stored in /run: -# source-directory /run/network/interfaces.d ## 在这行前面增加注释符 # 来注释 -``` - -将配置保存后,可以将模板虚拟机重启。 - -在系统重启完成后,执行以下命令检查系统的 IPv4 地址状态: - -```bash -## 检查系统 IP 地址状态 -ip a -``` - -如果系统网卡有且仅有一个 IPv4 地址,且该 IPv4 地址与 `Cloud-Init` 中设置的 IPv4 地址相同,则表示系统网卡已正确设置。 - -### 1.4.配置网卡IPv6 - -使用 `Cloud-Init` 并给虚拟机分配了静态 IPv6 地址后,该地址为私有 ULA 地址,此时虚拟机的网卡不会以 `SLAAC` 的方式配置 IPv6 地址。 - -如果上级路由器没有设置 `NAT66` ,且将公网 GUA 地址的 `Prefix` 通告到内网时,虚拟机将无法自动生成 GUA 地址,从而导致无法访问 IPv6 网络。 - -因此引入 `dhcpcd` 工具,在正确配置虚拟机 IPv6 地址的同时,保持与 `Cloud-Init` 网卡配置的兼容性。 - -执行以下命令对网卡的配置文件进行修改: - -```bash -## 备份 dhcpcd 配置文件 -sudo mv /etc/dhcpcd.conf /etc/dhcpcd.conf.bak - -## 修改 dhcpcd 配置文件 -sudo nano /etc/dhcpcd.conf -``` - -在编辑器对话框中输入以下内容,注意命令中间的空格: - -```bash -# 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 -``` - -保存该配置文件后,重启系统或者执行以下命令重启 `dhcpcd` 服务: - -```bash -## 重启 dhcpcd 服务 -sudo systemctl restart dhcpcd.service -``` - -### 1.5.调整系统内核参数 - -由于该 Debian 虚拟机模板将用于克隆内网 DNS 服务器,因此需要调整一部分系统内核参数来简单优化性能。 - -使用 `nano` 编辑器编辑 **内核参数** 配置文件,执行以下命令: +使用 `vim` 编辑器编辑 **内核参数** 配置文件,执行以下命令。 ```bash ## 编辑 内核参数 配置文件 -sudo nano /etc/sysctl.d/99-sysctl.conf +sudo vim /etc/sysctl.d/99-sysctl.conf ``` -在配置文件末尾输入以下内容,注意命令中间的空格: +在配置文件末尾输入以下内容,注意命令中间的空格。 ```bash # This configuration file is customized by fox @@ -308,23 +205,23 @@ net.ipv6.conf.default.use_tempaddr = 0 ``` -保存该配置文件后,重启系统或者执行以下命令让配置生效: +保存该配置文件后,重启系统或者执行以下命令让配置生效。 ```bash ## 让内核参数生效 sudo sysctl -f ``` -### 1.6.调整系统时间 +### 1.5.调整系统时间 -默认情况下的 Debian 云镜像的系统时间可能不正确,执行以下命令将系统时区设置为中国时区: +默认情况下的 Debian 云镜像的系统时间可能不正确,执行以下命令将系统时区设置为中国时区。 ```bash ## 设置系统时区 sudo timedatectl set-timezone Asia/Shanghai ``` -设置完成后,执行以下命令检查系统时间: +设置完成后,执行以下命令检查系统时间。 ```bash ## 检查系统时区 @@ -335,14 +232,14 @@ Debian 普通镜像常用 `systemd-timesyncd.service` 来同步时间,而 Debi 为了使用国内的 NTP 服务器,需要对 `chrony.service` 进行配置。 -执行以下命令对 `chrony` 的配置文件进行修改: +执行以下命令对 `chrony` 的配置文件进行修改。 ```bash ## 编辑 chrony 配置文件 sudo nano /etc/chrony/chrony.conf ``` -在编辑器对话框中,将 `pool 2.debian.pool.ntp.org iburst` 这行内容 “注释” 掉,并添加国内的 NTP 服务器,参考如下内容: +在编辑器对话框中,将 `pool 2.debian.pool.ntp.org iburst` 这行内容 “注释” 掉,并添加国内的 NTP 服务器,参考如下内容。 ```bash ## chrony 服务配置文件示例 @@ -355,21 +252,21 @@ pool ntp.tencent.com iburst pool ntp.aliyun.com iburst ``` -保存该配置文件后,重启 `chrony` 服务: +保存该配置文件后,重启 `chrony` 服务。 ```bash ## 重启 chrony 服务 sudo systemctl restart chrony.service ``` -再检查系统 NTP 服务器是否被正确修改: +再检查系统 NTP 服务器是否被正确修改。 ```bash ## 检查系统 NTP 服务器 chronyc sources -V ``` -如果输出以下类似内容,则表示系统 NTP 服务设置正确: +如果输出以下类似内容,则表示系统 NTP 服务设置正确。 ```bash ## NTP 服务示例输出 @@ -380,18 +277,18 @@ MS Name/IP address Stratum Poll Reach LastRx Last sample ^* 203.107.6.88 2 10 377 105 -1005us[-1094us] +/- 19ms ``` -### 1.7.配置系统自动更新 +### 1.6.配置自动更新 -配置 Debian 模板虚拟机的系统自动更新,与配置 PVE 系统自动更新方法基本一致,参阅 [PVE系统自动更新](./03.PVE系统调整.md#5pve系统自动更新) 。 +配置 Debian 模板虚拟机的系统自动更新,与配置 PVE 系统自动更新方法基本一致,参阅 [03.PVE系统调整](./03.PVE系统调整.md) 。 -配置系统更新之前,先检查当前系统定时器状态: +配置系统更新之前,先检查当前系统定时器状态。 ```bash ## 检查系统定时器 sudo systemctl status apt-daily-upgrade.timer ``` -配置系统自动更新策略,执行以下命令,使用键盘 `左右方向键` 进行选择,`回车键` 进行确认: +配置系统自动更新策略,执行以下命令,使用键盘 `左右方向键` 进行选择,`回车键` 进行确认。 ```bash ## 配置自动更新策略 @@ -402,7 +299,7 @@ sudo dpkg-reconfigure -plow unattended-upgrades Creating config file /etc/apt/apt.conf.d/20auto-upgrades with new version ``` -接下来调整 apt 的 `20auto-upgrades` 配置文件: +接下来调整 apt 的 `20auto-upgrades` 配置文件。 ```bash ## 进入 apt 的配置目录 @@ -420,7 +317,7 @@ APT::Periodic::CleanInterval "1"; 其中,用来控制系统更新周期的为 `APT::Periodic::Unattended-Upgrade` 这行内容,其中参数 `"5"` 表示更新周期为 `5` 天。 -再调整 apt 的 `50unattended-upgrades` 配置文件,所有修改项目汇聚如下: +再调整 apt 的 `50unattended-upgrades` 配置文件,所有修改项目汇聚如下。 ```bash ## 进入 apt 的配置目录 @@ -448,7 +345,7 @@ Unattended-Upgrade::Automatic-Reboot-Time "04:30"; 因为该配置文件很长,文章中留下一份 Debian 中已配置好的文件 [debian_50unattended_upgrades.conf](./src/debian_50unattended_upgrades.conf) ,以便对比。 -两个配置文件均修改完成后,需要重设系统自动更新触发器,执行以下命令: +两个配置文件均修改完成后,需要重设系统自动更新触发器,执行以下命令。 ```bash ## 重设自动更新触发器时间为凌晨02:00 @@ -461,7 +358,7 @@ OnCalendar=02:00 RandomizedDelaySec=0 ``` -重启触发器,并检查其状态,执行以下命令: +重启触发器,并检查其状态,执行以下命令。 ```bash ## 重启触发器 @@ -471,30 +368,11 @@ sudo systemctl restart apt-daily-upgrade.timer sudo systemctl status apt-daily-upgrade.timer ``` -在输出结果中看到系统自动更新的触发时间为凌晨 `02:00` 则表示设置正确。 +在输出结果中看到系统自动更新的触发时间为凌晨 `02:00` 则表示设置正确。 -### 1.8.配置Snap自动更新 +### 1.7.配置定时任务 -先查看 Snap 当前的更新策略,执行以下命令: - -```bash -## 显示当前 Snap 自动更新设置 -sudo snap refresh --time -``` - -将 Snap 自动更新时间设置为每天 `2:30-3:30` 和 `14:30-15:30` 两个时间段: - -```bash -## 修改 Snap 自动更新时间 -sudo snap set system refresh.timer=2:30-3:30,14:30-15:30 - -## 其他 Snap 自动更新时间设置语法参考 -sudo snap set system refresh.timer=mon,2:30,,fri,2:30 -``` - -### 1.9.配置系统定时任务 - -本步骤为可选操作,主要设置定时重启 Snap 服务以及操作系统。 +本步骤为可选操作,主要设置定时重启系统。 ```bash ## 查看系统定时任务 @@ -503,14 +381,13 @@ sudo crontab -l ## 编辑系统定时任务,编辑器选择 nano sudo crontab -e -## 定时重启 Snap 服务、操作系统 -0 5 * * * /usr/bin/snap restart adguard-home +## 定时重启系统 0 6 8,24 * * /usr/sbin/reboot ``` -### 1.10.配置ZSH +### 1.8.配置 ZSH -`Zsh` 是比 `Bash` 好用的 `Shell` 程序,使用 `oh-my-zsh` 进行配置: +`Zsh` 是比 `Bash` 好用的 `Shell` 程序,使用 `oh-my-zsh` 进行配置。 ```bash ## 返回 home 目录 @@ -528,11 +405,11 @@ Time to change your default shell to zsh: Do you want to change your default shell to zsh? [Y/n] y ``` -### 1.11.清理系统 +### 1.9.清理系统 Debian 模板虚拟机已经配置完成,在将其转换为模板前需要对系统进行清理。 -逐条执行以下命令,注意命令中的空格: +逐条执行以下命令,注意命令中的空格。 ```bash ## 清理系统软件包 @@ -555,7 +432,7 @@ history -c 将 Debian 模板虚拟机关机,并进入 PVE 的 WEB 管理界面。 -在左侧虚拟机列表中,鼠标 **右键单击** Debian 模板虚拟机,在弹出的菜单中选择 `转换成模板` : +在左侧虚拟机列表中,鼠标 **右键单击** Debian 模板虚拟机,在弹出的菜单中选择 `转换成模板` 。 ![虚拟机转模板](img/p05/vm_to_template.png) @@ -563,103 +440,3 @@ history -c 如果某些 Debian 系统的配置项有问题,只能在用该模板克隆出来的新虚拟机中进行修改,或者删除并重新制作模板。 -## 3.额外说明 - -本文中 Debian 云镜像的系统配置方法大多也适用于 Debian 普通镜像。 - -但有关 IP 地址、DNS、系统时区等内容的配置方法在 Debian 普通镜像上有区别。 - -而且,如果使用 Debian 普通镜像制作虚拟机模板,建议不要使用 `Cloud-Init` ,以避免某些配置发生变化。 - -使用 Debian ISO 镜像安装完操作系统后,修改网卡 IP 地址配置,需要执行以下命令: - -```bash -## Debian 普通镜像调整网卡配置 -sudo vim /etc/network/interfaces -``` - -在网卡的配置文件中需要修改 `address` 、 `dns-nameservers` 等参数,完整配置文件参考如下: - -```bash -# This file describes the network interfaces available on your system -# and how to activate them. For more information, see interfaces(5). - -source /etc/network/interfaces.d/* - -# The loopback network interface -auto lo -iface lo inet loopback - -# The primary network interface -allow-hotplug ens18 -iface ens18 inet static - address 172.16.1.2/24 - gateway 172.16.1.1 - # dns-* options are implemented by the resolvconf package, if installed - dns-nameservers 172.16.1.2 - dns-search fox.local -``` - -检查系统的 `hosts` ,执行以下命令: - -```bash -## 检查系统 hosts -cat /etc/hosts - -## 示例输出 - -127.0.0.1 localhost -172.16.1.2 dns01.fox.local dns01 - -# The following lines are desirable for IPv6 capable hosts -::1 localhost ip6-localhost ip6-loopback -ff02::1 ip6-allnodes -ff02::2 ip6-allrouters -``` - -Debian 普通镜像需要手动修改系统 DNS 配置,执行以下命令: - -```bash -## Debian 普通镜像修改系统 DNS -sudo vim /etc/resolv.conf -``` - -在编辑器对话框中输入以下内容,注意命令中间的空格: - -```bash -search fox.local -nameserver 172.16.1.2 -nameserver fdac::2 -nameserver 127.0.0.1 -``` - -Debian 普通镜像使用 `systemd-timesyncd.service` 来同步系统时间,检查服务状态: - -```bash -## 检查 Debian 普通镜像 NTP 服务器状态 -sudo systemctl status systemd-timesyncd.service -``` - -调整 NTP 服务,逐条执行以下命令: - -```bash -## 创建 NTP 配置文件的文件夹 -sudo mkdir /etc/systemd/timesyncd.conf.d - -## 创建 NTP 配置文件 -sudo nano /etc/systemd/timesyncd.conf.d/server_ntp.conf -``` - -在 nano 编辑器对话框中输入以下内容,并保存: - -```bash -[Time] -NTP=ntp.tencent.com ntp.aliyun.com -``` - -重启 NTP 服务: - -```bash -## 重启 NTP 服务 -sudo systemctl restart systemd-timesyncd.service -``` \ No newline at end of file diff --git a/06.PVE用模板克隆虚拟机.md b/06.PVE用模板克隆虚拟机.md index 960452a..dc34c0b 100644 --- a/06.PVE用模板克隆虚拟机.md +++ b/06.PVE用模板克隆虚拟机.md @@ -60,4 +60,54 @@ `启动延时` 为 `15` ,表示该虚拟机在 PVE 启动完成后,延迟 15 秒自行启动。 -至此,新虚拟机的 IP 地址设置、DNS 设置、自启动设置已经完成。 \ No newline at end of file +## 4.安装 Adguard + +Adguard Home 将采用 `snap` 形式安装,Adguard Home 安装完成后,先停止其服务: + +```bash +## 安装 snap +sudo apt install snapd + +## 安装 Adguard Home +sudo snap install adguard-home + +## 停止 Adguard Home +sudo snap stop adguard-home +``` + +### 4.1.配置Snap自动更新 + +先查看 Snap 当前的更新策略,执行以下命令: + +```bash +## 显示当前 Snap 自动更新设置 +sudo snap refresh --time +``` + +将 Snap 自动更新时间设置为每天 `2:30-3:30` 和 `14:30-15:30` 两个时间段: + +```bash +## 修改 Snap 自动更新时间 +sudo snap set system refresh.timer=2:30-3:30,14:30-15:30 + +## 其他 Snap 自动更新时间设置语法参考 +sudo snap set system refresh.timer=mon,2:30,,fri,2:30 +``` + +### 4.2.配置定时任务 + +本步骤为可选操作,主要设置定时重启 Snap 服务。 + +```bash +## 查看系统定时任务 +sudo crontab -l + +## 编辑系统定时任务,编辑器选择 nano +sudo crontab -e + +## 定时重启 Snap 服务、操作系统 +0 5 * * * /usr/bin/snap restart adguard-home +``` + +至此,新虚拟机的 IP 地址设置、DNS 设置、自启动设置已经完成。 +