Files
pve_toss_notes/06.PVE用模板克隆虚拟机.md
T
2023-11-13 12:21:25 +08:00

9.8 KiB

1.克隆虚拟机

在上一篇文章 05.PVE制作虚拟机模板 中,已经制作好了虚拟机模板。

接下来将使用该模板克隆出新的虚拟机,并安装 Adguard Home 作为内网的 DNS 服务器。

鼠标 右键单击 虚拟机模板,在弹出的菜单中选择 克隆

克隆虚拟机

在弹出的虚拟机克隆对话框中,根据实际情况及下方表格内容,修改虚拟机参数。

参数 说明
目标节点 node01 当前 PVE 服务器节点
VM ID 201 可自定义,不能与现存虚拟机 VM ID 相同
名称 DNS01 可自定义,Cloud-Init 将使用该名称作为虚拟机 hostname
模式 完整克隆 选择虚拟机的克隆模式
目标存储 local-lvm 克隆出的虚拟机文件存储位置

修改参数后,点击 克隆 ,即可使用该模板克隆出新的虚拟机。

克隆虚拟机参数

2.调整 Cloud-Init

克隆出来的虚拟机的 Cloud-Init 参数默认与模板完全一致。

根据 内部网络地址 规划,内网 DNS 服务器 IP 地址分别为:

  • 172.16.1.2/24 (fdac::2/64)

  • 172.16.1.3/24 (fdac::3/64)

因此需要调整新虚拟机的 Cloud-Init 参数。

  • IP配置 中的 IPv4 地址参数为 172.16.1.2/24 ,网关保持 172.16.1.1 不变。

  • IP配置 中的 IPv6 地址参数为 fdac::2/64 ,网关保持为空。

需要注意的是,如果修改了 用户 参数,相当于新建了一个系统管理员,之前设置的 oh-my-zsh 需要在新管理员下重新设置。

调整新虚拟机Cloud-Init

当主路由配置了 ULA IPv6 网段,且指定了内网 DNS 服务器的 ULA IPv6 地址时,参数如下。

调整新虚拟机Cloud-Init

3.调整配置参数

04.PVE创建模板虚拟机 中提到过,新虚拟机需要修改配置参数才能自动启动。

进入左侧虚拟机 选项 页面,将虚拟机 开机自启动 参数设置为

克隆虚拟机自动启动

鼠标 双击 启动/关机顺序 选项,可调整虚拟机的自动开机参数。

启动/关机顺序2 ,表示该虚拟机第 2 个启动,倒数第 2 个关机。

启动延时10 ,表示该虚拟机在 PVE 启动后,延迟 10 秒后自动启动。

克隆虚拟机自动启动顺序

4.调整系统端口

设置完成后,将该虚拟机开机,使用 SSH 工具登录,并使用以下命令检查端口占用。

## 检查 53 端口占用
$ sudo lsof -i :53

#### 端口占用示例输出
COMMAND   PID            USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
systemd-r 347 systemd-resolve   17u  IPv4  13445      0t0  UDP localhost:domain
systemd-r 347 systemd-resolve   18u  IPv4  13446      0t0  TCP localhost:domain (LISTEN)
systemd-r 347 systemd-resolve   19u  IPv4  13447      0t0  UDP localhost:domain
systemd-r 347 systemd-resolve   20u  IPv4  13448      0t0  TCP localhost:domain (LISTEN)

当前系统 53 端口被 systemd-resolved.service 占用,会导致设置 DNS 服务时监听端口失败。

为了正常使用 53 端口,需要对 systemd-resolved.service 进行配置,逐行执行以下命令。

## 创建 systemd-resolved 配置文件夹
$ sudo mkdir -p /etc/systemd/resolved.conf.d

## 创建 systemd-resolved 配置文件
$ sudo nvim /etc/systemd/resolved.conf.d/server_dns.conf

在配置文件中添加以下配置项,并保存。

## systemd-resolved 配置项

[Resolve]
DNS=127.0.0.1
DNSStubListener=no

保存该配置文件后,还需调整系统 resolv.conf 配置文件,逐行执行以下命令。

## 备份 resolv.conf 配置文件
$ sudo mv /etc/resolv.conf /etc/resolv.conf.bak

## 创建 resolv.conf 软链接
$ sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

配置完成后,需重启 systemd-resolved.service 服务,并再次检查系统 53 端口占用。

## 重启 systemd-resolved 服务
$ sudo systemctl restart systemd-resolved.service

5. Adguard Home

Adguard Home 将采用 snap 形式安装,逐行执行以下命令。

## 安装 snap
$ sudo apt install snapd

## 安装 Adguard Home
$ sudo snap install adguard-home

5.1.自动更新

查看 Snap 当前的更新策略,执行以下命令。

## 显示当前 Snap 自动更新设置
$ sudo snap refresh --time

Snap 自动更新时间设置为每天 2:30-3:3014:30-15:30 两个时间段。

## 修改 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

5.2.配置 Adguard Home

关于 Adguard Home 配置相关内容,请参阅 Adguard Home 折腾手记

