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

Debian 新手避坑手册:从换源、SSH 到 Docker 部署一篇搞定

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

Debian 常见问题汇总|附源码

Debian 是最受欢迎的 Linux 发行版之一,以稳定、安全、自由软件生态完善而闻名。无论是个人桌面、服务器部署、容器环境,还是嵌入式系统,Debian 都有非常广泛的应用场景。很多主流发行版,例如 Ubuntu、Linux Mint、Kali Linux 等,也都基于 Debian 构建。

不过,对于刚接触 Debian 的用户来说,在安装、换源、网络配置、软件管理、权限管理、服务维护、系统升级等方面,经常会遇到一些典型问题。本文将围绕 Debian 使用过程中高频出现的问题进行整理,并附带常用命令和示例配置源码,方便读者直接参考使用。


一、Debian 是什么?适合哪些场景?

Debian 是一个由社区维护的自由操作系统,采用 Linux 内核,也支持其他内核架构。它最大的特点是稳定性强、软件包管理成熟、文档完善、社区活跃。

Debian 通常分为以下几个主要分支:

分支 说明
stable 稳定版,适合服务器和生产环境
testing 测试版,软件较新,稳定性介于 stable 和 unstable 之间
unstable 不稳定版,也叫 sid,适合开发者和高级用户
oldstable 上一个稳定版本,仍会维护一段时间

对于大多数用户来说,如果是服务器环境,建议使用 stable;如果是个人桌面,希望软件版本更新一些,可以选择 testing,但要承担潜在风险。


二、如何查看 Debian 版本?

很多时候,我们需要确认当前系统版本,例如安装软件、排查兼容性问题、判断是否需要升级等。

1. 查看 Debian 版本

cat /etc/debian_version

示例输出:

12.5

2. 查看系统发行信息

cat /etc/os-release

示例输出:

PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian

3. 查看内核版本

uname -a

或者:

uname -r

三、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://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware

也可以使用阿里云源:

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/ bookworm-backports main contrib non-free non-free-firmware
deb https://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware

3. 更新软件包索引

sudo apt update

如果没有报错,说明软件源配置成功。


四、apt update 和 apt upgrade 有什么区别?

Debian 使用 APT 作为软件包管理工具。初学者经常混淆 updateupgrade

1. apt update

apt update 用于更新软件包索引,也就是从软件源同步最新的软件包列表。它不会真正升级系统中的软件。

sudo apt update

2. apt upgrade

apt upgrade 用于升级已经安装的软件包。

sudo apt upgrade

3. apt full-upgrade

full-upgrade 会在必要时安装或删除软件包,以完成更复杂的升级。

sudo apt full-upgrade

服务器生产环境中执行 full-upgrade 前,建议先查看将要变更的软件包列表,并做好备份。


五、Debian 无法使用 sudo 怎么办?

有些 Debian 最小化安装环境中,普通用户默认没有 sudo 权限,甚至系统可能没有安装 sudo。

1. 切换到 root 用户

su -

输入 root 密码后进入 root 环境。

2. 安装 sudo

apt update
apt install sudo -y

3. 将用户加入 sudo 用户组

假设用户名是 debian

usermod -aG sudo debian

然后退出当前登录会话,重新登录。

4. 验证 sudo 权限

sudo whoami

如果输出:

root

说明配置成功。


六、Debian 如何安装常用工具?

新安装的 Debian 系统可能缺少很多常用命令,例如 curlwgetvimgitnet-tools 等。

可以一次性安装:

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

这些工具用途如下:

工具 用途
vim 文本编辑器
curl 网络请求工具
wget 文件下载工具
git 版本控制工具
net-tools 提供 ifconfig、netstat 等命令
htop 进程监控工具
lsof 查看文件和端口占用
unzip/zip 压缩与解压
ca-certificates HTTPS 证书支持
gnupg GPG 密钥管理

七、Debian 网络不通如何排查?

网络问题是 Debian 使用过程中最常见的问题之一。可以按以下顺序排查。

1. 查看网卡信息

ip addr

或者:

ip a

如果看不到网卡 IP,可能是 DHCP 未获取到地址,或者网卡没有启用。

2. 查看默认路由

ip route

正常情况下应看到类似:

default via 192.168.1.1 dev eth0

3. 测试网关

ping -c 4 192.168.1.1

4. 测试公网 IP

ping -c 4 8.8.8.8

如果能 ping 通 IP 但不能访问域名,通常是 DNS 问题。

5. 测试域名解析

ping -c 4 debian.org

八、Debian 如何配置静态 IP?

Debian 的网络配置方式可能因安装环境不同而不同。常见方式包括 /etc/network/interfaces、NetworkManager 和 systemd-networkd。

