From b59e61fddae78341f5a370e6f9464f3ba636ce27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E5=88=80=E7=8B=90=E7=8B=B8?= Date: Sun, 7 Aug 2022 03:52:17 +0000 Subject: [PATCH] =?UTF-8?q?update=205.PVE=E9=85=8D=E7=BD=AE=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E8=99=9A=E6=8B=9F=E6=9C=BA.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 5.PVE配置模板虚拟机.md | 247 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 247 insertions(+) diff --git a/5.PVE配置模板虚拟机.md b/5.PVE配置模板虚拟机.md index e69de29..55373c8 100644 --- a/5.PVE配置模板虚拟机.md +++ b/5.PVE配置模板虚拟机.md @@ -0,0 +1,247 @@ +## 5.配置Debian系统 + +此时可以将 Debian 虚拟机开机,并使用 `Cloud-Init` 中设置的账号密码进行登录。 + +### 5.1.启用SSH密码登录 + +因为 Debian 的云镜像默认使用秘钥登录,因此切换到左侧菜单的 `控制台` 进行操作: + +![登录Debian操作系统](img/Template/os_login.png) + +在 Debian 服务器的命令行界面,使用 `nano` 编辑器编辑 `sshd` 服务的配置文件,执行以下命令: + +```bash +## 编辑 ssh 配置文件 +sudo nano /etc/ssh/sshd_config.d/fox_sshd.conf +``` + +在编辑器对话框中输入以下内容,注意命令中间的空格: + +```bash +PasswordAuthentication yes + +PermitEmptyPasswords no +``` + +`nano` 编辑器常用操作如下: +- 在编辑文件时,可以用键盘 `方向键` 移动光标位置 +- 在保存文件时,按下键盘 `ctrl 和 x` 组合键,再按下键盘 `y` 键,即可保存 + +然后,重启 SSH 服务: + +```bash +## 重启 sshd +sudo systemctl restart ssh.service +``` + +就可以像往常一样,通过 SSH 工具远程访问 Debian 虚拟机了。 + +### 5.2.配置系统软件源 + +使用 SSH 工具登录 Debian 虚拟机,常用 SSH 工具可以看之前的文章中的 [SSH 工具推荐](./0.PVE系统安装.md#03ssh工具) 。 + +首先需要对 Debian 系统的软件源进行修改,这里使用 USTC 的软件源。 + +如果以后系统版本发生变化,参考使用 snullp 大叔开发的 [配置生成器](https://mirrors.ustc.edu.cn/repogen/) 。 + +逐条执行以下命令: + +```bash +## 进入系统源配置文件目录 +cd /etc/apt/ + +## 备份旧的系统源配置文件 +sudo mv sources.list sources.list.bak + +## 创建新的系统源配置文件 +sudo nano sources.list +``` + +在 `nano` 编辑器对话框中输入以下内容(注释了 `deb-src` 以便加快软件源同步速度),并保存: + +```bash +deb https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free +# deb-src https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free + +deb https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free +# deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free + +deb https://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free +# deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free + +deb https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free +# deb-src https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free +``` + +更新软件源的同步内容,并更新系统,逐条执行以下命令: + +```bash +## 清理不必要的包 +sudo apt clean && sudo apt autoclean && sudo apt autoremove --purge + +## 更新软件源 +sudo apt update + +## 更新系统 +sudo apt dist-upgrade +``` + +接下来安装系统必要软件,Adguard Home 将采用 `snap` 形式安装,逐条执行以下命令: + +```bash +## 安装系统软件 +sudo apt install qemu-guest-agent zsh git curl htop lm-sensors fail2ban vim tmux unattended-upgrades apt-listchanges powermgmt-base + +## 安装网络工具 +sudo apt install iperf iperf3 iftop lsof dnsutils + +## 安装 snap +sudo apt install snapd + +## 安装 Adguard Home +sudo snap install adguard-home +``` + +Adguard Home 安装完成后,先停止其服务: + +```bash +## 停止 Adguard Home +sudo snap stop adguard-home +``` + +### 5.3.配置系统网卡 + +使用 `Cloud-Init` 并给虚拟机分配了 IP 地址后,虚拟机的网卡还会从路由器的 DHCP 再获取一个 IP 地址。 + +这个功能在给虚拟机添加网卡设备时很有用,但本 Debian 虚拟机模板的使用场景只需要一个静态 IP 地址,因此需要禁用网卡自动获取 IP 地址的功能。 + +执行以下命令对网卡的配置文件进行修改: + +```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 ## 在这行前面增加注释符 # 来注释 +``` + +将配置保存,可将系统重启。 + +在系统重启完成后,执行以下命令检查系统的 IP 地址状态: + +```bash +## 检查系统 IP 地址状态 +ip a +``` + +如果系统网卡有且仅有一个 IP 地址,且该 IP 地址与 `Cloud-Init` 中设置的 IP 地址相同,则表示系统网卡已正确设置。 + +### 5.4.调整系统内核参数 + +由于该 Debian 虚拟机模板将用于克隆内网 DNS 服务器,因此需要调整一部分系统内核参数来简单优化性能。 + +执行以下命令对 `sysctl` 的配置文件进行修改: + +```bash +## 调整系统内核参数 +sudo nano /etc/sysctl.conf +``` + +在配置文件末尾输入以下内容,注意命令中间的空格: + +```bash +net.core.default_qdisc = fq_codel +net.ipv4.tcp_congestion_control = bbr + +net.ipv6.conf.all.disable_ipv6 = 1 + +net.ipv4.tcp_fastopen = 1 +net.ipv4.tcp_syncookies = 1 +net.ipv4.tcp_notsent_lowat = 131072 + +net.ipv4.tcp_ecn = 1 +net.ipv4.tcp_sack = 1 +net.ipv4.tcp_dsack = 1 + +net.ipv4.icmp_ratelimit = 50 +``` + +保存该配置文件后,重启系统或者执行以下命令让配置生效: + +```bash +## 让内核参数生效 +sudo sysctl -f +``` + +### 5.5.调整系统时间 + +默认情况下的 Debian 云镜像的系统时间可能不正确,执行以下命令将系统时区设置为中国时区: + +```bash +## 设置系统时区 +sudo timedatectl set-timezone Asia/Shanghai +``` + +设置完成后,执行以下命令检查系统时间: + +```bash +date -R +``` + +Debian 普通镜像常用 `systemd-timesyncd.service` 来同步时间,而 Debian 云镜像使用 `chrony.service` 来同步时间。 + +为了使用国内的 NTP 服务器,需要对 `chrony.service` 进行配置。 + +执行以下命令对 `chrony` 的配置文件进行修改: + +```bash +## 编辑 chrony 配置文件 +sudo nano /etc/chrony/chrony.conf +``` + +在编辑器对话框中,将 `pool 2.debian.pool.ntp.org iburst` 这行内容 “注释” 掉,并添加国内的 NTP 服务器,参考如下内容: + +```bash +## chrony 服务配置文件示例 + +# Use Debian vendor zone. +# pool 2.debian.pool.ntp.org iburst ## 在这行前面增加注释符 # 来注释 + +# Use Custom vendor zone. +pool ntp.tencent.com iburst +pool ntp.aliyun.com iburst +``` + +保存该配置文件后,重启 `chrony` 服务: + +```bash +## 重启 chrony 服务 +sudo systemctl restart chrony.service +``` + +再检查系统 NTP 服务器是否被正确修改: + +```bash +## 检查系统 NTP 服务器 +chronyc sources -V +``` + +如果输出以下类似内容,则表示系统 NTP 服务设置正确: + +```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 +``` \ No newline at end of file