diff --git a/05.PVE制作虚拟机模板.md b/05.PVE制作虚拟机模板.md index 03ee6e1..7386309 100644 --- a/05.PVE制作虚拟机模板.md +++ b/05.PVE制作虚拟机模板.md @@ -22,7 +22,7 @@ ```bash ## 编辑 ssh 配置文件 -sudo vim /etc/ssh/sshd_config.d/server_sshd.conf +$ sudo vim /etc/ssh/sshd_config.d/server_sshd.conf ``` 在配置文件中添加以下配置项,并保存。 @@ -40,7 +40,7 @@ UseDNS no ```bash ## 重启 sshd -sudo systemctl restart ssh.service +$ sudo systemctl restart ssh.service ``` ### 1.2.配置软件源 @@ -55,27 +55,22 @@ Debian12 云镜像的软件源配置采用了 `DEB822` 格式,新版 `sources. ```bash ## 进入系统源配置文件目录 -cd /etc/apt/ +$ cd /etc/apt/ ## 查看镜像配置文件 -cat sources.list - - -## 配置文件输出 +$ cat sources.list +#### 配置文件示例输出 # See /etc/apt/sources.list.d/debian.sources - ``` 进一步查看新配置文件 `debian.sources` 的内容,其配置包含 `3` 个部分。 ```bash ## 查看新版镜像配置文件 -cat /etc/apt/sources.list.d/debian.sources - - -## 新版配置文件输出 (主体部分) +$ cat /etc/apt/sources.list.d/debian.sources +#### 新版配置文件示例输出 (主体部分) Types: deb deb-src URIs: mirror+file:///etc/apt/mirrors/debian.list Suites: bookworm bookworm-updates bookworm-backports @@ -86,22 +81,16 @@ URIs: mirror+file:///etc/apt/mirrors/debian-security.list Suites: bookworm-security Components: main - ## 查看关联的配置文件 -cat /etc/apt/mirrors/debian.list - - -## 关联配置文件输出 (关联部分 1 ) +$ cat /etc/apt/mirrors/debian.list +#### 关联配置文件示例输出 (关联部分 1 ) https://deb.debian.org/debian - ## 查看关联的配置文件 -cat /etc/apt/mirrors/debian-security.list - - -## 关联配置文件输出 (关联部分 2 ) +$ cat /etc/apt/mirrors/debian-security.list +#### 关联配置文件示例输出 (关联部分 2 ) https://deb.debian.org/debian-security ``` @@ -116,7 +105,7 @@ https://deb.debian.org/debian-security ```bash ## 编辑 debian.sources 配置文件 -sudo vim /etc/apt/sources.list.d/debian.sources +$ sudo vim /etc/apt/sources.list.d/debian.sources ``` 删除里面全部内容,添加以下配置项,并保存。 @@ -140,7 +129,7 @@ Components: main contrib non-free non-free-firmware ```bash ## 编辑 debian.list 配置文件 -sudo vim /etc/apt/mirrors/debian.list +$ sudo vim /etc/apt/mirrors/debian.list ## 替换为 USTC 地址 @@ -153,7 +142,7 @@ http://mirrors.ustc.edu.cn/debian ```bash ## 编辑 debian-security.list 配置文件 -sudo vim /etc/apt/mirrors/debian-security.list +$ sudo vim /etc/apt/mirrors/debian-security.list ## 替换为 USTC 地址 @@ -168,29 +157,29 @@ http://mirrors.ustc.edu.cn/debian-security ```bash ## 清理不必要的包 -sudo apt clean && sudo apt autoclean && sudo apt autoremove --purge +$ sudo apt clean && sudo apt autoclean && sudo apt autoremove --purge ## 更新软件源 -sudo apt update +$ sudo apt update ## 更新系统 -sudo apt dist-upgrade +$ sudo apt dist-upgrade ``` 接下来安装系统必要软件,安装 `iperf3` 后,系统将询问是否将其作为系统服务开机自启,选择 `no` 即可。 ```bash ## 安装系统软件 -sudo apt install qemu-guest-agent zsh git htop lm-sensors tmux cron nftables sshguard +$ sudo apt install qemu-guest-agent zsh git htop lm-sensors tmux cron nftables sshguard ## 安装系统自动更新工具 -sudo apt install unattended-upgrades powermgmt-base python3-gi +$ sudo apt install unattended-upgrades powermgmt-base python3-gi ## 安装网络工具 -sudo apt install iperf iperf3 iftop lsof ldnsutils +$ sudo apt install iperf iperf3 iftop lsof ldnsutils ## 写入磁盘 -sudo sync +$ sudo sync ``` ### 1.4.调整内核参数 @@ -201,7 +190,7 @@ sudo sync ```bash ## 编辑 内核参数 配置文件 -sudo vim /etc/sysctl.d/99-sysctl.conf +$ sudo vim /etc/sysctl.d/99-sysctl.conf ``` 在配置文件末尾输入以下配置项,注意配置中间的空格。 @@ -237,7 +226,7 @@ net.ipv6.conf.default.use_tempaddr = 0 ```bash ## 让内核参数生效 -sudo sysctl -f +$ sudo sysctl -f ``` ### 1.5.调整系统时间 @@ -246,16 +235,13 @@ sudo sysctl -f ```bash ## 设置系统时区 -sudo timedatectl set-timezone Asia/Shanghai +$ sudo timedatectl set-timezone Asia/Shanghai ## 检查系统时间 -date -R - - -## 参考输出 +$ date -R +#### 系统时间示例输出 Mon, 26 Jun 2023 16:16:16 +0800 - ``` Debian 云镜像默认使用 `systemd-timesyncd.service` 同步时间,且需要调整为使用国内 NTP 服务器。 @@ -264,10 +250,10 @@ Debian 云镜像默认使用 `systemd-timesyncd.service` 同步时间,且需 ```bash ## 创建 NTP 配置文件的文件夹 -sudo mkdir /etc/systemd/timesyncd.conf.d +$ sudo mkdir /etc/systemd/timesyncd.conf.d ## 创建 NTP 配置文件 -sudo vim /etc/systemd/timesyncd.conf.d/server_ntp.conf +$ sudo vim /etc/systemd/timesyncd.conf.d/server_ntp.conf ``` 在配置文件中添加以下配置项,并保存。 @@ -284,17 +270,16 @@ NTP=ntp.tencent.com ntp.aliyun.com ```bash ## 重启 chrony 服务 -sudo systemctl restart systemd-timesyncd.service +$ sudo systemctl restart systemd-timesyncd.service ## 检查系统 NTP 服务器 -sudo systemctl status systemd-timesyncd.service +$ sudo systemctl status systemd-timesyncd.service ``` 如果输出以下类似内容,则表示系统 NTP 服务设置正确。 ```bash -## NTP 服务示例输出 - +#### NTP 服务示例输出 ● 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 @@ -311,7 +296,6 @@ Jun 26 16:48:00 DNST01 systemd[1]: Starting systemd-timesyncd.service - Network 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.配置自动更新 @@ -322,29 +306,26 @@ Jun 26 16:48:00 DNST01 systemd-timesyncd[18829]: Initial clock synchronization t ```bash ## 检查系统定时器 -sudo systemctl status apt-daily-upgrade.timer +$ sudo systemctl status apt-daily-upgrade.timer ``` 配置系统自动更新策略,执行以下命令,使用键盘 `左右方向键` 进行选择,`回车键` 进行确认。 ```bash ## 配置自动更新策略 -sudo dpkg-reconfigure -plow unattended-upgrades +$ sudo dpkg-reconfigure -plow unattended-upgrades ## 选择 “是” (“YES”) - -## 示例输出 - +#### 系统自动更新示例输出 Creating config file /etc/apt/apt.conf.d/20auto-upgrades with new version - ``` 接下来调整 apt 的 `20auto-upgrades` 配置文件。 ```bash ## 编辑 20auto-upgrades 配置文件 -sudo vim /etc/apt/apt.conf.d/20auto-upgrades +$ sudo vim /etc/apt/apt.conf.d/20auto-upgrades ``` 删除里面全部内容,添加以下配置项,并保存。 @@ -365,7 +346,7 @@ APT::Periodic::CleanInterval "1"; ```bash ## 编辑 50unattended-upgrades 配置文件 -sudo vim /etc/apt/apt.conf.d/50unattended-upgrades +$ sudo vim /etc/apt/apt.conf.d/50unattended-upgrades ``` 根据 “注释” 中相关说明,调整配置文件。 @@ -377,7 +358,6 @@ sudo vim /etc/apt/apt.conf.d/50unattended-upgrades "origin=Debian,codename=${distro_codename}-updates"; - ## 在配置文件末尾增加以下配置项,代表启用,并调整参数 Unattended-Upgrade::AutoFixInterruptedDpkg "true"; @@ -398,7 +378,7 @@ Unattended-Upgrade::Automatic-Reboot-Time "04:30"; ```bash ## 配置系统定时器 -sudo systemctl edit apt-daily-upgrade.timer +$ sudo systemctl edit apt-daily-upgrade.timer ``` 根据配置文件中的提示,在中间空白处填入以下配置项。 @@ -419,10 +399,10 @@ RandomizedDelaySec=0 ```bash ## 重启触发器 -sudo systemctl restart apt-daily-upgrade.timer +$ sudo systemctl restart apt-daily-upgrade.timer ## 再次检查触发器状态 -sudo systemctl status apt-daily-upgrade.timer +$ sudo systemctl status apt-daily-upgrade.timer ``` ### 1.7.配置定时任务 @@ -431,13 +411,16 @@ sudo systemctl status apt-daily-upgrade.timer ```bash ## 查看系统定时任务 -sudo crontab -l +$ sudo crontab -l ## 编辑系统定时任务,编辑器选择 nano -sudo crontab -e +$ sudo crontab -e +``` +在配置文件末尾,增加以下配置项。 -## 在配置文件末尾增加以下配置项 +```bash +## 定时任务配置项 0 6 8,24 * * /usr/sbin/reboot @@ -449,16 +432,17 @@ sudo crontab -e ```bash ## 返回 home 目录 -cd +$ cd ## 使用 curl 安装 oh-my-zsh -sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" +$ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" ## 或者使用 wget 安装 oh-my-zsh -sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)" +$ sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)" ## 询问是否切换默认 shell,输入 Y -## 示例输出 + +#### 示例输出 Time to change your default shell to zsh: Do you want to change your default shell to zsh? [Y/n] y ``` @@ -471,19 +455,19 @@ Debian 模板虚拟机已经配置完成,在将其转换为模板前需要对 ```bash ## 清理系统软件包 -sudo apt clean && sudo apt autoclean && sudo apt autoremove --purge +$ sudo apt clean && sudo apt autoclean && sudo apt autoremove --purge ## 清理系统缓存 -sudo rm -rvf /var/cache/apt/* /var/lib/apt/lists/* /tmp/* +$ sudo rm -rvf /var/cache/apt/* /var/lib/apt/lists/* /tmp/* ## 清理系统日志 -sudo find /var/log/ -type f | xargs sudo rm -rvf +$ sudo find /var/log/ -type f | xargs sudo rm -rvf ## 清理命令历史记录文件 -rm -rvf ~/.bash_history ~/.zsh_history +$ rm -rvf ~/.bash_history ~/.zsh_history ## 清理命令历史 -history -c +$ history -c ``` ## 2.虚拟机转为模板