Ubuntu 預設鎖定 root 帳號(無密碼),透過 sudo 提權。若需啟用 root 密碼登入:
sudo passwd root
# 輸入當前使用者密碼(sudo 驗證)
# 輸入新的 root 密碼
# 再次確認新密碼
# passwd: password updated successfully
之後可使用 su - 或 SSH 直接以 root 登入(若 SSH 允許)。
編輯 SSH 伺服器配置檔:
sudo vi /etc/ssh/sshd_config
# 1. 禁止密碼認證(核心設定)
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no # 若使用 PAM 需額外配置,建議關閉
# 2. Root 登入控制(三選一)
# 選項 A:僅允許金鑰登入(推薦,兼顧管理需求與安全)
PermitRootLogin prohibit-password
# 或 PermitRootLogin without-password (舊語法)
# 選項 B:完全禁止 root 遠程登入(最安全)
# PermitRootLogin no
# 選項 C:允許密碼+金鑰(不安全,不推薦)
# PermitRootLogin yes
# 3. 限制認證嘗試次數與時間
MaxAuthTries 3
LoginGraceTime 30s
# 4. 僅允許特定使用者/群組(可選)
AllowUsers hadoop admin
# AllowGroups sshusers
sudo systemctl restart sshd
# 或
sudo service ssh restart
ssh -i ~/.ssh/id_rsa user@hostssh -o PreferredAuthentications=password user@host 應提示 Permission denied (publickey)。sshd_config。cp /etc/ssh/sshd_config{,.bak},並在當前會話保持連線狀態下開新終端機測試。# 客戶端生成(若無)
ssh-keygen -t ed25519 -C "user@host-$(date +%F)"
# ed25519 比 RSA 更安全、金鑰更短、速度更快
# 部署公開金鑰至伺服器
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
# 伺服器端 authorized_keys 範例(可加限制)
# command="/usr/local/bin/backup.sh",restrict,from="192.168.1.0/24" ssh-ed25519 AAAA... user@host
「修改 root 密碼」與「SSH 僅允許金鑰登入」是兩個獨立但常搭配的安全加固動作。生產環境強烈建議:禁用 root 密碼登入(PermitRootLogin prohibit-password)+ 禁用所有密碼認證(PasswordAuthentication no)+ 普通使用者 sudo 提權 + 金鑰管理自動化。