Debian
Debian 相关的系统配置与应用教程
📘 网络配置
systemd-networkd 静态IP
编辑文件
terminal
vi /etc/systemd/network/xxxx.network
terminal
[Match]
MACAddress=9c:6b:00:68:14:6e
[Network]
# IPv4 配置
Address=192.0.2.45/24
Gateway=192.0.2.1
DNS=1.1.1.1
# IPv6 配置
Address=2001:db8:abcd:200:30:abcd:12ef:3456/64
Gateway=2001:db8:abcd::1
DNS=2606:4700:4700::1111
重启服务
terminal
sudo systemctl restart systemd-networkd
sudo systemctl restart systemd-resolved
验证
terminal
ip addr show
ip -6 route show
ping6 google.com
interfaces 静态IP
编辑文件
terminal
vi /etc/network/interfaces
terminal
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug eno2np0
iface eno2np0 inet static
address 192.0.2.10/24
gateway 192.0.2.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 1.1.1.1
iface eno2np0 inet6 static
address 2001:db8:1234::10
netmask 64
gateway 2001:db8:1234::1
配置多个静态ip
terminal
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens3
iface ens3 inet static
address 192.168.50.129/24
gateway 192.168.50.1
netmask 255.255.255.0
dns-nameservers 8.8.8.8 8.8.4.4
iface ens3 inet static
address 192.168.50.156/24
重启服务
terminal
systemctl restart networking
interfaces 动态IP
编辑文件
terminal
vi /etc/network/interfaces
terminal
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens5
iface ens5 inet dhcp
iface ens5 inet6 dhcp
重启服务
terminal
systemctl restart networking
Netplan 配置 LACP
Netplan 其实就是一个“前端”,它的 YAML 配置文件在 /etc/netplan/*.yaml,然后根据你的后端(renderer)去生成配置:
- 如果 renderer 是 networkd → 配置会转成 /run/systemd/network/…
- 如果 renderer 是 NetworkManager → 配置会交给 /etc/NetworkManager/system-connections/
编辑文件
terminal
vi /etc/network/interfaces
terminal
network:
version: 2
renderer: networkd
ethernets:
enp1s0f0:
optional: true
enp1s0f1:
optional: true
bonds:
bond0:
interfaces:
- enp1s0f0
- enp1s0f1
parameters:
mode: 802.3ad
lacp-rate: fast
mii-monitor-interval: 100
bridges:
vmbr0:
interfaces:
- bond0
parameters:
stp: false
forward-delay: 0
addresses:
- 2001:db8::1/64
- 192.0.2.64/24
routes:
- to: default
via: 2001:db8::1
on-link: true
- to: default
via: 192.0.2.1
on-link: true
应用配置
修改好 YAML 后执行:
terminal
netplan apply
如果怕断网,可以先测试:
terminal
# 会给你一个 120 秒确认时间,失败会回滚
netplan try
📘 硬件信息查看
主板信息
terminal
dmidecode -t baseboard
内存信息
dmidecode
terminal
dmidecode -t memory
lshw
terminal
sudo apt install lshw
sudo lshw -class memory
查看温度
terminal
apt install lm-sensors
sensors
查看 KVM 服务器内存是否启用virtio_balloon
terminal
lsmod | grep virtio_balloon
工作原理
virtio_balloon 是一种用于虚拟化环境中的内存管理技术,特别是用于动态调整虚拟机 (VM) 内存使用的机制。它由 Virtio 提供,主要用于 KVM (Kernel-based Virtual Machine) 和 QEMU 等虚拟化平台。下面是对 virtio_balloon 机制的详细解释:
- 气球膨胀:
- 当物理主机需要回收内存时,虚拟机管理程序(Hypervisor)会请求虚拟机释放一些内存。
- 虚拟机内的
virtio_balloon驱动程序会分配一块内存,并将其“膨胀”,即将这块内存标记为不可用,然后通知虚拟机管理程序。 - 这块内存实际上变成了空闲内存,供物理主机上的其他虚拟机或进程使用。
- 气球收缩:
- 当物理主机内存压力减小或虚拟机需要更多内存时,虚拟机管理程序会请求虚拟机恢复部分被释放的内存。
virtio_balloon驱动程序会“收缩”这块内存,将其重新标记为可用。- 这块内存再次变得可供虚拟机使用。
📘 软件源
更换软件源
个人比较喜欢中国科技技术大学镜像站,修改方式直接访问官方然后点对应系统的『Help』就行了。
📘 UFW - 防火墙设置
基础使用
安装
terminal
apt install ufw
配置文件介绍(可选)
/etc/ufw/user.rules这是存储用户自定义规则的主要文件,包含了所有手动添加的规则。它是 ufw 在启用时加载的主要文件。/etc/ufw/user6.rules这是存储针对 IPv6 网络的用户自定义规则的文件,类似于user.rules,但是仅适用于 IPv6 地址。/etc/ufw/applications.d/该目录用于存放应用程序的配置文件,每个文件通常对应一种应用服务的预定义规则。例如,OpenSSH、HTTP、HTTPS 等服务的规则可以在这里定义。/var/log/ufw.log如果启用了日志记录,ufw会将日志写入这个文件,记录哪些连接被允许或拒绝。/etc/ufw/after.rules是 UFW (Uncomplicated Firewall) 的配置文件之一,用于定义在 UFW 启动时应用的规则,它在 before.rules 后面执行。具体来说,after.rules 用来设置一些防火墙规则,这些规则会在所有其他默认规则和自定义规则之后被应用。
规则写法示例
terminal
# 允许ssh
ufw allow ssh
# 允许iperf3
ufw allow iperf3
# 允许指定端口
ufw allow 1432
# 允许指定端口协议
ufw allow 80/tcp
ufw allow 5555/tcp
# 允许某个 IP 所有端口访问
ufw allow from 203.0.113.5
# 允许指定 IP、端口和协议
ufw allow from 192.168.1.100 to any port 80 proto tcp
删除规则
terminal
## 删除相关规则
ufw delete allow 80/tcp
## 显示现有规则
ufw status numbered
## 删除指定编号规则
ufw delete 2
启用防火墙
terminal
ufw enable
📘 编码设置
设置终端文本
terminal
localectl
localectl set-locale LANG=en_US.UTF-8
export LC_ALL="en_US.UTF-8"
📘 网络测试
HTTP 下载测试
terminal
# 启动简易 http 服务
python3 -m http.server
# 客户端
# 直接用浏览器下载或其他工具下载
wget http://你服务器的IP:8000/5GB_file
📘 NFS (Network File System)
服务端配置
安装 NFS 服务端
terminal
apt update
apt install nfs-kernel-server -y
创建共享目录
terminal
mkdir -p /srv/nfs/share
chown nobody:nogroup /srv/nfs/share
chmod 777 /srv/nfs/share
配置导出目录
编辑/etc/exports 文件,添加以下配置
terminal
# rw:读写
# async:异步写,提高性能
# insecure:允许客户端使用 非特权端口 (>1024)
# no_subtree_check:禁用子目录检查,减少开销
# no_root_squash:允许 root 用户不被降级
# 允许所有客户端 IP 访问
/srv/nfs/share *(rw,async,insecure,no_subtree_check,no_root_squash)
# 限定某个子网
/srv/nfs/share 192.168.1.0/24(rw,async,insecure,no_subtree_check,no_root_squash)
# 限定多个子网
/srv/nfs/share 192.168.1.0/24(rw,async,no_subtree_check,no_root_squash) 10.0.0.0/16(rw,async,no_subtree_check,no_root_squash)
# 限定单个 IP
/srv/nfs/share 192.168.1.10(rw,async,no_subtree_check,no_root_squash)
# 支持主机名
/srv/nfs/share nfs.digvps.com(rw,async,no_subtree_check,no_root_squash)
应用配置并启动
terminal
exportfs -ra
exportfs -v # 查看启用的共享
systemctl enable nfs-server
systemctl restart nfs-server
客户端配置
安装 NFS 客户端
terminal
apt update
apt install nfs-common -y
创建挂载点
terminal
mkdir -p /mnt/nfs_share
挂载
terminal
# vers=4.2:强制使用 NFSv4.2,性能和特性最好
# tcp:使用 TCP 协议
# nconnect=8:并行 8 个 TCP 连接,大幅提高多线程 IO 性能(Linux 内核 ≥ 5.3)
mount -o vers=4.2,tcp,nconnect=8 192.168.1.10:/srv/nfs/share /mnt/nfs_share
卸载
terminal
umount /mnt/nfs_share
开机自动挂载(可选)
编辑/etc/fstab文件添加以下内容
terminal
192.168.1.10:/srv/nfs/share /mnt/nfs_share nfs vers=4.2,tcp,nconnect=8 0 0
测试
terminal
mount -a
📘 文件传输
Rsync
terminal
# -a 归档模式,保留权限、时间戳等属性
# -v 显示详细输出
# -z 传输时压缩数据
# --delete 删除目标中源目录没有的文件
# 将本地文件传到远程服务器(push)
rsync -avz --delete ./dist/ 服务器的用户名@服务器的IP:/var/www/digvps/dist/
# 将远程文件传输到本地(pull)
rsync -a username@remote_host:/home/username/dir1 place_to_sync_on_local_machine
# 指定 ssh 端口
rsync -avz -e "ssh -p $port" /local/path/ user@remoteip:/path/to/files/
# 设置备份带宽
rsync --bwlimit=100 -avzhe ssh /var/lib/rpm/ root@remoteip:/root/tmprpm/
SFTP(Secret File Transfer Protocol)
交互模式
连接服务器
terminal
sftp root@47.254.16.58
常用操作
terminal
pwd # 显示远程服务器当前目录
lpwd # 显示本地机器当前目录
ls # 列出远程目录文件
lls # 列出本地目录文件
cd /var/www # 切换远程目录
lcd ./dist # 切换本地目录
get file.txt # 下载远程文件到本地
get -r dist/ # 递归下载整个目录
put file.txt # 上传本地文件到远程
put -r dist/ # 递归上传整个目录
mkdir test # 在远程创建目录
rmdir test # 删除远程目录
rm file.txt # 删除远程文件
rename old.txt new.txt # 重命名远程文件
chmod 644 file.txt # 修改远程文件权限
退出 sftp 会话
terminal
sftp> exit
非交互模式
管道传输
terminal
# 上传单个文件
echo "put ./dist/index.html /var/www/digvps/" | sftp 用户名@服务器IP
# 上传整个目录
echo "put -r ./dist /var/www/digvps/" | sftp 用户名@服务器IP
SCP (Secure Copy Protocol)
terminal
# 上传文件到服务器
scp ./index.html 用户名@服务器IP:/var/www/digvps/
# 上传整个目录
scp -r ./dist 用户名@服务器IP:/var/www/digvps/
# 下载文件到本地
scp 用户名@服务器IP:/var/www/digvps/config.json ./config.json
# 下载整个目录
scp -r 用户名@服务器IP:/var/www/digvps/dist ./dist_backup
# 指定端口(比如 2222)
scp -P 2222 ./index.html 用户名@服务器IP:/var/www/digvps/
# 使用 SSH key
scp -i ~/.ssh/id_rsa ./index.html 用户名@服务器IP:/var/www/digvps/
# 服务器之间直接拷贝(不经过本地)
scp 用户1@服务器1:/var/www/digvps/file.txt 用户2@服务器2:/var/www/backup/
lrzsz
安装
terminal
apt install lrzsz
使用
terminal
# 从本地上传文件到服务器
rz
# 从服务器下载文件到本地
sz
未集成 ZMODEM 协议终端配置(可选)
Xshell、SecureCRT 等终端自带对 ZMODEM 协议的支持,可直接进行文件传输。但并非所有终端都支持该协议,这里以 macOS 下的 iTerm2 为例,展示其配置方法。
- 将iterm2-zmodem项目中的
iterm2-send-zmodem.sh和iterm2-recv-zmodem.sh放到/usr/local/bin/目录 - 在iTerm2里面的Trigger(
Preferences->Profiles->Advanced) 中进行以下配置
terminal
Regular expression: rz waiting to receive.\*\*B0100
Action: Run Silent Coprocess
Parameters: /usr/local/bin/iterm2-send-zmodem.sh
Instant: checked
Regular expression: \*\*B00000000000000
Action: Run Silent Coprocess
Parameters: /usr/local/bin/iterm2-recv-zmodem.sh
Instant: checked
📘 修改主机名
terminal
hostnamectl set-hostname digvps
hostnamectl status
📘 常用命令
批量查找文件做内容替换
terminal
find ./ -type f -name consumer.xml -exec sed -i "s/aaaa/bbbb/g" {} \;
# find # 查找命令
# ./ # 在当前目录及其子目录中查找
# -type f # 只查找普通文件(不包含目录、符号链接等)
# -name consumer.xml # 文件名匹配为 consumer.xml
#
# -exec # 对查找到的每个文件执行后面的命令
# sed # 流编辑器,用来修改文件内容
# -i # 就地修改文件(直接改原文件,不输出到终端)
# "s/aaaa/bbbb/g" # 替换命令,把文件中的 aaaa 全部替换为 bbbb
# {} # find 查到的文件会替换这里
# \; # -exec 语法结尾,表示命令结束d
批量查找文件作拷贝打包
terminal
(find . -name "*.txt" | tar -czf test.tar -T -) && cp -f test.tar /home/.
用户请求所建立的网络链接状态
terminal
apt install net-tools
netstat -n | awk '/^tcp/ { ++S[$NF] } END { for (a in S) print a, S[a] }'
提取主机上的 IP 信息
terminal
ip a | grep "global" | awk '{print $2}' | awk -F'/' '{print $1}'
全局搜索文本
terminal
grep -Rni "bond0" /etc 2>/dev/null
📘 进程相关
查看进程对应可执行文件的路径
terminal
readlink -f /proc/2169424/exe
# 批量查看
for p in $(pgrep foo); do readlink -f /proc/$p/exe; done | sort -u
查看父进程
terminal
ps -o pid,ppid,cmd -p 2169424
pstree -ap | grep foo
批量杀进程
terminal
pkill -9 foo
📘 端口相关
查看正在监听的端口
terminal
# 查看所有监听端口(TCP/UDP)
sudo ss -lntup
# 只看 TCP 监听
sudo ss -lntp
# 只看 UDP 监听
sudo ss -lnup
terminal
# 列出所有监听端口
sudo lsof -nP -iTCP -sTCP:LISTEN
sudo lsof -nP -iUDP
# 查某个端口是谁占用(例:80)
sudo lsof -nP -i :80
📘 时间设定
timesyncd
检查是否安装 systemd-timesyncd
terminal
dpkg -l | grep systemd-timesyncd
如果没有输出,说明未安装,执行:
terminal
apt update
apt install systemd-timesyncd -y
启用并启动服务
terminal
systemctl unmask systemd-timesyncd
systemctl enable systemd-timesyncd
systemctl start systemd-timesyncd
开启 NTP 自动同步
terminal
timedatectl set-ntp true
查看当前状态:
terminal
timedatectl status
正常情况下应看到:
System clock synchronized: yes
NTP service: active
(可选)修改 NTP 服务器源
编辑配置文件:
terminal
nano /etc/systemd/timesyncd.conf
示例配置:
[Time]
NTP=time.cloudflare.com time.google.com ntp.aliyun.com
FallbackNTP=pool.ntp.org
保存后重启服务:
terminal
systemctl restart systemd-timesyncd
强制重新同步一次
terminal
systemctl restart systemd-timesyncd
查看最近日志确认是否成功同步:
terminal
journalctl -u systemd-timesyncd -n 20
如果看到类似:
Synchronized to time server ...
说明同步成功。
快速检查是否已经同步
terminal
timedatectl
关键看:
System clock synchronized: yesNTP service: active

