Debian 新手常见故障与实用命令排查指南
Debian 常见问题汇总|附完整命令
Debian 是最稳定、最经典的 Linux 发行版之一,广泛用于服务器、开发环境、桌面系统、容器镜像以及嵌入式场景。很多用户选择 Debian,是因为它软件源稳定、社区资料丰富、系统结构清晰、安全维护周期长。但对于新手来说,Debian 在安装、软件源、网络、权限、服务管理、防火墙、磁盘、SSH、中文环境等方面,也经常会遇到一些问题。
本文整理 Debian 使用过程中最常见的问题,并附上完整命令示例,适合 Debian 11、Debian 12 以及大多数基于 Debian 的系统参考使用。
一、如何查看 Debian 系统版本?
在排查问题前,首先要确认当前系统版本、内核版本和架构。
1. 查看 Debian 版本
cat /etc/debian_version
2. 查看系统发行版信息
cat /etc/os-release
示例输出可能类似:
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
VERSION_ID="12"
VERSION="12 (bookworm)"
3. 查看内核版本
uname -a
或:
uname -r
4. 查看系统架构
dpkg --print-architecture
常见结果:
amd64
arm64
i386
二、Debian 如何切换国内软件源?
Debian 默认软件源在国外,国内访问速度可能较慢。建议根据系统版本切换到国内镜像源,例如清华源、中科大源、阿里云源等。
1. 备份原软件源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
2. 编辑软件源文件
sudo nano /etc/apt/sources.list
如果你使用的是 Debian 12 Bookworm,可以参考以下清华源配置:
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/ bookworm-backports 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
Debian 11 Bullseye 可使用:
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
3. 更新软件包索引
sudo apt update
4. 升级系统软件包
sudo apt upgrade -y
如需执行完整升级:
sudo apt full-upgrade -y
三、apt update 报错怎么办?
apt update 是 Debian 中最常用的命令之一,但也容易出现各种错误。
问题 1:Could not resolve host
报错示例:
Could not resolve 'deb.debian.org'
通常是 DNS 解析失败。
解决方法:检查网络
ping -c 4 8.8.8.8
如果 IP 能 ping 通,但域名不能 ping 通:
ping -c 4 deb.debian.org
说明 DNS 有问题。
临时修改 DNS
sudo nano /etc/resolv.conf
写入:
nameserver 223.5.5.5
nameserver 8.8.8.8
然后重新执行:
sudo apt update
如果 /etc/resolv.conf 被系统自动覆盖,需要检查 NetworkManager 或 systemd-resolved 配置。
问题 2:NO_PUBKEY 公钥错误
报错示例:
NO_PUBKEY XXXXXXXX
可以安装 Debian 密钥包:
sudo apt install debian-archive-keyring -y
sudo apt update
如果是第三方源缺少 key,应按该软件源官方文档重新添加 GPG key。通用方式示例:
curl -fsSL https://example.com/key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/example.gpg
然后在源配置中使用:
deb [signed-by=/usr/share/keyrings/example.gpg] https://example.com/debian stable main
问题 3:软件源版本代号写错
如果系统是 Debian 12,却写成了 bullseye,可能导致依赖混乱。
查看系统代号:
. /etc/os-release
echo $VERSION_CODENAME
常见代号:
| Debian 版本 | 代号 |
|---|---|
| Debian 10 | buster |
| Debian 11 | bullseye |
| Debian 12 | bookworm |
| Debian 13 | trixie |
编辑软件源:
sudo nano /etc/apt/sources.list
将代号改为正确版本,然后执行:
sudo apt update
四、Debian 如何安装常用软件?
新装 Debian 后,建议安装一些常用工具。
sudo apt update
sudo apt install -y curl wget vim nano git net-tools lsof unzip zip tar htop tree sudo ca-certificates gnupg software-properties-common
各工具作用如下:
| 软件包 | 作用 |
|---|---|
| curl | 命令行请求工具 |
| wget | 文件下载工具 |
| vim/nano | 文本编辑器 |
| git | 代码版本管理 |
| net-tools | 提供 ifconfig、netstat |
| lsof | 查看文件和端口占用 |
| unzip/zip | 解压缩工具 |
| htop | 进程监控 |
| tree | 树状查看目录 |
| sudo | 普通用户提权 |
| ca-certificates | HTTPS 证书支持 |
五、普通用户无法使用 sudo 怎么办?
很多 Debian 最小化安装后,普通用户默认不在 sudo 组中,会出现:
user is not in the sudoers file
1. 切换到 root 用户
su -
输入 root 密码。
2. 安装 sudo
apt update
apt install sudo -y
3. 将用户加入 sudo 组
假设用户名是 debianuser:
usermod -aG sudo debianuser
4. 重新登录用户
退出当前终端重新登录,或执行:
su - debianuser
验证:
sudo whoami
如果输出:
root
说明配置成功。
六、Debian 如何配置静态 IP?
不同 Debian 环境网络管理方式可能不同,常见有 /etc/network/interfaces、NetworkManager、systemd-networkd。
方法一:使用 interfaces 配置静态 IP
查看网卡名称:
ip addr
假设网卡名为 ens33。
编辑配置:
sudo nano /etc/network/interfaces
写入:
auto ens33
iface ens33 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
或重启系统:
sudo reboot
查看 IP:
ip addr show ens33
方法二:使用 nmcli 配置静态 IP
如果系统使用 NetworkManager,可以用:
nmcli connection show
假设连接名为 Wired connection 1,执行:
sudo nmcli connection modify "Wired connection 1" ipv4.addresses 192.168.1.100/24
sudo nmcli connection modify "Wired connection 1" ipv4.gateway 192.168.1.1
sudo nmcli connection modify "Wired connection 1" ipv4.dns "223.5.5.5 8.8.8.8"
sudo nmcli connection modify "Wired connection 1" ipv4.method manual
sudo nmcli connection down "Wired connection 1"
sudo nmcli connection up "Wired connection 1"
查看结果:
ip route
ip addr
七、Debian 如何开启 SSH 远程登录?
服务器场景中,SSH 是最常见的远程管理方式。
1. 安装 OpenSSH Server
sudo apt update
sudo apt install openssh-server -y
2. 启动 SSH 服务
sudo systemctl start ssh
3. 设置开机自启
sudo systemctl enable ssh
4. 查看 SSH 状态
sudo systemctl status ssh
5. 查看服务器 IP
ip addr
6. 客户端连接
ssh 用户名@服务器IP
示例:
ssh debianuser@192.168.1.100
八、SSH 连接失败如何排查?
1. 检查 SSH 服务是否运行
sudo systemctl status ssh
如果未运行:
sudo systemctl restart ssh
2. 检查端口是否监听
sudo ss -tlnp | grep :22
或:
sudo netstat -tlnp | grep :22
3. 检查防火墙
如果使用 UFW:
sudo ufw status
放行 SSH:
sudo ufw allow 22/tcp
sudo ufw reload
如果使用 nftables:
sudo nft list ruleset
4. 检查 SSH 配置文件
sudo nano /etc/ssh/sshd_config
常见配置:
Port 22
PermitRootLogin no
PasswordAuthentication yes
修改后重启:
sudo systemctl restart ssh
5. 查看日志
sudo journalctl -u ssh -xe
或:
sudo tail -f /var/log/auth.log
九、Debian 如何修改 SSH 端口?
为了降低被扫描的概率,可以将默认 22 端口改为其他端口,例如 2222。
编辑配置:
sudo nano /etc/ssh/sshd_config
找到或添加:
Port 2222
检查配置语法:
sudo sshd -t
如果没有输出,说明语法正确。
放行新端口:
sudo ufw allow 2222/tcp
重启 SSH:
sudo systemctl restart ssh
连接时指定端口:
ssh -p 2222 用户名@服务器IP
注意:不要在确认新端口可连接之前关闭当前 SSH 会话,避免把自己锁在服务器外。
十、Debian 如何查看端口占用?
1. 使用 ss 查看端口
sudo ss -tulnp
查看指定端口:
sudo ss -tulnp | grep :80
2. 使用 lsof 查看端口
sudo lsof -i :80
3. 使用 netstat 查看端口
如果未安装:
sudo apt install net-tools -y
查看:
sudo netstat -tulnp
十一、Debian 如何管理系统服务?
Debian 使用 systemd 管理服务。
1. 查看服务状态
sudo systemctl status nginx
2. 启动服务
sudo systemctl start nginx
3. 停止服务
sudo systemctl stop nginx
4. 重启服务
sudo systemctl restart nginx
5. 重新加载配置
sudo systemctl reload nginx
6. 设置开机自启
sudo systemctl enable nginx
7. 取消开机自启
sudo systemctl disable nginx
8. 查看开机启动服务
systemctl list-unit-files --type=service | grep enabled
9. 查看失败服务
systemctl --failed
十二、Debian 如何配置防火墙?
Debian 可以使用 UFW、iptables 或 nftables。对新手来说,UFW 更简单。
1. 安装 UFW
sudo apt update
sudo apt install ufw -y
2. 默认拒绝入站,允许出站
sudo ufw default deny incoming
sudo ufw default allow outgoing
3. 放行 SSH
sudo ufw allow ssh
如果 SSH 端口是 2222:
sudo ufw allow 2222/tcp
4. 放行 HTTP/HTTPS
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
5. 启用防火墙
sudo ufw enable
6. 查看状态
sudo ufw status verbose
7. 删除规则
sudo ufw delete allow 80/tcp
十三、Debian 磁盘空间满了怎么办?
磁盘空间不足会导致服务异常、日志无法写入、软件无法安装等问题。
1. 查看磁盘占用
df -h
2. 查看目录大小
sudo du -sh /*
查看当前目录下各文件夹大小:
du -h --max-depth=1
3. 查找大文件
sudo find / -type f -size +500M -exec ls -lh {} \; 2>/dev/null
4. 清理 apt 缓存
sudo apt clean
sudo apt autoclean
5. 删除无用依赖
sudo apt autoremove -y
6. 清理 systemd 日志
查看日志占用:
journalctl --disk-usage
只保留最近 7 天:
sudo journalctl --vacuum-time=7d
限制日志总大小:
sudo journalctl --vacuum-size=500M
十四、Debian 内存占用高如何排查?
1. 查看内存使用情况
free -h
2. 使用 top
top
3. 使用 htop
sudo apt install htop -y
htop
4. 查看占用内存最高的进程
ps aux --sort=-%mem | head
5. 查看占用 CPU 最高的进程
ps aux --sort=-%cpu | head
如需结束某个进程:
sudo kill PID
强制结束:
sudo kill -9 PID
十五、Debian 时间不准确怎么办?
1. 查看当前时间
date
2. 查看时区
timedatectl
3. 设置中国时区
sudo timedatectl set-timezone Asia/Shanghai
4. 开启 NTP 自动同步
sudo timedatectl set-ntp true
5. 查看同步状态
timedatectl status
如果需要安装 chrony:
sudo apt install chrony -y
sudo systemctl enable chrony
sudo systemctl start chrony
chronyc tracking
十六、Debian 如何安装 Nginx?
1. 安装 Nginx
sudo apt update
sudo apt install nginx -y
2. 启动并设置自启
sudo systemctl start nginx
sudo systemctl enable nginx
3. 查看状态
sudo systemctl status nginx
4. 放行端口
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
5. 测试访问
curl http://127.0.0.1
6. 检查配置
sudo nginx -t
7. 重载配置
sudo systemctl reload nginx
十七、Debian 如何安装 Docker?
Debian 官方源中也有 Docker 相关包,但推荐使用 Docker 官方源安装较新版本。
1. 卸载旧版本
sudo apt remove docker docker-engine docker.io containerd runc -y
2. 安装依赖
sudo apt update
sudo apt install ca-certificates curl gnupg -y
3. 添加 Docker 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
4. 添加 Docker 软件源
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
5. 安装 Docker
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
6. 启动 Docker
sudo systemctl enable docker
sudo systemctl start docker
7. 测试 Docker
sudo docker run hello-world
8. 允许普通用户运行 Docker
sudo usermod -aG docker $USER
重新登录后测试:
docker ps
十八、Debian 中文乱码怎么办?
如果系统中文显示异常,可能是 locale 没有正确配置。
1. 安装 locales
sudo apt update
sudo apt install locales -y
2. 生成中文 UTF-8 locale
sudo dpkg-reconfigure locales
在界面中勾选:
zh_CN.UTF-8 UTF-8
也可以手动编辑:
sudo nano /etc/locale.gen
取消注释:
zh_CN.UTF-8 UTF-8
生成 locale:
sudo locale-gen
3. 设置默认语言环境
sudo update-locale LANG=zh_CN.UTF-8
重新登录后查看:
locale
十九、Debian 如何修改主机名?
1. 查看当前主机名
hostnamectl
2. 修改主机名
sudo hostnamectl set-hostname debian-server
3. 修改 hosts 文件
sudo nano /etc/hosts
添加或修改:
127.0.1.1 debian-server
4. 验证
hostname
hostnamectl
二十、Debian 忘记 root 密码怎么办?
如果是本地机器或有控制台权限,可以通过单用户模式重置 root 密码。
操作步骤
- 重启系统,在 GRUB 菜单选择 Debian 启动项;
- 按
e编辑启动参数; - 找到以
linux开头的行; - 在该行末尾添加:
init=/bin/bash
- 按
Ctrl + X或F10启动; - 进入 shell 后重新挂载根分区为可写:
mount -o remount,rw /
- 修改 root 密码:
passwd root
- 同步磁盘并重启:
sync
reboot -f
注意:该方法需要物理机、虚拟机控制台或云服务器救援模式权限。
二十一、Debian 如何查看日志?
日志排查是 Linux 运维的重要技能。
1. 查看系统日志
journalctl
2. 查看最近启动日志
journalctl -b
3. 查看指定服务日志
journalctl -u nginx
实时查看:
journalctl -u nginx -f
4. 查看 SSH 登录日志
sudo tail -f /var/log/auth.log
5. 查看内核日志
dmesg
分页查看:
dmesg | less
6. 查看最近错误
journalctl -p err -b
二十二、Debian 如何升级大版本?
例如从 Debian 11 升级到 Debian 12,需要谨慎操作,建议先备份。
1. 备份重要数据
sudo tar -czvf /root/etc-backup.tar.gz /etc
如有数据库、网站文件,也应单独备份。
2. 升级当前系统
sudo apt update
sudo apt upgrade -y
sudo apt full-upgrade -y
sudo apt autoremove -y
3. 修改软件源代号
将 bullseye 替换为 bookworm:
sudo sed -i 's/bullseye/bookworm/g' /etc/apt/sources.list
如果 /etc/apt/sources.list.d/ 下也有源文件:
sudo grep -R bullseye /etc/apt/sources.list.d/
根据情况修改。
4. 更新索引
sudo apt update
5. 执行最小升级
sudo apt upgrade --without-new-pkgs -y
6. 执行完整升级
sudo apt full-upgrade -y
7. 重启系统
sudo reboot
8. 验证版本
cat /etc/os-release
二十三、Debian 常用命令速查表
| 功能 | 命令 |
|---|---|
| 更新软件源 | sudo apt update |
| 升级软件 | sudo apt upgrade -y |
| 完整升级 | sudo apt full-upgrade -y |
| 安装软件 | sudo apt install 软件名 -y |
| 删除软件 | sudo apt remove 软件名 -y |
| 彻底删除软件 | sudo apt purge 软件名 -y |
| 清理依赖 | sudo apt autoremove -y |
| 查看 IP | ip addr |
| 查看路由 | ip route |
| 查看端口 | sudo ss -tulnp |
| 查看磁盘 | df -h |
| 查看内存 | free -h |
| 查看进程 | ps aux |
| 查看服务 | systemctl status 服务名 |
| 查看日志 | journalctl -xe |
| 重启系统 | sudo reboot |
| 关机 | sudo poweroff |
二十四、Debian 使用建议
-
不要混用不同版本的软件源
例如 Debian 12 不要长期混用 Debian 11 的源,否则容易造成依赖冲突。 -
服务器建议禁用 root SSH 密码登录
可以使用普通用户加 sudo,并配置 SSH key 登录。 -
重要操作前先备份
特别是系统升级、分区调整、数据库迁移等操作。 -
定期更新安全补丁
sudo apt update
sudo apt upgrade -y
-
少用来历不明的一键脚本
一键脚本可能修改系统源、防火墙、SSH 配置,甚至带来安全风险。 -
养成查看日志的习惯
遇到服务无法启动,不要只反复重启,应先查看:
journalctl -u 服务名 -xe
总结
Debian 的优势在于稳定、规范和可维护,但刚开始使用时,确实可能在软件源、网络、权限、SSH、防火墙、磁盘空间、服务管理等方面遇到问题。掌握 apt、systemctl、journalctl、ip、ss、ufw 等核心命令后,绝大多数日常问题都可以快速定位和解决。
如果是服务器环境,建议优先完成以下基础配置:切换可靠软件源、创建普通 sudo 用户、开启 SSH、配置防火墙、设置正确时区、安装常用工具、定期更新系统。这样可以让 Debian 系统在长期运行中更加稳定、安全、易维护。