新 Debian 主機上線前的檢查清單
拿到一台全新的 Debian 主機後,我固定會做的十件事:帳號、SSH、更新、防火牆與時間同步。
每次開新主機都從頭想一遍很浪費時間,所以把流程整理成清單。以下以 Debian 12 為例,Ubuntu LTS 幾乎相同。
1. 更新系統
apt update && apt full-upgrade -y
apt install -y sudo curl vim htop unattended-upgrades
先更新再做其他事,避免後面裝套件時遇到相依性衝突。
2. 建立一般使用者
不要長期用 root 操作:
adduser alice
usermod -aG sudo alice
3. 設定 SSH 金鑰登入
在自己的電腦上產生金鑰(若還沒有)並複製到主機:
ssh-keygen -t ed25519 -C "laptop"
ssh-copy-id alice@server
確認可以用金鑰登入之後,再修改 /etc/ssh/sshd_config.d/10-hardening.conf:
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
重新載入 SSH 前,保留一個已登入的工作階段。設定錯誤時才有退路。
sshd -t && systemctl reload ssh
4. 防火牆
只開放真正需要的連接埠:
apt install -y ufw
ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw allow 80,443/tcp
ufw enable
5. 自動安全更新
dpkg-reconfigure -plow unattended-upgrades
預設只會套用 security 來源的更新,對伺服器來說是相對安全的折衷。
6. 時區與時間同步
timedatectl set-timezone Asia/Taipei
timedatectl status # 確認 System clock synchronized: yes
時間不準會讓 TLS 憑證驗證、日誌比對都出問題。
7. Swap
小記憶體主機建議加一點 swap,避免 OOM 直接殺掉服務:
fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile && swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
8. 主機名稱
hostnamectl set-hostname web-01
並確認 /etc/hosts 有對應的項目。
9. 日誌保存上限
journald 預設可能吃掉不少空間,在 /etc/systemd/journald.conf 設上限:
SystemMaxUse=500M
10. 記錄下來
最後把這台主機的用途、開放的連接埠與重要設定寫進筆記。三個月後的自己會感謝你。
小結
| 項目 | 指令/檔案 |
|---|---|
| 更新 | apt full-upgrade |
| SSH | /etc/ssh/sshd_config.d/ |
| 防火牆 | ufw |
| 自動更新 | unattended-upgrades |
| 時間 | timedatectl |