5.3.定时任务

本步骤为可选操作,主要设置定时重启 Adguard Home

## 查看系统定时任务
$ sudo crontab -l

## 编辑系统定时任务,编辑器选择 nano
$ sudo crontab -e

在配置文件末尾,增加以下配置项。

## 定时任务配置项

30 4 * * * /usr/bin/snap restart adguard-home

6. SmartDNS

若需使用 SmartDNS 代替 Adguard Home ,可使用 Debian 官方源进行安装,但其版本通常较为 “过时” 。

## 安装 SmartDNS
$ sudo apt install smartdns

因此,更推荐使用其 Github 仓库中的最新稳定版进行安装,官方仓库请参阅 pymumu/smartdns

下载 SmartDNS 最新版本时,请根据系统架构选择合适的版本,逐行执行以下命令。

## 创建存放 SmartDNS 安装包的临时目录
$ mkdir -p /tmp/SmartDNS

## 进入文件夹
$ cd /tmp/SmartDNS

## 下载 SmartDNS 安装包
$ wget https://github.com/pymumu/smartdns/releases/download/Release43/smartdns.1.2023.08.11-1937.x86_64-linux-all.tar.gz

## 解压缩 SmartDNS 安装包
$ tar zxf smartdns.1.2023.08.11-1937.x86_64-linux-all.tar.gz

## 进入安装包目录
$ cd smartdns

## 赋予安装脚本执行权限
$ chmod +x ./install

## 安装 SmartDNS
$ sudo ./install -i

修改 SmartDNS 配置之前,需检查 smartdns.service 服务状态,确保该服务开机自启动。

## 检查 smartdns.service
$ sudo systemctl status smartdns.service

## 设置 smartdns.service 开机自启动
$ sudo systemctl enable smartdns.service

6.1.配置 SmartDNS

SmartDNS 配置较为复杂,可按需制定各类 DNS 请求规则,建议先查阅官方提供的 配置指导配置选项

若需使用 SmartDNS 过滤广告,则需下载其广告过滤配置文件,为可选操作。

## 下载广告过滤配置文件
$ sudo wget https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns/anti-ad-smartdns.conf

SmartDNS 的主配置文件一般位于 /etc/smartdns 目录下,修改配置文件之前,使用以下命令将其备份。

## 备份 SmartDNS 主配置文件
$ sudo mv /etc/smartdns/smartdns.conf /etc/smartdns/smartdns.conf.bak

使用 neovim 编辑器创建 SmartDNS 主配置文件,执行以下命令。

## 创建 SmartDNS 主配置文件
$ sudo nvim /etc/smartdns/smartdns.conf

在编辑器对话框中输入以下内容,并保存。

额外说明:

  • 当不使用 anti-ad-smartdns.conf 进行广告过滤时,需移除主配置文件中对应配置项。

  • 检查配置文件中关于本地域名及其上游 DNS 服务器相关配置,请根据实际情况进行调整。

# This configuration file is customized by fox,
# Optimize SmartDNS parameters for local DNS server.
#
# For use common DNS server as upstream DNS server,
# please modify 'server' parameter according to
# your network environment.
#
# eg:
#       server 119.29.29.29
#       server 223.5.5.5
#       server 114.114.114.114
#       server 2402:4e00::
#       server 2400:3200::1

conf-file /etc/smartdns/anti-ad-smartdns.conf
cache-file /tmp/smartdns.cache

bind [::]:53
bind-tcp [::]:53

serve-expired yes
serve-expired-ttl 57600
serve-expired-reply-ttl 3
prefetch-domain yes
serve-expired-prefetch-time 28800

speed-check-mode ping,tcp:80,tcp:443

force-qtype-SOA 65

log-level notice

server-tcp 119.29.29.29 -group dnspod -exclude-default-group
server-tcp 2402:4e00:: -group dnspod -exclude-default-group
nameserver /doh.pub/dnspod
nameserver /dot.pub/dnspod

server-tcp 223.5.5.5 -group alidns -exclude-default-group
server-tcp 2400:3200::1 -group alidns -exclude-default-group
nameserver /dns.alidns.com/alidns

server 172.16.1.1 -group intranet -exclude-default-group
server fdac::1 -group intranet -exclude-default-group
nameserver /fox.home.arpa/intranet
domain-rules /fox.home.arpa/ -speed-check-mode none -no-cache

server-tls dot.pub
server-tls dns.alidns.com

server-https https://doh.pub/dns-query
server-https https://dns.alidns.com/dns-query

6.2.定时任务

本步骤为可选操作,主要设置 SmartDNS 定时更新广告过滤配置文件和定时重启。

## 编辑系统定时任务,编辑器选择 nano
$ sudo crontab -e

在配置文件末尾,增加以下配置项。

## 定时任务配置项

15 5 * * * /usr/bin/wget -q --tries=10 --retry-connrefused --random-wait https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns/anti-ad-smartdns.conf

25 5 * * * /usr/bin/systemctl restart smartdns.service

至此,新虚拟机已配置完成,可作为内网 DNS 服务器使用。