本文适用于 Debian 12(Bookworm)服务器,示例网络参数如下:
| 项目 | 示例值 |
|---|---|
| 网卡 | eth0 |
| 静态 IP | 192.168.0.180/24 |
| 网关 | 192.168.0.1 |
| DNS | 223.5.5.5、119.29.29.29 |
执行前请根据实际环境修改网卡名、IP、网关和 DNS。远程修改网络配置可能导致 SSH 断开,建议通过虚拟机控制台或服务器本地终端操作。
一、切换 Debian 12 软件源
1. 确认系统版本
cat /etc/os-release
应能看到类似内容:
VERSION_CODENAME=bookworm
2. 备份原软件源
cp -a /etc/apt/sources.list /etc/apt/sources.list.backup-$(date +%Y%m%d-%H%M%S)
如果系统使用 DEB822 格式的软件源,同时备份:
if [ -f /etc/apt/sources.list.d/debian.sources ]; then
cp -a /etc/apt/sources.list.d/debian.sources \
/etc/apt/sources.list.d/debian.sources.backup-$(date +%Y%m%d-%H%M%S)
fi
3. 使用清华大学镜像源
先停用可能重复的 debian.sources:
if [ -f /etc/apt/sources.list.d/debian.sources ]; then
mv /etc/apt/sources.list.d/debian.sources \
/etc/apt/sources.list.d/debian.sources.disabled
fi
写入 /etc/apt/sources.list:
cat > /etc/apt/sources.list <<'EOF'
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
EOF
说明:安全更新保留 Debian 官方 security.debian.org,避免镜像同步延迟影响安全补丁。
4. 更新软件索引和系统
apt clean
apt update
apt full-upgrade -y
如更新了内核或关键系统组件,执行:
reboot
二、安装并开启 SSH
1. 安装 OpenSSH Server
apt update
apt install -y openssh-server
2. 启用并启动 SSH
systemctl enable --now ssh
systemctl status ssh --no-pager
查看监听端口:
ss -lntp | grep ':22'
3. 推荐方式:使用普通用户登录
创建管理用户并加入 sudo 组:
apt install -y sudo
adduser admin
usermod -aG sudo admin
随后可使用:
ssh admin@192.168.0.180
4. 可选:临时允许 root 密码登录
仅在确实需要时使用:
mkdir -p /etc/ssh/sshd_config.d
cat > /etc/ssh/sshd_config.d/99-root-login.conf <<'EOF'
PermitRootLogin yes
PasswordAuthentication yes
EOF
sshd -t && systemctl restart ssh
连接方式:
ssh root@192.168.0.180
部署完成后建议禁用 root 密码远程登录:
cat > /etc/ssh/sshd_config.d/99-root-login.conf <<'EOF'
PermitRootLogin no
PasswordAuthentication yes
EOF
sshd -t && systemctl restart ssh
如果已经配置 SSH 密钥,还可将 PasswordAuthentication 改为 no。
三、配置静态 IP
1. 查看实际网络参数
ip -br address
ip route
cat /etc/resolv.conf
确认以下信息:
- 实际网卡名称,例如
eth0、ens18或ens33。 - 默认网关地址。
- 目标 IP 未被其他设备占用。
可从同一局域网的另一台设备测试目标 IP:
ping 192.168.0.180
目标地址已被其他设备使用时,不要继续配置。
2. 判断网络管理方式
systemctl is-active networking
systemctl is-active NetworkManager
systemctl is-active systemd-networkd
服务器精简安装通常使用 /etc/network/interfaces。只选择与当前系统匹配的一种方式,不要同时配置多套网络管理服务。
四、方式 A:使用/etc/network/interfaces
1. 备份网络配置
cp -a /etc/network/interfaces \
/etc/network/interfaces.backup-$(date +%Y%m%d-%H%M%S)
2. 写入静态地址
以下示例网卡为 eth0:
cat > /etc/network/interfaces <<'EOF'
source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet static
address 192.168.0.180/24
gateway 192.168.0.1
dns-nameservers 223.5.5.5 119.29.29.29
EOF
如果实际网卡不是 eth0,请替换配置中的两处 eth0。
3. 应用配置
从本地控制台操作时可执行:
systemctl restart networking
远程 SSH 操作时,重启网络会立即断开连接。更稳妥的做法是写完配置后直接重启:
reboot
五、方式 B:使用 NetworkManager
先查看连接名称:
nmcli connection show
假设连接名称为 Wired connection 1:
nmcli connection modify 'Wired connection 1' \
ipv4.method manual \
ipv4.addresses 192.168.0.180/24 \
ipv4.gateway 192.168.0.1 \
ipv4.dns '223.5.5.5 119.29.29.29'
nmcli connection up 'Wired connection 1'
六、检查配置结果
重新连接服务器:
ssh root@192.168.0.180
检查地址、路由、DNS 和 SSH:
ip -br address
ip route
getent hosts deb.debian.org
ping -c 4 192.168.0.1
ping -c 4 deb.debian.org
systemctl is-enabled ssh
systemctl is-active ssh
ss -lntp | grep ':22'
预期结果:
- 网卡具有
192.168.0.180/24。 - 默认路由指向
192.168.0.1。 - 域名能够正常解析。
- SSH 服务状态为
enabled和active。
七、防火墙配置(可选)
如果启用了 UFW,开放 SSH 后再启用防火墙:
apt install -y ufw
ufw allow 22/tcp
ufw enable
ufw status verbose
如果 SSH 使用自定义端口,应先放行对应端口,避免把自己锁在服务器外。
八、网络异常时回退
通过服务器本地控制台登录,找到之前生成的备份文件:
ls -l /etc/network/interfaces.backup-*
恢复备份并重启网络:
cp -a /etc/network/interfaces.backup-YYYYMMDD-HHMMSS \
/etc/network/interfaces
systemctl restart networking
如无法确定网络服务状态,可直接重启:
reboot
九、当前服务器示例结果
系统:Debian 12 Bookworm
网卡:eth0
IP:192.168.0.180
掩码:255.255.255.0(/24)
网关:192.168.0.1
SSH:TCP 22