上一篇 下一篇 分享链接 返回 返回顶部

Debian 12/11 上手不踩坑:软件源、网络、SSH 与常用配置一次理清

发布人:慈云数据-客服中心 发布时间:13小时前 阅读量:4

Debian 使用避坑指南|附配置文件

Debian 是一款非常经典、稳定、社区驱动的 Linux 发行版。它不像某些发行版那样追求“最新”,而是更强调可靠性、安全性和可维护性。因此,Debian 常被用于服务器、开发环境、软路由、NAS、桌面系统以及各类生产环境。

不过,Debian 的“稳定”并不意味着“开箱即用没有坑”。尤其是从 Ubuntu、CentOS、Arch 或 Windows 转过来的用户,经常会遇到软件源、驱动、权限、网络、中文输入法、时区、服务管理、APT 依赖、桌面环境等问题。

本文整理了一份 Debian 使用避坑指南,并附带常用配置文件示例,适合 Debian 12 / Debian 11 用户参考。服务器和桌面用户都可以从中找到有用内容。


一、安装 Debian 前需要知道的几件事

1. Debian Stable 不是“软件最新版”

Debian Stable 的核心优势是稳定。它的软件包版本通常不会特别新,例如:

  • Nginx 可能不是最新版;
  • PHP、Python、Node.js 版本可能落后;
  • GNOME、KDE 桌面环境更新节奏较慢;
  • 内核版本相对保守。

这不是缺点,而是 Debian 的设计目标。

如果你需要最新软件,可以考虑:

  • 使用 backports
  • 使用官方第三方仓库;
  • 使用 Docker;
  • 使用 Flatpak / AppImage;
  • 从源码编译;
  • 改用 Debian Testing 或 Sid,但不建议生产环境使用。

2. 安装镜像选择很重要

Debian 官方镜像分为多种:

  • netinst 网络安装镜像
  • DVD 镜像
  • live 镜像
  • 带非自由固件的安装镜像

如果你的电脑需要 Wi-Fi 固件、显卡固件、网卡固件,建议优先下载包含 firmware 的镜像。

从 Debian 12 开始,非自由固件的处理相比之前更友好,但仍然建议选择官方带固件镜像,避免安装过程中出现:

  • 无法识别无线网卡;
  • 无法连接网络;
  • 显卡黑屏;
  • 声卡不可用;
  • 触摸板异常。

二、软件源配置避坑

Debian 使用 APT 管理软件包,软件源配置非常关键。很多新手的问题都来自错误的软件源。

Debian 的软件源文件主要位于:

/etc/apt/sources.list

也可能存在于:

/etc/apt/sources.list.d/

三、Debian 12 推荐软件源配置

以下是 Debian 12 Bookworm 的示例配置。

注意:请根据自己的地区选择合适镜像源。国内用户可使用清华、阿里云、中科大等镜像源。

1. 官方源配置

# /etc/apt/sources.list

deb http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware
deb http://deb.debian.org/debian bookworm-updates main contrib non-free non-free-firmware
deb http://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware

更新软件包索引:

sudo apt update

2. 清华源配置

# /etc/apt/sources.list

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware

3. 中科大源配置

# /etc/apt/sources.list

deb https://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware

4. 阿里云源配置

# /etc/apt/sources.list

deb https://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware

四、软件源配置常见坑

1. 不要混用不同 Debian 版本的软件源

例如你的系统是 Debian 12 Bookworm,却配置了 Debian 11 Bullseye 或 Debian Testing 的源:

deb http://deb.debian.org/debian bullseye main
deb http://deb.debian.org/debian testing main

这很容易导致依赖混乱,甚至系统无法正常升级。

查看当前系统版本:

cat /etc/debian_version
lsb_release -a

如果没有 lsb_release 命令,可安装:

sudo apt install lsb-release

2. 不建议长期混用 Testing / Sid 源

很多人为了安装新版软件,会把 Testing 或 Sid 源加入 Stable 系统。这种做法风险很高。

可能导致:

  • libc 版本升级;
  • systemd 版本升级;
  • 桌面环境依赖被替换;
  • APT 依赖关系破坏;
  • 生产服务器服务异常。