1. 使用 interfaces 配置静态 IP

编辑文件:

sudo nano /etc/network/interfaces

示例配置源码:

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

或者重启系统:

sudo reboot

2. 使用 NetworkManager 配置静态 IP

查看连接名称:

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 up "Wired connection 1"

九、Debian DNS 解析异常怎么办?

如果系统能 ping 通公网 IP,但无法解析域名,例如:

ping debian.org

提示:

Temporary failure in name resolution

说明 DNS 可能异常。

1. 临时修改 DNS

编辑 /etc/resolv.conf

sudo nano /etc/resolv.conf

写入:

nameserver 223.5.5.5
nameserver 8.8.8.8

不过需要注意,某些系统中 /etc/resolv.conf 可能会被 NetworkManager 或 systemd-resolved 自动覆盖。

2. 使用 systemd-resolved

查看状态:

systemctl status systemd-resolved

启用服务:

sudo systemctl enable --now systemd-resolved

配置 DNS:

sudo nano /etc/systemd/resolved.conf

示例配置:

[Resolve]
DNS=223.5.5.5 8.8.8.8
FallbackDNS=1.1.1.1

重启服务:

sudo systemctl restart systemd-resolved

十、Debian 如何开放端口?

服务器部署 Web 服务、数据库服务或自定义应用时,经常需要开放端口。Debian 本身不一定默认启用防火墙,常见工具有 ufwiptables

1. 安装 ufw

sudo apt install ufw -y

2. 允许 SSH

在开启防火墙前,远程服务器务必先允许 SSH,否则可能导致无法远程登录。

sudo ufw allow ssh

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

sudo ufw allow 2222/tcp

3. 开放 HTTP 和 HTTPS

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

4. 启用防火墙

sudo ufw enable

5. 查看状态

sudo ufw status verbose

十一、Debian SSH 无法连接怎么办?

SSH 是管理服务器最常用的方式。如果 SSH 无法连接,可以从以下几个方面排查。

1. 确认是否安装 OpenSSH Server

dpkg -l | grep openssh-server

如果没有安装:

sudo apt update
sudo apt install openssh-server -y

2. 查看 SSH 服务状态

sudo systemctl status ssh

启动 SSH:

sudo systemctl enable --now ssh

3. 查看端口监听

sudo ss -tlnp | grep ssh

或:

sudo netstat -tlnp | grep ssh

4. 检查 SSH 配置

sudo nano /etc/ssh/sshd_config

常见配置:

Port 22
PermitRootLogin no
PasswordAuthentication yes
PubkeyAuthentication yes

修改后重启 SSH:

sudo systemctl restart ssh

注意:如果是远程服务器,修改 SSH 端口前一定要先开放防火墙端口,并保持当前连接不要断开,确认新连接可用后再退出。


十二、Debian 如何配置 SSH 密钥登录?

密钥登录比密码登录更安全。客户端生成密钥:

ssh-keygen -t ed25519 -C "your_email@example.com"

将公钥复制到服务器:

ssh-copy-id user@server_ip

如果没有 ssh-copy-id,可以手动添加:

cat ~/.ssh/id_ed25519.pub

复制输出内容,然后在服务器端执行:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

为了提升安全性,可以禁用密码登录:

sudo nano /etc/ssh/sshd_config

配置如下:

PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin no

重启 SSH:

sudo systemctl restart ssh

十三、Debian 软件安装失败怎么办?

安装软件时可能遇到依赖错误、锁文件占用、软件源不可用等问题。

1. 修复依赖

sudo apt --fix-broken install

2. 重新配置未完成的软件包

sudo dpkg --configure -a

3. 清理缓存

sudo apt clean
sudo apt autoclean

4. 移除无用依赖

sudo apt autoremove

5. 检查 apt 锁

如果提示:

Could not get lock /var/lib/dpkg/lock-frontend

先查看是否有 apt 进程正在运行:

ps aux | grep apt

如果确实没有其他 apt 进程,可以谨慎删除锁文件:

sudo rm -f /var/lib/dpkg/lock-frontend
sudo rm -f /var/lib/dpkg/lock
sudo dpkg --configure -a

十四、Debian 如何查看端口占用?

查看端口占用常用于排查服务启动失败。例如 Nginx 启动失败,可能是 80 端口已被其他程序占用。

1. 使用 ss

sudo ss -tulnp

查看指定端口:

sudo ss -tulnp | grep :80

2. 使用 lsof

sudo lsof -i :80

输出示例:

COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   666 root    6u  IPv4  12345      0t0  TCP *:http (LISTEN)

如果需要结束进程:

sudo kill -9 666

生产环境中不建议直接 kill -9,应优先使用服务管理命令:

