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 机制的详细解释:

  1. 气球膨胀:
  • 当物理主机需要回收内存时,虚拟机管理程序(Hypervisor)会请求虚拟机释放一些内存。
  • 虚拟机内的 virtio_balloon 驱动程序会分配一块内存,并将其“膨胀”,即将这块内存标记为不可用,然后通知虚拟机管理程序。
  • 这块内存实际上变成了空闲内存,供物理主机上的其他虚拟机或进程使用。
  1. 气球收缩:
  • 当物理主机内存压力减小或虚拟机需要更多内存时,虚拟机管理程序会请求虚拟机恢复部分被释放的内存。
  • virtio_balloon 驱动程序会“收缩”这块内存,将其重新标记为可用。
  • 这块内存再次变得可供虚拟机使用。

软件源

更换软件源

个人比较喜欢中国科技技术大学镜像站,修改方式直接访问官方然后点对应系统的『Help』就行了。

编码设置

设置终端文本

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

修改主机名

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: yes
  • NTP service: active

ACL

Access Control List,也就是“访问控制列表”。

它是在传统 Linux 权限 rwx 基础上的补充。

普通权限只能控制三类:

  • 属主 owner
  • 属组 group
  • 其他人 others

但 ACL 可以做到更细粒度,比如:

  • 单独给某个用户目录读写权限
  • 单独给某个组额外权限
  • 给新建文件设置默认继承权限

比如你想让 www-data 和 backup 都能访问同一个目录,普通 chmod 往往不太方便,这时 ACL 就很好用。

安装

terminal
apt update
apt install acl

常用命令

查看 ACL:

terminal
getfacl 文件名

给用户加权限:

terminal
setfacl -m u:用户名:rwx 文件名

给组加权限:

terminal
setfacl -m g:组名:r-x 文件名

删除某个用户的 ACL:

terminal
setfacl -x u:用户名 文件名

递归设置目录:

terminal
setfacl -R -m u:www-data:rwx /data/test

设置默认 ACL,让目录下新文件继承权限:

terminal
setfacl -d -m u:www-data:rwx /data/test

单用户模式修改密码

一、进入 GRUB 编辑界面

开机时:

👉 在 GRUB 菜单界面按 e(编辑启动项)


二、修改启动参数

找到这一行(很关键):

terminal
linux /boot/vmlinuz-xxx root=UUID=xxx ro quiet

把里面的:

terminal
ro

改成:

terminal
rw init=/bin/bash

👉 最终类似:

terminal
linux /boot/vmlinuz-xxx root=UUID=xxx rw init=/bin/bash

三、启动进入单用户模式

按:

terminal
Ctrl + X

或者:

terminal
F10

👉 这时候会直接进一个 root shell(无需密码)


四、修改密码

直接执行:

terminal
passwd

或改指定用户:

terminal
passwd 用户名

五、同步并重启

terminal
sync
reboot -f

开启远程桌面 XRDP

安装 xrdp

terminal
sudo apt-get update
sudo apt-get install --no-install-recommends -y xrdp xorgxrdp xfce4 xfce4-terminal dbus-x11

创建登录用户:

terminal
sudo adduser digvps
sudo usermod -aG sudo digvps

给这个用户设置 XFCE session:

terminal
sudo -u crawler sh -c 'echo xfce4-session > ~/.xsession'

重启 xrdp:

terminal
sudo systemctl restart xrdp

最新内容第一时间推送到你的邮箱,不错过任何重要更新。

通过 RSS 阅读器订阅,随时随地获取最新内容更新。

订阅 RSS

Copyright © 2026 DigVPS