如果确实需要新版软件,优先考虑:

bookworm-backports

3. 正确启用 backports

Debian Backports 提供了从 Testing 回移植到 Stable 的较新软件包,适合安装新版内核、显卡驱动、部分开发工具。

Debian 12 启用 backports:

# /etc/apt/sources.list

deb http://deb.debian.org/debian bookworm-backports main contrib non-free non-free-firmware

更新索引:

sudo apt update

从 backports 安装软件时建议显式指定:

sudo apt install -t bookworm-backports linux-image-amd64

不要直接全量升级 backports:

sudo apt upgrade

通常不会自动升级到 backports 版本,但为了安全,仍然建议明确指定包名。


五、APT 使用避坑

APT 是 Debian 的核心工具,使用不当会造成不少问题。


1. 更新索引不等于升级系统

sudo apt update

只是更新软件包索引,不会升级软件。

真正升级软件需要:

sudo apt upgrade

如果涉及依赖变化,可以使用:

sudo apt full-upgrade

一般服务器日常维护推荐:

sudo apt update
sudo apt upgrade

系统大版本升级或内核升级时再考虑:

sudo apt full-upgrade

2. 不要随意执行 autoremove

sudo apt autoremove

这个命令会删除系统认为“不再需要”的依赖包。大多数时候没问题,但如果你曾经手动安装过桌面环境、驱动、内核或服务组件,可能会误删关键包。

执行前一定要仔细看将要删除的列表。

特别是看到以下包时要谨慎:

  • linux-image-*
  • firmware-*
  • gnome-*
  • kde-*
  • xserver-*
  • network-manager
  • openssh-server
  • grub-*

3. 软件包锁定

如果你不希望某个软件包被升级,可以使用:

sudo apt-mark hold package-name

取消锁定:

sudo apt-mark unhold package-name

查看被锁定的软件包:

apt-mark showhold

例如锁定 Nginx:

sudo apt-mark hold nginx

4. 清理缓存

APT 下载的软件包缓存位于:

/var/cache/apt/archives/

清理缓存:

sudo apt clean

只清理过期包:

sudo apt autoclean

六、sudo 权限避坑

Debian 默认安装时,如果你设置了 root 密码,普通用户可能不会自动加入 sudo 组。

如果执行:

sudo command

出现:

user is not in the sudoers file

说明当前用户没有 sudo 权限。

解决方法:切换到 root:

su -

安装 sudo:

apt install sudo

把用户加入 sudo 组:

usermod -aG sudo your_username

退出重新登录后生效。

检查用户所属组:

groups

七、SSH 配置避坑

服务器用户最常见的配置就是 SSH。Debian 默认可能已经安装 OpenSSH,也可能没有。

安装 SSH 服务:

sudo apt install openssh-server

查看状态:

systemctl status ssh

启动并设置开机自启:

sudo systemctl enable --now ssh

1. SSH 推荐配置文件

编辑:

sudo nano /etc/ssh/sshd_config

推荐配置:

# /etc/ssh/sshd_config

Port 22
Protocol 2

PermitRootLogin no
PasswordAuthentication yes
PubkeyAuthentication yes

PermitEmptyPasswords no
X11Forwarding no

ClientAliveInterval 300
ClientAliveCountMax 2

UseDNS no

如果你已经配置好密钥登录,建议关闭密码登录:

PasswordAuthentication no

重启 SSH:

sudo systemctl restart ssh

重要提醒:修改 SSH 配置前,建议保留一个已登录终端,不要立即关闭。确认新连接可用后再退出,避免把自己锁在服务器外面。


八、网络配置避坑

Debian 网络配置方式有多种,常见包括:

  • /etc/network/interfaces
  • NetworkManager
  • systemd-networkd

服务器通常使用 /etc/network/interfaces 或 systemd-networkd;桌面环境通常使用 NetworkManager。


九、interfaces 静态 IP 配置示例

编辑文件:

sudo nano /etc/network/interfaces

示例:

# /etc/network/interfaces

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 223.5.5.5 8.8.8.8

重启网络:

sudo systemctl restart networking

如果你使用的是 NetworkManager,不建议同时手动修改 interfaces 管理同一个网卡,否则可能冲突。

查看网卡名称:

ip addr

Debian 中网卡名可能不是 eth0,而是类似:

ens33
enp3s0
eno1

需要根据实际名称修改。


十、systemd-networkd 静态 IP 配置示例

创建配置文件:

sudo nano /etc/systemd/network/10-ens33.network

示例:

# /etc/systemd/network/10-ens33.network

[Match]
Name=ens33

[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=223.5.5.5
DNS=8.8.8.8

启用服务:

sudo systemctl enable --now systemd-networkd
sudo systemctl enable --now systemd-resolved

如果使用 systemd-resolved,可设置 resolv.conf:

sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf

查看状态:

networkctl
resolvectl status

十一、DNS 配置避坑

很多网络问题表面看是“无法访问网站”,实际是 DNS 解析失败。

测试网络连通性:

ping 8.8.8.8

测试 DNS:

ping debian.org

如果 IP 能 ping 通,域名不通,多半是 DNS 问题。

临时修改 DNS:

sudo nano /etc/resolv.conf

写入:

nameserver 223.5.5.5
nameserver 8.8.8.8

但需要注意,/etc/resolv.conf 可能会被 NetworkManager、systemd-resolved 或 DHCP 覆盖。


十二、防火墙配置避坑

Debian 默认不一定启用防火墙。服务器建议配置防火墙。

1. 使用 UFW

安装:

sudo apt install ufw

允许 SSH:

sudo ufw allow ssh

如果 SSH 使用自定义端口,例如 2222:

sudo ufw allow 2222/tcp

启用防火墙:

sudo ufw enable

查看状态:

sudo ufw status verbose

常用规则:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw deny 3306/tcp

注意:远程服务器启用防火墙前,一定先放行 SSH 端口。


2. nftables 基础配置

Debian 现代版本推荐 nftables。

配置文件:

sudo nano /etc/nftables.conf

示例:

#!/usr/sbin/nft -f

flush ruleset

table inet filter {
    chain input {
        type filter hook input priority 0;
        policy drop;

        iif "lo" accept
        ct state established,related accept

        tcp dport 22 accept
        tcp dport 80 accept
        tcp dport 443 accept

        ip protocol icmp accept
        ip6 nexthdr icmpv6 accept

        counter drop
    }

    chain forward {
        type filter hook forward priority 0;
        policy drop;
    }

    chain output {
        type filter hook output priority 0;
        policy accept;
    }
}

启用:

sudo systemctl enable --now nftables
sudo nft list ruleset

十三、中文环境与输入法避坑

桌面用户安装 Debian 后,常见问题是中文字体缺失、中文显示不美观、输入法无法切换。

安装中文字体:

sudo apt install fonts-noto-cjk fonts-wqy-microhei fonts-wqy-zenhei

安装 Fcitx5:

sudo apt install fcitx5 fcitx5-chinese-addons fcitx5-pinyin

设置环境变量:

sudo nano /etc/environment

加入:

GTK_IM_MODULE=fcitx
QT_IM_MODULE=fcitx
XMODIFIERS=@im=fcitx

重新登录桌面后生效。

如果使用 GNOME,也可以考虑 IBus,但 Fcitx5 在中文输入体验上通常更灵活。


十四、时区与时间同步避坑

查看当前时间和时区:

timedatectl

设置上海时区:

sudo timedatectl set-timezone Asia/Shanghai

启用 NTP 时间同步:

sudo timedatectl set-ntp true

如果服务器时间不准确,可能导致:

  • HTTPS 证书校验失败;
  • APT 更新报错;
  • 日志时间混乱;
  • 定时任务异常;
  • 数据库时间错误。

十五、主机名配置避坑

查看主机名:

hostnamectl

修改主机名:

sudo hostnamectl set-hostname debian-server

同时建议检查:

sudo nano /etc/hosts

示例:

127.0.0.1       localhost
127.0.1.1       debian-server

::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

如果 /etc/hosts 配置不当,某些程序启动时可能会出现 hostname 解析慢的问题。


十六、Swap 配置避坑

小内存服务器建议配置 Swap,尤其是 1GB 或 2GB 内存的 VPS。

查看 Swap:

free -h
swapon --show

创建 2GB Swap 文件:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

写入 /etc/fstab

sudo nano /etc/fstab

加入:

/swapfile none swap sw 0 0

调整 swappiness:

sudo nano /etc/sysctl.d/99-custom.conf

加入:

vm.swappiness=10

生效:

sudo sysctl -p /etc/sysctl.d/99-custom.conf

十七、系统优化配置示例

以下是一个常用的 sysctl 配置,适合普通服务器参考。

# /etc/sysctl.d/99-custom.conf

# 减少使用 swap 的倾向
vm.swappiness=10

# 文件句柄优化
fs.file-max=1048576

# TCP 优化
net.ipv4.tcp_fin_timeout=15
net.ipv4.tcp_keepalive_time=600
net.ipv4.tcp_tw_reuse=1

# 开启 BBR
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr

生效:

sudo sysctl --system

检查 BBR:

sysctl net.ipv4.tcp_congestion_control

输出如果是:

net.ipv4.tcp_congestion_control = bbr

说明已启用。


十八、日志与磁盘空间避坑

Debian 使用 systemd-journald 记录日志。如果不限制日志大小,长期运行可能占用较多磁盘。

查看日志占用:

journalctl --disk-usage

清理旧日志:

sudo journalctl --vacuum-time=7d

限制 journald 日志大小:

sudo nano /etc/systemd/journald.conf

配置示例:

# /etc/systemd/journald.conf

[Journal]
SystemMaxUse=500M
RuntimeMaxUse=200M
MaxRetentionSec=1month

重启服务:

sudo systemctl restart systemd-journald

十九、服务管理避坑

Debian 使用 systemd 管理服务。

常用命令:

systemctl status nginx
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl enable nginx
sudo systemctl disable nginx

查看失败服务:

systemctl --failed

查看服务日志:

journalctl -u nginx

实时查看:

journalctl -u nginx -f

很多服务修改配置后,不一定要 restart,可以使用 reload。例如 Nginx:

sudo nginx -t
sudo systemctl reload nginx

这样可以避免连接中断。


二十、Nginx 配置避坑

安装 Nginx:

sudo apt install nginx

Debian 的 Nginx 站点配置通常位于:

/etc/nginx/sites-available/
/etc/nginx/sites-enabled/

推荐做法是在 sites-available 中创建配置,然后软链接到 sites-enabled

示例配置:

# /etc/nginx/sites-available/example.com

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example.com;
    index index.html index.htm;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    location / {
        try_files $uri $uri/ =404;
    }
}

