14 KiB
14 KiB
服务器运行与维护常用命令
物理机
Proxmox 9
硬盘
sudo df –ah # 磁盘空间使用情况
sudo fdisk -l # 列出所有分区
sudo cfdisk /dev/sda # 管理某块硬盘
sudo sgdisk -p /dev/sda # 列出所有分区 (GPT 硬盘)
sudo sgdisk -e /dev/sda # 修复 GPT 分区表
# 块设备
lsblk
# LVM 物理卷
pvs
# LVM 卷组
vgs
# LVM 逻辑卷
lvs
网络 (Debian 13 Server)
sudo nano /etc/network/interfaces # 网络接口配置
sudo resolvectl status # DNS 管理
ls -al /etc/resolv.conf # 确认 DNS 配置托管情况
APT 镜像源 - 清华源 (Debian 13 Server)
sudo nano /etc/apt/sources.list
# Or
sudo nano /etc/apt/sources.list.d/debian.sources
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/debian
Suites: trixie trixie-updates trixie-backports
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
Types: deb
URIs: https://security.debian.org/debian-security
Suites: trixie-security
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
创建虚拟机
服务初始化
Maria
推送 GitHub 私钥
# 在 CatTomServer1 处执行
rsync -avP /home/cattom/.ssh/cattomgithub [server_id]:/root/.ssh/
# 指定用户名与 IP
rsync -avP /home/cattom/.ssh/cattomgithub root@[server_ip]:/root/.ssh/
# 指定端口、私钥、用户名和 IP
rsync -avP -e 'ssh -p 25800 -i /home/cattom/.ssh/Maria' /home/cattom/.ssh/cattomgithub root@[server_ip]:/root/.ssh/
编辑 SSH 客户端配置
sudo nano ~/.ssh/config
Host github.com
HostName ssh.github.com
User git
Port 443
PreferredAuthentications publickey
IdentityFile "~/.ssh/cattomgithub"
测试能否正常连接: ssh -T git@github.com
克隆 Maria 仓库
git clone git@github.com:cattomgithub/maria.git
cd maria/script && chmod +x *.sh && ./maria.sh
HomeLab
推送 Gitea 私钥
# 在 CatTomServer1 处执行
rsync -avP /home/cattom/.ssh/cattom-gitea [server_id]:/home/cattom/.ssh/
# 指定用户名与 IP
rsync -avP /home/cattom/.ssh/cattom-gitea root@[server_ip]:/home/cattom/.ssh/
# 指定端口、私钥、用户名和 IP
rsync -avP -e 'ssh -p 25800 -i /home/cattom/.ssh/CatTomServer' /home/cattom/.ssh/cattom-gitea root@[server_ip]:/home/cattom/.ssh/
编辑 SSH 客户端配置
sudo nano ~/.ssh/config
Host gitea.cattom.site
HostName gitea.cattom.site
User git
Port 25801
PreferredAuthentications publickey
IdentityFile "~/.ssh/cattom-gitea"
测试能否正常连接: ssh -T git@gitea.cattom.site
克隆 HomeLab 仓库
git clone git@gitea.cattom.site:cattom/HomeLab.git
cd HomeLab/script && chmod +x *.sh && ./init.sh
修改 SSH 配置
启动 SSH 服务
sudo systemctl enable ssh && sudo systemctl restart ssh && sudo systemctl status ssh
配置 SSH 密钥: 在 CatTomServer1 终端执行
# Maria
ssh-copy-id -i /home/cattom/.ssh/Maria.pub root@[server_ip]
# HomeLab
ssh-copy-id -i /home/cattom/.ssh/CatTomServer.pub cattom@[server_ip]
# 指定端口
ssh-copy-id -p 25800 ...
若 ssh-copy-id 执行不成功,可将对应公钥添加至 $HOME/.ssh/authorized_keys 文件的末尾。
修改 SSH 配置
sudo tee /etc/ssh/sshd_config.d/00-user.conf >/dev/null <<'EOF'
Port 25800
PermitRootLogin prohibit-password
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
AuthenticationMethods publickey
EOF
sudo chown root:root /etc/ssh/sshd_config.d/00-user.conf
sudo chmod 644 /etc/ssh/sshd_config.d/00-user.conf
检查 SSH 配置
# 检查配置语法
sudo /usr/sbin/sshd -t
# 查看配置是否正确生效
sudo /usr/sbin/sshd -T
重启 SSH 服务以刷新配置: sudo systemctl restart ssh
安装业务程序 (按需安装)
首先,通过Maria管理工具安装需要的业务工具: maria - ++2++
Traefik
请到 Releases - traefik/traefik 检查最新版本。
mkdir /root/traefik
cd /root/traefik || exit
touch acme.json && chmod 600 acme.json
wget -c [link]
tar -zxvf [file_name] && rm [file_name] LICENSE.md CHANGELOG.md && mv traefik /usr/local/bin/traefik
ln -s /root/maria/config/traefik/static.yaml /root/traefik/static.yaml
ln -s /root/maria/config/${SERVER}/traefik.yaml /root/traefik/dynamic.yaml
ln -s /root/maria/config/systemd/traefik.service /etc/systemd/system/traefik.service && sudo systemctl daemon-reload
sudo systemctl enable traefik.service && sudo systemctl restart traefik.service
阿里云 CLI
注意: 请到 RAM 访问控制 按照业务需求生成 AccessKey.
cd /root || exit
/bin/bash -c "$(curl -fsSL https://aliyuncli.alicdn.com/install.sh)"
aliyun configure set --profile profile1 --mode AK --access-key-id [AccessKeyID] --access-key-secret [AccessKeySecret] --region "cn-shenzhen"
阿里云 CLI 现已集成 ossutil,示例:
# Example 1
aliyun ossutil sync /root/backup/ oss://cattom/${SERVER}/backup/ --force --update --delete -e oss-cn-shenzhen.aliyuncs.com
# Example 2
aliyun ossutil sync /root/blog/site oss://cattom-blog --force --update --delete --region cn-hongkong
Flexget
安装 Flexget。
cd /root || exit
sudo apt -y install python3 python3-full python3-pip python3.12-venv
python3 -m venv /root/flexget/
/root/flexget/bin/pip install --upgrade pip setuptools
/root/flexget/bin/pip install flexget
测试配置并启动后台进程。
sudo ln -s /root/maria/config/flexget/config.yml /root/flexget/config.yml
/root/flexget/bin/flexget -c /root/flexget/config.yml --test execute
/root/flexget/bin/flexget -c /root/flexget/config.yml daemon start -d --autoreload-config
设置开机自启动。
(
crontab -u $(whoami) -l
echo "@reboot /root/flexget/bin/flexget -c /root/flexget/config.yml daemon start -d --autoreload-config"
) | crontab -u $(whoami) -
配置 alias。
echo 'alias flexget="/root/flexget/bin/flexget -c /root/flexget/config.yml"' >> /root/.bashrc
配置防火墙
!!! warning "注意"
所有经由 Tailscale 的连接都不需要在 UFW 中放行或加以限制。
所有在 Docker Compose 文件中没有定义特定监听地址的端口都不需要在 UFW 中放行。
HomeLab 非标端口开放表
| 服务器 | 端口 | 来源IP/IP段 | 目标IP/IP段 | 协议 | 备注 |
|---|---|---|---|---|---|
| 1 | 3493 | 192.168.1.0/24 | / | / | NUT |
| 1 | 8090 | / | / | / | UpSnap |
| 1 | 9090 | / | / | / | One-KVM 1 |
| 服务器 | 端口 | 来源IP/IP段 | 目标IP/IP段 | 协议 | 备注 |
|---|---|---|---|---|---|
| 3-1 | 8080 | 192.168.1.0/24 | / | / | Dagu |
Maria 非标端口开放表
| 服务器 | 端口 | 来源IP/IP段 | 目标IP/IP段 | 协议 | 备注 |
|---|---|---|---|---|---|
| 2 | 7100 | / | / | / | Frps 监听端口 |
| 2 | 18080 | 127.0.0.1 | / | / | Bitwarden |
| 2 | 18081 | 127.0.0.1 | / | / | Bitwarden(yuan) |
| 2 | 5230 | 127.0.0.1 | / | / | Memos |
| 2 | 3002 | 127.0.0.1 | / | / | Homepage |
| 2 | 3100 | 127.0.0.1 | / | / | Gitea |
| 2 | 25801 | / | / | / | Gitea SSH |
| 服务器 | 端口 | 来源IP/IP段 | 目标IP/IP段 | 协议 | 备注 |
|---|---|---|---|---|---|
| 3 | 6800 | 127.0.0.1 | / | / | Aria2 RPC |
| 3 | 6888 | / | / | / | Aria2 BT |
| 3 | 6888 | / | / | UDP | Aria2 BT |
| 3 | 9100 | / | / | / | Node Exporter 2 |
# 重置防火墙
sudo ufw reset
# 添加默认条件
sudo ufw default allow outgoing # 默认允许所有数据出站
sudo ufw default deny incoming # 默认禁止所有数据入站
# SSH
sudo ufw allow 25800 # Maria
sudo ufw allow from 192.168.1.0/24 to any port 25800 # HomeLab - lan
# Web
sudo ufw allow 80 && sudo ufw allow 443
# Example 1
sudo ufw allow 7100
# Example 2
sudo ufw allow from 127.0.0.1 to any port 18080
# Example 3
sudo ufw allow from 192.168.1.0/24 to any port 8080
# Example 4
sudo ufw allow 6888/udp
# 启用日志
sudo ufw logging medium
# 列出端口开放情况
sudo ufw status numbered
# 启用防火墙
sudo ufw enable
加载 Docker 容器
sudo docker compose -f /root/maria/config/"${SERVER}"/docker-compose.yml pull
sudo docker compose -f /root/maria/config/"${SERVER}"/docker-compose.yml up -d --remove-orphans
sudo docker system prune -f
# 可选
sudo systemctl restart traefik
Linux 内核网络栈调优 (可选)
编辑 /etc/sysctl.conf,在末尾添加以下内容:
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_rmem = 16384 262144 8388608
net.ipv4.tcp_wmem = 32768 524288 16777216
net.core.somaxconn = 8192
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.wmem_default = 2097152
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_max_syn_backlog = 10240
net.core.netdev_max_backlog = 10240
net.netfilter.nf_conntrack_max = 1000000
net.netfilter.nf_conntrack_tcp_timeout_established = 7200
net.core.default_qdisc = fq_codel
net.ipv4.tcp_congestion_control = bbr
net.ipv4.tcp_slow_start_after_idle = 0
刷新配置。
sysctl -p
验证是否成功启动 BBR。
lsmod | grep bbr
若出现 tcp_bbr 的输出,说明开启成功。
配置 Actions
GitHub Actions
转至 cattomgithub/maria - Settings - Secrets - Actions
修改以下配置项:
- SSH_HOSTS: foo,bar
Gitea Actions
(待施工)
NFS 配置
# Install
sudo apt -y update && sudo apt -y install nfs-kernel-server nfs-common
# Modify firewall on host
sudo ufw allow from [client_ip] to any port nfs
# Create mount point on client
sudo mkdir -p [/path/on/client]
# Mount directories on client
sudo mount [host_ip]:[/path/on/host] [/path/on/client]
# Check stats on client
sudo df -h
# Mount the directories at boot
sudo echo "[host_ip]:[/path/on/host] [/path/on/client] nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0" >> /etc/fstab
# Unmount NFS
sudo umount [/path/on/client]
macOS 恢复挂载网络存储(Samba)
macOS 完成更新后,依照以下流程恢复挂载 NFS:
- 添加挂载点:
sudo su
echo "/System/Volumes/Data/mnt/CTS2 auto_mount_config -nosuid,noowners" >> /etc/auto_master
- 确认挂载配置有效性:
cat /etc/auto_mount_config
- 恢复挂载并验证:
automount -vc
ls -al /System/Volumes/Data/mnt/CTS2
rsync
rsync 参数解释:
-a: 归档模式,保留文件的权限、时间戳、软链接、属主和属组。-v: 详细模式,输出同步过程中的文件名。-z: 在传输时进行压缩。-P: 等同于--partial--progress。--progress会显示每个文件的传输进度,--partial允许断点续传。--delete: 删除目标目录中,源目录内已不存在的文件,即完全镜像同步。
rsync 对路径末尾的斜杠敏感:
/local/source/folder/(有斜杠): 只同步文件夹里面的内容到目标目录。/local/source/folder(无斜杠): 将folder这个目录本身连同里面的内容,整个放入目标目录中。
推送文件
# 将本地文件推送到远程服务器 (Push)
rsync -avP /root/backup/grafana-storage.tar.gz root@100.97.10.99:/root/backup/
同步文件夹
# 远程服务器与本地文件夹保持同步 (Push)
rsync -avzP --delete /local/source/folder/ [server_id]:/remote/destination/folder/
# 本地文件夹与远程服务器保持同步 (Pull)
rsync -avzP --delete [server_id]:/local/source/folder/ /remote/destination/folder/
tar
压缩:
# 先进入 [target] 的父目录
cd /root/backrest/tmp/grafana-storage-backrest-restore
tar -czpf /root/backup/grafana-storage.tar.gz grafana-storage
解压缩:
# 先进入进行解压缩的目录
cd /root/backup
tar -xzpf grafana-storage.tar.gz
# 解压缩产物: /root/backup/grafana-storage/
TCP ping 与 UDP ping
TCP ping:
sudo apt install hping3
hping3 -S -p 80 -c 3 www.cisco.com
UDP ping:
sudo nmap -sU -p 161 itsfoss.com
netcat -v -u -z itsfoss.com 161
SSH 隧道
ssh -L [local-port]:[lan-ip]:[lan-port] [username]@[remote-ip]
# Example:
ssh -L 8007:192.168.1.6:8007 cattom@100.116.56.77
进入 Docker 容器的终端
sudo docker exec -it [container_name] /bin/bash
# Or
sudo docker exec -it [container_name] /bin/sh