Debian 服务器新手必会的操作
Debian 服务器的核心哲学是稳定 > 最新,所以新手最该学的不是“酷炫工具”,而是怎么让服务器活得久、不被黑、不崩溃。
下面按时间顺序和重要程度排序,建议新手安装完系统后立即按顺序执行。
阶段 1:刚开机 / 首次登录(必须在 5 分钟内完成)
改 root 密码(如果安装时没设强密码)
Bashpasswd至少 12 位,包含大小写+数字+符号。
创建日常管理用户 + 加入 sudo 组(千万别长期用 root 登录!)
Bashadduser yourname # 创建用户 usermod -aG sudo yourname # 加到 sudo 组(Debian 默认叫 sudo)退出 root,改用新用户 + sudo 登录。
禁用 root 通过 SSH 直接登录(最重要的一步防暴力破解)
编辑 /etc/ssh/sshd_config:
textPermitRootLogin no PasswordAuthentication no # 如果你打算只用密钥登录(推荐)然后:
Bashsudo systemctl restart ssh
阶段 2:基础更新与安全加固(安装后第一天)
| 操作 | 命令 | 为什么重要 |
|---|---|---|
| 更新软件源索引 | sudo apt update | 必须先做 |
| 升级所有已安装包(保守升级) | sudo apt upgrade | 修补已知漏洞 |
| 完整升级(允许依赖变更) | sudo apt full-upgrade | 处理内核/重要库升级 |
| 自动安装安全更新(强烈推荐) | sudo apt install unattended-upgrades apt-listchanges sudo dpkg-reconfigure unattended-upgrades | 无人值守修补漏洞(选 Yes) |
| 清理无用依赖 | sudo apt autoremove && sudo apt autoclean | 保持系统干净 |
| 重启(如果内核升级了) | sudo reboot | 应用新内核 |
小贴士:把 unattended-upgrades 打开后,Debian 会自动在后台安装安全更新,几乎是服务器标配。
阶段 3:网络 + 防火墙(服务器暴露公网必做)
检查当前网络配置
Baship -c addr show ss -tuln # 查看监听端口启用并配置 nftables 防火墙(Debian 13 默认推荐 nftables)
Bashsudo apt install nftables sudo systemctl enable nftables最简规则示例(只允许 SSH + 你需要的服务):
Bashsudo nano /etc/nftables.conf内容示例(只开 22 端口 + 允许 loopback):
texttable inet filter { chain input { type filter hook input priority 0; policy drop; ct state established,related accept iif lo accept tcp dport 22 accept # 再加你需要的端口,如 80、443 等 } chain forward { type filter hook forward priority 0; policy drop; } chain output { type filter hook output priority 0; policy accept; } }应用:
Bashsudo nft -f /etc/nftables.conf sudo systemctl restart nftables新手常用替代方案:如果你怕写规则,用 ufw(更简单):
Bashsudo apt install ufw sudo ufw allow OpenSSH sudo ufw enable sudo ufw status
阶段 4:常用监控与运维操作(每天/每周看一眼)
| 目的 | 命令 | 说明 |
|---|---|---|
| 查看系统负载/进程 | top 或 htop(需安装) | htop 更友好:sudo apt install htop |
| 查看内存使用 | free -h | -h 人类可读 |
| 查看磁盘使用 | df -h / du -sh /* | 找哪个目录占空间大 |
| 查看日志(最重要!) | journalctl -u ssh / journalctl -p err -b | 看服务报错 / 本次开机错误 |
| 实时查看日志 | tail -f /var/log/auth.log | 监控登录尝试 |
| 列出所有服务状态 | systemctl list-units --type=service | — |
| 重启某个服务 | sudo systemctl restart nginx | 示例 |
| 开机自启服务 | sudo systemctl enable mariadb | — |
阶段 5:新手最容易踩的坑 & 正确做法
别直接改 /etc/ssh/sshd_config 里的端口 → 改端口防扫描效果很有限,远不如用密钥 + 禁用密码登录有效。
别长期用密码登录 SSH → 生成密钥对:
Bashssh-keygen -t ed25519 ssh-copy-id yourname@server-ip然后在 sshd_config 里设 PasswordAuthentication no
别装一堆没用的监控工具 → 先学会 htop + journalctl + df 就够用 80% 场景。
备份 /etc → 重要配置文件改坏了能救命:
Bashsudo tar czf /root/etc-backup-$(date +%F).tar.gz /etc
阶段 6:推荐新手必装小工具(服务器常用)
Bash
sudo apt install htop tree tmux vim curl wget fail2ban logwatch dnsutils net-tools- fail2ban:自动封禁暴力破解 IP(默认保护 SSH)
- logwatch:每天发邮件总结日志异常(需配置 postfix 或其他 MTA)
- tmux:断线也能继续运行命令
总结:新手服务器“保命 10 条” checklist
- 创建普通用户 + sudo,禁用 root ssh 登录
- 启用 unattended-upgrades 自动安全更新
- 配置 nftables / ufw,只开必要端口
- 用密钥登录 SSH,禁用密码登录
- 安装 fail2ban
- 定期 apt update && apt full-upgrade && apt autoremove
- 学会看 journalctl 和 /var/log/auth.log
- 重要配置文件改前备份
- 别乱加第三方源,除非真的需要
- 服务器对外暴露时,最少权限原则永远第一
掌握以上内容,你已经能把一台 Debian 服务器从“随便就能被黑”提升到“大多数机器人会跳过”的水平了。
版权声明:
作者:后浪云
链接:https://idc.net/help/442468/
文章版权归作者所有,未经允许请勿转载。
THE END
