Debian 实战指南

FreeGuideOnline 最新 2026-07-14

bash usermod -aG sudo 你的用户名


注销重新登录后,即可使用 `sudo` 执行管理命令。

### 更换国内软件源

编辑 `/etc/apt/sources.list`,将官方源替换为国内镜像,比如科大镜像(示例 Debian 12 Bookworm):

```bash
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
sudo nano /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

更新软件包列表并升级系统:

sudo apt update
sudo apt upgrade

配置防火墙(UFW)

Ubuntu 的 Uncomplicated Firewall 在 Debian 上同样好用:

sudo apt install ufw
sudo ufw allow ssh        # 放行 SSH
sudo ufw enable
sudo ufw status verbose

安装常用工具

sudo apt install curl wget git vim htop net-tools

APT 包管理实战:软件的安装、查询与净化

查找与安装软件

  • 更新包索引:sudo apt update
  • 搜索软件包:apt search 关键词
  • 安装软件:sudo apt install 包名
  • 安装 .deb 文件:sudo dpkg -i /path/to/package.deb,若报依赖错误,运行 sudo apt install -f 自动修复。

查看软件信息

  • 查看包详情:apt show 包名
  • 列出已安装包:apt list --installed
  • 查看某个文件属于哪个包:dpkg -S /path/to/file

安全更新与卸载

  • 升级所有包:sudo apt upgrade(保守升级,不删除包);sudo apt full-upgrade(智能解决依赖,可能删除过时包)
  • 卸载包但保留配涚:sudo apt remove 包名
  • 彻底卸载并清除配置:sudo apt purge 包名
  • 清理不再需要的依赖:sudo apt autoremove
  • 清理下载的缓存:sudo apt clean

系统管理:用户、进程和服务

用户与权限管理

  • 添加用户:sudo adduser 新用户名
  • 删除用户:sudo deluser 用户名(保留家目录);sudo deluser --remove-home 用户名(删除家目录)
  • 修改密码:passwd(普通用户修改自己);sudo passwd 用户名
  • 查看登录历史:last
  • 文件权限:chmodchown,学会用 ls -l 查看权限字符串。

systemd 服务管理

Debian 使用 systemd 管理系统和服务。

  • 查看服务状态:systemctl status 服务名
  • 启动/停止/重启:sudo systemctl start|stop|restart 服务名
  • 设置开机自启:sudo systemctl enable 服务名
  • 禁止自启:sudo systemctl disable 服务名
  • 重新加载配置文件(不中断服务):sudo systemctl reload 服务名
  • 查看日志:journalctl -u 服务名 -f(实时跟踪)

进程监控与 kill

  • 实时监控:tophtop(需安装)
  • 列出进程:ps aux
  • 按名称查找进程:pgrep 进程名
  • 强制结束进程:kill -9 PIDpkill -9 进程名

网络配置与排错

网络接口管理

  • 查看 IP 地址:ip addr show
  • 查看路由表:ip route show
  • 网络连通性测试:ping 目标地址traceroute 目标地址
  • DNS 解析查询:nslookup 域名dig 域名

配置静态 IP(/etc/network/interfaces)

默认使用 NetworkManager 时,可直接图形界面配置。若需传统方式,编辑 /etc/network/interfaces

auto eth0
iface eth0 inet static
    address 192.168.1.100/24
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

修改后重启网络服务:sudo systemctl restart networking

搭建常用服务:快速上手 LAMP 与 SSH

强化 SSH 安全

SSH 服务已默认安装。编辑 /etc/ssh/sshd_config 加固配置:

Port 2222                     # 修改默认端口
PermitRootLogin prohibit-password   # 禁止 root 密码登录,仅允许密钥
PubkeyAuthentication yes
PasswordAuthentication no    # 禁用密码登录(先确保密钥可用)

重启服务:sudo systemctl restart sshd

一键部署 LAMP 环境

使用 tasksel 或手动安装:

sudo apt install apache2 mariadb-server php libapache2-mod-php