sudo systemctl stop nginx

十五、Debian 如何管理 systemd 服务?

Debian 现代版本默认使用 systemd 管理服务。

1. 查看服务状态

systemctl status nginx

2. 启动服务

sudo systemctl start nginx

3. 停止服务

sudo systemctl stop nginx

4. 重启服务

sudo systemctl restart nginx

5. 设置开机自启

sudo systemctl enable nginx

6. 取消开机自启

sudo systemctl disable nginx

7. 查看服务日志

journalctl -u nginx

实时查看日志:

journalctl -u nginx -f

十六、Debian 如何查看磁盘空间?

磁盘空间不足会导致数据库写入失败、日志无法生成、系统更新失败等问题。

1. 查看磁盘使用情况

df -h

2. 查看目录大小

du -sh /var/log

3. 查看当前目录下各文件夹大小

du -h --max-depth=1

4. 清理 apt 缓存

sudo apt clean

5. 清理 systemd 日志

查看日志占用:

journalctl --disk-usage

保留最近 7 天日志:

sudo journalctl --vacuum-time=7d

限制日志大小为 500M:

sudo journalctl --vacuum-size=500M

十七、Debian 如何安装 Nginx?

Nginx 是常用的 Web 服务器和反向代理服务。

1. 安装 Nginx

sudo apt update
sudo apt install nginx -y

2. 启动并设置开机自启

sudo systemctl enable --now nginx

3. 查看状态

systemctl status nginx

4. 测试访问

curl http://127.0.0.1

如果返回 HTML 内容,说明 Nginx 正常工作。

5. 示例站点配置源码

创建网站目录:

sudo mkdir -p /var/www/example
sudo nano /var/www/example/index.html

写入:




    
    Debian Nginx 示例


    

Hello Debian

这是一个运行在 Debian 上的 Nginx 示例页面。

创建 Nginx 配置:

sudo nano /etc/nginx/sites-available/example

写入:

server {
    listen 80;
    server_name example.com;

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

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

启用站点:

sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

十八、Debian 如何安装 Docker?

Docker 在 Debian 上使用非常普遍。建议使用 Docker 官方仓库安装。

1. 安装依赖

sudo apt update
sudo apt install -y ca-certificates curl gnupg

2. 添加 Docker GPG 密钥

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

3. 添加 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

4. 安装 Docker

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

5. 启动 Docker

sudo systemctl enable --now docker

6. 测试 Docker

sudo docker run hello-world

7. 允许普通用户使用 Docker

sudo usermod -aG docker $USER

重新登录后执行:

docker ps

十九、Debian 如何编写定时任务?

Linux 中常用 cron 实现定时任务。

1. 编辑当前用户定时任务

crontab -e

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

0 2 * * * /bin/bash /opt/scripts/backup.sh

3. 示例备份脚本源码

创建脚本:

sudo mkdir -p /opt/scripts
sudo nano /opt/scripts/backup.sh

写入:

#!/bin/bash

SOURCE_DIR="/var/www"
BACKUP_DIR="/backup"
DATE=$(date +%F_%H-%M-%S)

mkdir -p "$BACKUP_DIR"

tar -czf "$BACKUP_DIR/www_$DATE.tar.gz" "$SOURCE_DIR"

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

echo "Backup completed at $(date)"

赋予执行权限:

sudo chmod +x /opt/scripts/backup.sh

这个脚本会将 /var/www 打包到 /backup,并自动删除 7 天前的备份文件。


二十、Debian 如何配置自动安全更新?

服务器长时间运行时,安全更新非常重要。Debian 可以使用 unattended-upgrades 自动安装安全补丁。

1. 安装工具

sudo apt install unattended-upgrades apt-listchanges -y

2. 启用自动更新

sudo dpkg-reconfigure unattended-upgrades

选择 Yes

3. 配置文件位置

/etc/apt/apt.conf.d/50unattended-upgrades

可查看或调整允许自动更新的来源:

sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

4. 手动测试

sudo unattended-upgrade --dry-run --debug

二十一、Debian 如何升级大版本?

例如从 Debian 11 升级到 Debian 12。大版本升级有风险,建议先备份数据。

1. 完整更新当前系统

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

2. 备份软件源

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

3. 修改版本代号

Debian 11 是 bullseye,Debian 12 是 bookworm。可以使用 sed 替换:

sudo sed -i 's/bullseye/bookworm/g' /etc/apt/sources.list

如果 /etc/apt/sources.list.d/ 中也有相关源,需要一起检查:

grep -R "bullseye" /etc/apt/sources.list.d/

4. 执行升级

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

5. 重启并检查版本

sudo reboot
cat /etc/os-release

二十二、Debian 如何查看系统日志?

日志是排查问题的重要依据。

1. 查看系统日志

journalctl

2. 查看本次启动日志

journalctl -b

3. 查看错误日志

journalctl -p err

4. 查看某个服务日志

journalctl -u ssh

5. 实时追踪日志

journalctl -f

传统日志目录通常在:

/var/log

例如:

ls -lh /var/log

常见日志文件包括:

/var/log/syslog
/var/log/auth.log
/var/log/kern.log
/var/log/dpkg.log

二十三、Debian 时间不准怎么办?

服务器时间不准可能导致证书校验失败、日志时间混乱、数据库同步异常等问题。

1. 查看当前时间

date
timedatectl

2. 设置时区为上海

sudo timedatectl set-timezone Asia/Shanghai

3. 启用 NTP 时间同步

sudo timedatectl set-ntp true

查看状态:

timedatectl status

如果需要安装 chrony:

sudo apt install chrony -y
sudo systemctl enable --now chrony

二十四、Debian 忘记 root 密码怎么办?

如果忘记 root 密码,可以通过 GRUB 单用户模式重置。但这需要拥有服务器控制台权限,例如云服务器 VNC、物理机显示器或虚拟机控制台。

基本思路如下:

  1. 重启系统进入 GRUB 菜单;
  2. 选择 Debian 启动项,按 e 编辑;
  3. 找到以 linux 开头的行;
  4. 在末尾添加:
init=/bin/bash
  1. Ctrl + X 启动;
  2. 重新挂载根分区为可写:
mount -o remount,rw /
  1. 修改 root 密码:
passwd root
  1. 同步并重启:
sync
reboot -f

此方法要求用户拥有系统本机控制权。对于云服务器,也可以通过云厂商控制台重置密码或挂载救援系统处理。


二十五、附:Debian 初始化脚本源码

下面提供一个常用的 Debian 服务器初始化脚本,适用于新服务器基础配置。它会更新系统、安装常用工具、设置时区、启用防火墙、开启自动安全更新等。

使用前请根据自己的环境修改 SSH 端口、防火墙规则和软件源。

#!/bin/bash

set -e

echo "==== Debian 初始化脚本开始 ===="

if [ "$(id -u)" -ne 0 ]; then
    echo "请使用 root 用户运行该脚本"
    exit 1
fi

echo "==== 更新软件包索引 ===="
apt update

echo "==== 升级系统软件包 ===="
apt upgrade -y

echo "==== 安装常用工具 ===="
apt install -y \
    vim \
    curl \
    wget \
    git \
    net-tools \
    htop \
    lsof \
    unzip \
    zip \
    tar \
    ca-certificates \
    gnupg \
    ufw \
    chrony \
    unattended-upgrades \
    apt-listchanges

echo "==== 设置时区 ===="
timedatectl set-timezone Asia/Shanghai

echo "==== 启用 chrony 时间同步 ===="
systemctl enable --now chrony

echo "==== 配置防火墙 ===="
ufw allow ssh
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable

echo "==== 启用自动安全更新 ===="
dpkg-reconfigure -f noninteractive unattended-upgrades || true

echo "==== 清理无用依赖 ===="
apt autoremove -y
apt clean

echo "==== 当前系统信息 ===="
cat /etc/os-release
uname -a
df -h

echo "==== Debian 初始化脚本完成 ===="

保存为:

nano init-debian.sh

赋予执行权限:

chmod +x init-debian.sh

执行:

sudo ./init-debian.sh

二十六、常见问题速查表

问题 常用命令
查看版本 cat /etc/os-release
更新软件源索引 sudo apt update
升级软件包 sudo apt upgrade
修复依赖 sudo apt --fix-broken install
查看 IP ip a
查看路由 ip route
查看端口 sudo ss -tulnp
查看磁盘 df -h
查看内存 free -h
查看进程 htop
查看服务 systemctl status 服务名
查看日志 journalctl -u 服务名
开放端口 sudo ufw allow 端口/tcp
重启系统 sudo reboot

总结

Debian 的优势在于稳定、安全、简洁和可控,但也正因为它偏向稳定和通用,很多配置需要用户自己完成。对于新手来说,最常见的问题通常集中在软件源、sudo 权限、网络配置、SSH 连接、软件安装失败、端口占用、服务管理、日志排查和系统升级等方面。

掌握本文中的常用命令和配置示例后,基本可以应对 Debian 日常使用中的大部分问题。对于服务器环境,建议养成以下习惯:定期更新安全补丁、修改配置前备份文件、谨慎开放端口、使用 SSH 密钥登录、关注磁盘空间和系统日志,并在大版本升级前做好完整备份。这样可以显著提升 Debian 系统的稳定性和安全性。

目录结构
全文