创建网站目录:

sudo mkdir -p /var/www/example.com
echo "Hello Debian" | sudo tee /var/www/example.com/index.html

启用站点:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

检查配置:

sudo nginx -t

重载:

sudo systemctl reload nginx

常见坑:

  • 忘记配置 DNS 解析;
  • 防火墙没放行 80/443;
  • server_name 写错;
  • 站点目录权限不正确;
  • 修改配置后没执行 nginx -t
  • 软链接重复或默认站点冲突。

二十一、定时任务避坑

Debian 中常用 cron 管理定时任务。

编辑当前用户的 crontab:

crontab -e

示例:每天凌晨 3 点执行备份脚本:

0 3 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

系统级定时任务可以放在:

/etc/cron.d/

示例:

# /etc/cron.d/backup

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 3 * * * root /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

常见坑:

  • cron 环境变量很少;
  • 脚本没有执行权限;
  • 路径没有写绝对路径;
  • 没有重定向日志;
  • 时区配置错误;
  • 脚本依赖交互式 shell。

给脚本执行权限:

sudo chmod +x /usr/local/bin/backup.sh

二十二、Docker 安装避坑

Debian 官方仓库里可能有 Docker 相关包,但建议使用 Docker 官方源安装。

卸载旧包:

sudo apt remove docker docker-engine docker.io containerd runc

安装依赖:

sudo apt update
sudo apt install ca-certificates curl gnupg

