mirror of
https://gitee.com/callmer/pve_toss_notes.git
synced 2026-08-31 05:52:52 +08:00
618 lines
15 KiB
Markdown
618 lines
15 KiB
Markdown
## 0.前期准备
|
||
|
||
某些业务场景下需要构建安全可靠的网络隧道,来打通异地内网环境或从外部访问内网的私有资源。
|
||
|
||
经过实际测试,当 TS 服务器具有 IPv6 GUA 地址时,能稳定建立隧道。
|
||
|
||
本文将使用 Debian 云镜像以及 `Tailscale` 来制作内网组网服务器。
|
||
|
||
对于虚拟机创建部分,请参考 [04.PVE创建模板虚拟机](./04.PVE创建模板虚拟机.md) ,其他 `Cloud-Init` 相关参数如下。
|
||
|
||
|参数|值|说明|
|
||
|--|--|--|
|
||
|虚拟机名称|`SVR01`| TS 服务器 `主机名` |
|
||
|DNS 域|`fox.home.arpa`| TS 服务器 `Cloud-Init` |
|
||
|DNS 服务器|`172.16.1.1`| TS 服务器 `Cloud-Init` |
|
||
|IPv4|`172.16.1.4/24`| TS 服务器 `Cloud-Init` |
|
||
|IPv4 网关|`172.16.1.1`| TS 服务器 `Cloud-Init` |
|
||
|IPv6|`SLAAC`| TS 服务器 `Cloud-Init` |
|
||
|
||
## 1.配置系统
|
||
|
||
由于 TS 服务器具备路由功能,所以在配置方法和系统参数方面与内网 DNS 服务器有一些区别。
|
||
|
||
### 1.1.配置 SSH
|
||
|
||
与配置内网 DNS 服务器时一样,首先需要调整系统的 SSH 登录权限参数。
|
||
|
||
在虚拟机的命令行界面,使用 `vim` 编辑器编辑 `sshd` 服务的配置文件,执行以下命令。
|
||
|
||
```bash
|
||
## 编辑 ssh 配置文件
|
||
$ sudo vim /etc/ssh/sshd_config.d/server_sshd.conf
|
||
```
|
||
|
||
在配置文件中添加以下配置项,并保存。
|
||
|
||
```bash
|
||
## SSH 配置项
|
||
|
||
PasswordAuthentication yes
|
||
PermitEmptyPasswords no
|
||
UseDNS no
|
||
|
||
```
|
||
|
||
修改完成后,需要重启 SSH 服务。
|
||
|
||
```bash
|
||
## 重启 sshd
|
||
$ sudo systemctl restart ssh.service
|
||
```
|
||
|
||
### 1.2.配置软件源
|
||
|
||
使用 SSH 工具登录 TS 服务器,常用 SSH 工具请参阅 [01.PVE系统安装](./01.PVE系统安装.md) 。
|
||
|
||
首先需要对 Debian 系统软件源进行修改,这里使用 [USTC](http://mirrors.ustc.edu.cn/help/debian.html) 镜像站作为演示。
|
||
|
||
当系统版本发生变化时,请参考使用 snullp 大叔开发的 [配置生成器](https://mirrors.ustc.edu.cn/repogen/) 。
|
||
|
||
使用 `vim` 编辑器编辑 `debian.sources` 配置文件,执行以下命令。
|
||
|
||
```bash
|
||
## 编辑 debian.sources 配置文件
|
||
$ sudo vim /etc/apt/sources.list.d/debian.sources
|
||
```
|
||
|
||
删除里面全部内容,添加以下配置项,并保存。
|
||
|
||
```bash
|
||
## 系统软件源配置项
|
||
|
||
Types: deb
|
||
URIs: https://mirrors.ustc.edu.cn/debian
|
||
Suites: bookworm bookworm-updates
|
||
Components: main contrib non-free non-free-firmware
|
||
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
|
||
|
||
Types: deb
|
||
URIs: https://mirrors.ustc.edu.cn/debian-security
|
||
Suites: bookworm-security
|
||
Components: main contrib non-free non-free-firmware
|
||
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
|
||
|
||
```
|
||
|
||
为了防止 `Cloud-Init` 服务意外修改软件源配置,需要添加文件保护,执行以下命令。
|
||
|
||
```bash
|
||
## 增加文件保护
|
||
$ sudo chattr +i /etc/apt/sources.list.d/debian.sources
|
||
|
||
## 检查文件保护
|
||
$ lsattr /etc/apt/sources.list.d/debian.sources
|
||
|
||
#### 示例输出
|
||
----i---------e------- /etc/apt/sources.list.d/debian.sources
|
||
```
|
||
|
||
进一步添加 TS 签名密钥以及软件源,执行以下命令。
|
||
|
||
```bash
|
||
## 添加 TS 签名密钥
|
||
$ curl -fsSL https://pkgs.tailscale.com/stable/debian/bookworm.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg > /dev/null
|
||
|
||
## 添加 TS 软件源
|
||
$ curl -fsSL https://pkgs.tailscale.com/stable/debian/bookworm.tailscale-keyring.list | sudo tee /etc/apt/sources.list.d/tailscale.list
|
||
```
|
||
|
||
### 1.3.安装软件
|
||
|
||
软件源设置完成后,需要更新系统,执行以下命令。
|
||
|
||
```bash
|
||
## 清理不必要的包
|
||
$ sudo apt clean && sudo apt autoclean && sudo apt autoremove --purge
|
||
|
||
## 更新软件源
|
||
$ sudo apt update
|
||
|
||
## 更新系统
|
||
$ sudo apt dist-upgrade
|
||
```
|
||
|
||
接下来安装系统必要软件,安装 `iperf3` 后,系统将询问是否将其作为系统服务开机自启,选择 `no` 即可。
|
||
|
||
```bash
|
||
## 安装系统软件
|
||
$ sudo apt install qemu-guest-agent zsh git htop tmux cron nftables sshguard neovim
|
||
|
||
## 安装系统自动更新工具
|
||
$ sudo apt install unattended-upgrades powermgmt-base python3-gi
|
||
|
||
## 安装网络工具
|
||
$ sudo apt install iperf iperf3 iftop lsof knot-dnsutils dnsmasq conntrack
|
||
|
||
## 安装 TS
|
||
$ sudo apt install tailscale
|
||
|
||
## 写入磁盘
|
||
$ sudo sync
|
||
```
|
||
|
||
### 1.4.调整内核模块
|
||
|
||
使用 `neovim` 编辑器编辑 **内核模块** 配置文件,执行以下命令。
|
||
|
||
```bash
|
||
## 创建 内核模块 配置文件
|
||
$ sudo nvim /etc/modules-load.d/server_modules.conf
|
||
```
|
||
|
||
在配置文件中添加以下配置项,并保存。
|
||
|
||
```bash
|
||
# This configuration file is customized by fox,
|
||
# Optimize netfilter related modules at system boot.
|
||
|
||
nf_conntrack
|
||
|
||
```
|
||
|
||
### 1.5.调整内核参数
|
||
|
||
使用 `neovim` 编辑器编辑 **内核参数** 配置文件,执行以下命令。
|
||
|
||
```bash
|
||
## 编辑 内核参数 配置文件
|
||
$ sudo nvim /etc/sysctl.d/99-sysctl.conf
|
||
```
|
||
|
||
在配置文件末尾输入以下配置项,注意配置中间的空格。
|
||
|
||
```bash
|
||
# This configuration file is customized by fox,
|
||
# Optimize sysctl parameters for local TS server.
|
||
|
||
kernel.panic = 20
|
||
kernel.panic_on_oops = 1
|
||
|
||
net.core.default_qdisc = fq_codel
|
||
net.ipv4.tcp_congestion_control = bbr
|
||
|
||
net.ipv4.ip_forward = 1
|
||
|
||
net.ipv6.conf.all.forwarding = 1
|
||
net.ipv6.conf.default.forwarding = 1
|
||
|
||
# Other adjustable system parameters
|
||
|
||
net.core.netdev_budget = 600
|
||
net.core.netdev_budget_usecs = 20000
|
||
|
||
net.core.rps_sock_flow_entries = 32768
|
||
|
||
net.ipv4.conf.all.accept_redirects = 0
|
||
net.ipv4.conf.default.accept_redirects = 0
|
||
|
||
net.ipv4.conf.all.accept_source_route = 0
|
||
net.ipv4.conf.default.accept_source_route = 0
|
||
|
||
net.ipv4.conf.all.arp_ignore = 1
|
||
net.ipv4.conf.default.arp_ignore = 1
|
||
|
||
net.ipv4.conf.all.rp_filter = 2
|
||
net.ipv4.conf.default.rp_filter = 2
|
||
|
||
net.ipv4.conf.all.log_martians = 1
|
||
|
||
net.ipv4.igmp_max_memberships = 256
|
||
|
||
net.ipv4.route.error_burst = 500
|
||
net.ipv4.route.error_cost = 100
|
||
|
||
net.ipv4.route.redirect_load = 2
|
||
net.ipv4.route.redirect_silence = 2048
|
||
|
||
net.ipv4.tcp_challenge_ack_limit = 1000
|
||
net.ipv4.tcp_fin_timeout = 30
|
||
net.ipv4.tcp_keepalive_time = 120
|
||
net.ipv4.tcp_syncookies = 1
|
||
|
||
net.ipv6.conf.all.accept_ra = 0
|
||
net.ipv6.conf.default.accept_ra = 0
|
||
|
||
net.ipv6.conf.all.accept_redirects = 0
|
||
net.ipv6.conf.default.accept_redirects = 0
|
||
|
||
net.ipv6.conf.all.accept_source_route = 0
|
||
net.ipv6.conf.default.accept_source_route = 0
|
||
|
||
net.ipv6.conf.all.use_tempaddr = 0
|
||
net.ipv6.conf.default.use_tempaddr = 0
|
||
|
||
net.netfilter.nf_conntrack_acct = 1
|
||
net.netfilter.nf_conntrack_checksum = 0
|
||
net.netfilter.nf_conntrack_tcp_timeout_established = 7440
|
||
|
||
```
|
||
|
||
保存该配置文件后,重启系统或者执行以下命令让配置生效。
|
||
|
||
```bash
|
||
## 让内核参数生效
|
||
$ sudo sysctl -f
|
||
```
|
||
|
||
### 1.6.调整系统时间
|
||
|
||
默认情况下 Debian 云镜像的系统时间需要调整,执行以下命令将系统时区设置为中国时区。
|
||
|
||
```bash
|
||
## 设置系统时区
|
||
$ sudo timedatectl set-timezone Asia/Shanghai
|
||
|
||
## 检查系统时间
|
||
$ date -R
|
||
```
|
||
|
||
Debian 云镜像默认使用 `systemd-timesyncd.service` 同步时间,且需要调整为使用国内 NTP 服务器。
|
||
|
||
调整 NTP 服务器参数,执行以下命令。
|
||
|
||
```bash
|
||
## 创建 NTP 配置文件的目录
|
||
$ sudo mkdir -p /etc/systemd/timesyncd.conf.d
|
||
|
||
## 创建 NTP 配置文件
|
||
$ sudo nvim /etc/systemd/timesyncd.conf.d/server_ntp.conf
|
||
```
|
||
|
||
在配置文件中添加以下配置项,并保存。
|
||
|
||
```bash
|
||
## NTP 配置项
|
||
|
||
[Time]
|
||
NTP=ntp.tencent.com ntp.aliyun.com
|
||
|
||
```
|
||
|
||
保存该配置文件后,需重启 `systemd-timesyncd.service` 服务,并再次检查系统 NTP 服务器地址。
|
||
|
||
```bash
|
||
## 重启 chrony 服务
|
||
$ sudo systemctl restart systemd-timesyncd.service
|
||
|
||
## 检查系统 NTP 服务器
|
||
$ sudo systemctl status systemd-timesyncd.service
|
||
```
|
||
|
||
### 1.7.配置自动更新
|
||
|
||
配置系统自动更新策略,执行以下命令,使用键盘 `左右方向键` 进行选择,`回车键` 进行确认。
|
||
|
||
```bash
|
||
## 配置自动更新策略
|
||
$ sudo dpkg-reconfigure -plow unattended-upgrades
|
||
|
||
## 选择 “是” (“YES”)
|
||
|
||
#### 系统自动更新示例输出
|
||
Creating config file /etc/apt/apt.conf.d/20auto-upgrades with new version
|
||
```
|
||
|
||
进一步调整 `20auto-upgrades` 配置文件。
|
||
|
||
```bash
|
||
## 编辑 20auto-upgrades 配置文件
|
||
$ sudo nvim /etc/apt/apt.conf.d/20auto-upgrades
|
||
```
|
||
|
||
删除里面全部内容,添加以下配置项,并保存。
|
||
|
||
配置文件中,用来控制更新周期的参数为 `APT::Periodic::Unattended-Upgrade` ,`7` 表示更新周期为 `7` 天。
|
||
|
||
```bash
|
||
## 系统更新周期配置项
|
||
|
||
APT::Periodic::Update-Package-Lists "1";
|
||
APT::Periodic::Unattended-Upgrade "7";
|
||
APT::Periodic::AutocleanInterval "1";
|
||
APT::Periodic::CleanInterval "1";
|
||
|
||
```
|
||
|
||
进一步调整 `50unattended-upgrades` 配置文件。
|
||
|
||
```bash
|
||
## 编辑 50unattended-upgrades 配置文件
|
||
$ sudo nvim /etc/apt/apt.conf.d/50unattended-upgrades
|
||
```
|
||
|
||
根据 “注释” 中相关说明,调整配置文件。
|
||
|
||
因为该配置文件很长,完整的配置文件可查看 [debian_ts_50unattended_upgrades.conf](./src/debian/debian_ts_50unattended_upgrades.conf) 以便对比。
|
||
|
||
```bash
|
||
## 删除以下行前面的注释符 // ,代表启用
|
||
|
||
"origin=Debian,codename=${distro_codename}-updates";
|
||
|
||
## 添加 TS 更新项目
|
||
|
||
"origin=Tailscale,codename=${distro_codename},label=Tailscale";
|
||
|
||
## 在配置文件末尾增加以下配置项,代表启用,并调整参数
|
||
|
||
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 "03:00";
|
||
|
||
```
|
||
|
||
系统自动更新配置文件修改完成后,需要重设自动更新定时器,执行以下命令。
|
||
|
||
```bash
|
||
## 配置系统定时器
|
||
$ sudo systemctl edit apt-daily-upgrade.timer
|
||
```
|
||
|
||
根据配置文件中的提示,在中间空白处填入以下配置项。
|
||
|
||
```bash
|
||
## 定时器配置项
|
||
|
||
[Timer]
|
||
OnCalendar=
|
||
OnCalendar=02:00
|
||
RandomizedDelaySec=0
|
||
|
||
```
|
||
|
||
设置完成后,重启自动更新定时器并检查其状态,执行以下命令。
|
||
|
||
在输出结果中,看到系统自动更新的触发时间为 `02:00` 则表示设置正确。
|
||
|
||
```bash
|
||
## 重启触发器
|
||
$ sudo systemctl restart apt-daily-upgrade.timer
|
||
|
||
## 再次检查触发器状态
|
||
$ sudo systemctl status apt-daily-upgrade.timer
|
||
```
|
||
|
||
### 1.8.配置防火墙
|
||
|
||
修改防火墙配置之前,需检查 `nftables.service` 服务状态,确保该服务开机自启。
|
||
|
||
```bash
|
||
## 检查 nftables.service
|
||
$ sudo systemctl status nftables.service
|
||
|
||
## 设置 nftables.service 开机自启
|
||
$ sudo systemctl enable nftables.service
|
||
```
|
||
|
||
使用 `neovim` 编辑器修改 `nftables` 配置文件,执行以下命令。
|
||
|
||
```bash
|
||
## 备份 nftables 配置文件
|
||
$ sudo mv /etc/nftables.conf /etc/nftables.conf.bak
|
||
|
||
## 创建新的 nftables 配置文件
|
||
$ sudo nvim /etc/nftables.conf
|
||
```
|
||
|
||
由于防火墙配置文件很长,因此请查阅文件 [debian_ts_nftables.conf](./src/debian/debian_ts_nftables.conf) 进行复制。
|
||
|
||
配置完成后,需重启 `nftables.service` 服务。
|
||
|
||
```bash
|
||
## 重启 nftables.service
|
||
$ sudo systemctl restart nftables.service
|
||
```
|
||
|
||
### 1.9.调整系统端口
|
||
|
||
为了正常使用 `53` 端口,需要对 `systemd-resolved.service` 进行配置,执行以下命令。
|
||
|
||
```bash
|
||
## 创建 systemd-resolved 配置目录
|
||
$ sudo mkdir -p /etc/systemd/resolved.conf.d
|
||
|
||
## 创建 systemd-resolved 配置文件
|
||
$ sudo nvim /etc/systemd/resolved.conf.d/server_dns.conf
|
||
```
|
||
|
||
在配置文件中添加以下配置项,并保存。
|
||
|
||
```bash
|
||
## systemd-resolved 配置项
|
||
|
||
[Resolve]
|
||
DNS=127.0.0.1
|
||
DNS=::1
|
||
DNSStubListener=no
|
||
|
||
```
|
||
|
||
保存该配置文件后,还需调整系统 `resolv.conf` 配置文件,执行以下命令。
|
||
|
||
```bash
|
||
## 创建 resolv.conf 软链接
|
||
$ sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
|
||
```
|
||
|
||
配置完成后,需重启 `systemd-resolved.service` 服务。
|
||
|
||
```bash
|
||
## 重启 systemd-resolved.service
|
||
$ sudo systemctl restart systemd-resolved.service
|
||
```
|
||
|
||
### 1.10.配置 Dnsmasq
|
||
|
||
检查 `dnsmasq.service` 服务状态,确保该服务开机自启。
|
||
|
||
```bash
|
||
## 检查 dnsmasq.service
|
||
$ sudo systemctl status dnsmasq.service
|
||
|
||
## 设置 dnsmasq.service 开机自启
|
||
$ sudo systemctl enable dnsmasq.service
|
||
```
|
||
|
||
`Dnsmasq` 的主配置文件一般位于 `/etc` 目录下,修改配置文件之前,执行以下命令将其备份。
|
||
|
||
```bash
|
||
## 备份 Dnsmasq 主配置文件
|
||
$ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
|
||
```
|
||
|
||
使用 `neovim` 编辑器创建 `Dnsmasq` 主配置文件,执行以下命令。
|
||
|
||
```bash
|
||
## 创建 Dnsmasq 主配置文件
|
||
$ sudo nvim /etc/dnsmasq.conf
|
||
```
|
||
|
||
在编辑器对话框中输入以下内容,并保存。
|
||
|
||
**额外说明:**
|
||
|
||
- 请根据系统内存使用情况,调整缓存参数 `cache-size`
|
||
|
||
- 配置文件中内网域名为 `fox.home.arpa` ,请根据实际情况进行调整
|
||
|
||
- `Dnsmasq` 上游 DNS 服务器分为三类,请根据实际情况进行调整
|
||
- `server=/ts.net/100.100.100.100` :TS 服务 `MagicDNS` 专用 DNS 服务器
|
||
- `server=/fox.home.arpa/172.16.1.1` :内网域名解析 DNS 服务器,通常为主路由地址
|
||
- `server` 参数中的其他 DNS 服务器供 TS 服务器自身及其下游设备使用
|
||
|
||
```bash
|
||
# This configuration file is customized by fox,
|
||
# Optimize dnsmasq parameters for local TS server.
|
||
|
||
# Main Config
|
||
|
||
conf-dir=/etc/dnsmasq.d/,*.conf
|
||
conf-file=/etc/dnsmasq.conf
|
||
|
||
log-facility=/var/log/dnsmasq.log
|
||
log-async=20
|
||
|
||
cache-size=1024
|
||
max-cache-ttl=7200
|
||
edns-packet-max=1232
|
||
rebind-domain-ok=/fox.home.arpa/
|
||
|
||
bind-dynamic
|
||
bogus-priv
|
||
domain-needed
|
||
localise-queries
|
||
local-service
|
||
no-hosts
|
||
no-negcache
|
||
no-round-robin
|
||
rebind-localhost-ok
|
||
stop-dns-rebind
|
||
|
||
# DNS Filter
|
||
|
||
server=/alt/
|
||
server=/home.arpa/
|
||
server=/ipv4only.arpa/
|
||
server=/resolver.arpa/
|
||
server=/example/
|
||
server=/bind/
|
||
server=/invalid/
|
||
server=/local/
|
||
server=/localhost/
|
||
server=/onion/
|
||
server=/test/
|
||
|
||
# DNS Server
|
||
|
||
server=/ts.net/100.100.100.100
|
||
|
||
server=/fox.home.arpa/172.16.1.1
|
||
|
||
server=172.16.1.1
|
||
|
||
```
|
||
|
||
配置完成后,需重启 `dnsmasq.service` 服务。
|
||
|
||
```bash
|
||
## 重启 dnsmasq.service
|
||
$ sudo systemctl restart dnsmasq.service
|
||
```
|
||
|
||
### 1.11.配置 ZSH
|
||
|
||
`Zsh` 是比 `Bash` 好用的 `Shell` 程序,使用 `oh-my-zsh` 进行配置。
|
||
|
||
```bash
|
||
## 返回 home 目录
|
||
$ cd
|
||
|
||
## 使用 curl 安装 oh-my-zsh
|
||
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
|
||
|
||
## 或者使用 wget 安装 oh-my-zsh
|
||
$ sh -c "$(wget https://raw.githubusercontent.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
|
||
```
|
||
|
||
## 2.配置 Tailscale
|
||
|
||
根据不同的启动参数,TS 服务将具有不同的业务能力。
|
||
|
||
若仅需 TS 组网功能,执行以下命令。
|
||
|
||
```bash
|
||
## TS 普通组网模式
|
||
$ sudo tailscale up
|
||
```
|
||
|
||
若需 TS 提供 `Exit Node` 功能,执行以下命令。
|
||
|
||
```bash
|
||
## TS Exit Node 模式
|
||
$ sudo tailscale up --advertise-exit-node --reset
|
||
|
||
## TS Exit Node 模式,但不使用 MagicDNS
|
||
$ sudo tailscale up --advertise-exit-node --accept-dns=false --reset
|
||
```
|
||
|
||
若需 TS 提供内网路由功能并能访问内网私有服务,执行以下命令。
|
||
|
||
**额外说明:**
|
||
|
||
- 请根据内网网段,调整 TS 内网路由参数 `advertise-routes`
|
||
|
||
```bash
|
||
## TS 内网路由模式
|
||
$ sudo tailscale up --advertise-exit-node --accept-routes --advertise-routes=172.16.1.0/24 --reset
|
||
```
|
||
|
||
执行命令后,TS 将自动显示登录链接,只需根据链接进行登录操作即可。
|
||
|
||
至此,TS 服务器已配置完成。
|
||
|