添加 GPG key:

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

添加软件源:

echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

安装:

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

启动:

sudo systemctl enable --now docker

将用户加入 docker 组:

sudo usermod -aG docker $USER

重新登录后生效。

注意:加入 docker 组等同于给用户较高权限,服务器上不要随意给普通用户添加 docker 组。


二十三、备份避坑

Debian 再稳定,也不能代替备份。任何系统都可能因为误操作、硬盘故障、升级失败、安全事件而损坏。

建议至少备份:

  • /etc
  • /home
  • /var/www
  • 数据库
  • Docker Compose 项目目录
  • 重要脚本
  • SSH 密钥
  • 防火墙规则
  • crontab

简单备份脚本示例:

#!/bin/bash
# /usr/local/bin/backup.sh

set -e

BACKUP_DIR="/backup"
DATE="$(date +%F)"
TARGET="$BACKUP_DIR/debian-backup-$DATE.tar.gz"

mkdir -p "$BACKUP_DIR"

tar -czf "$TARGET" \
    /etc \
    /home \
    /var/www \
    2>/tmp/backup-error.log

find "$BACKUP_DIR" -type f -name "debian-backup-*.tar.gz" -mtime +7 -delete

赋权:

sudo chmod +x /usr/local/bin/backup.sh

建议备份后传到远端,例如:

  • 另一台服务器;
  • NAS;
  • 对象存储;
  • 云盘;
  • rsync 服务器。

二十四、升级 Debian 大版本避坑

Debian 大版本升级前一定要备份。不要直接盲目修改源然后 full-upgrade

基本流程:

  1. 备份重要数据;
  2. 查看当前版本;
  3. 更新当前系统到最新;
  4. 阅读 Debian 官方 Release Notes;
  5. 修改软件源到新版本代号;
  6. 执行最小升级;
  7. 执行完整升级;
  8. 检查服务状态;
  9. 清理旧包。

示例流程:

sudo apt update
sudo apt upgrade
sudo apt full-upgrade
sudo apt autoremove

修改软件源后:

sudo apt update
sudo apt upgrade --without-new-pkgs
sudo apt full-upgrade

升级完成后检查:

cat /etc/debian_version
systemctl --failed
journalctl -p err

二十五、常用排障命令汇总

系统信息

uname -a
cat /etc/os-release
hostnamectl
timedatectl

磁盘

df -h
du -sh /*
lsblk
blkid

内存与进程

free -h
top
htop
ps aux

安装 htop:

sudo apt install htop

网络

ip addr
ip route
ss -tunlp
ping debian.org
curl -I https://debian.org

服务

systemctl status service-name
journalctl -u service-name
systemctl --failed

APT

apt update
apt list --upgradable
apt policy package-name
dpkg -l | grep package-name

二十六、推荐基础软件包

服务器基础工具:

sudo apt install vim curl wget git unzip zip tar htop net-tools dnsutils lsof rsync ca-certificates gnupg

桌面用户可以安装:

sudo apt install vlc gimp libreoffice fonts-noto-cjk fcitx5 fcitx5-chinese-addons

开发环境常用:

sudo apt install build-essential pkg-config cmake python3 python3-pip python3-venv

二十七、最终建议

Debian 是一个非常值得长期使用的系统,但它更适合“理解后再配置”,而不是一味复制命令。使用 Debian 时建议遵循以下原则:

  1. 软件源不要乱混,Stable 就保持 Stable;
  2. 重要操作前先备份,尤其是升级和改 SSH;
  3. 服务器先放行 SSH 再开防火墙
  4. 修改服务配置后先测试再重启
  5. 桌面用户优先解决字体、输入法和驱动问题
  6. 需要新版软件优先考虑 backports、Docker 或官方源
  7. 不要滥用 autoremove 和第三方脚本
  8. 定期查看日志、磁盘、服务状态

Debian 的魅力在于稳定、清晰、可控。只要避开软件源混用、权限配置、网络冲突、驱动缺失和盲目升级这些常见坑,它可以成为非常可靠的服务器系统,也可以成为简洁高效的桌面工作环境。

目录结构
全文