Ubuntu 22.04 SSH 金鑰認證與安全加固

Ubuntu 22.04 SSH Key Authentication and Security Hardening

本文將介紹如何在 Ubuntu 22.04 上設定 SSH 金鑰認證,並透過多項安全加固措施來保護您的伺服器免受未授權存取。

金鑰認證設定

在客戶端產生金鑰對

  1. 建立 SSH 目錄並設定權限

    1
    2
    
    mkdir -p ~/.ssh
    chmod 700 ~/.ssh
    
  2. 產生 ED25519 金鑰(推薦,比 RSA 更安全且效能更好)

    1
    
    ssh-keygen -t ed25519 -C "your_email@example.com"
    

    或者使用 4096 位元的 RSA 金鑰

    1
    
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  3. 將公鑰複製到伺服器

    1
    
    ssh-copy-id -i ~/.ssh/id_ed25519.pub username@server_ip
    

在伺服器端設定

  1. 確認公鑰已正確新增至 ~/.ssh/authorized_keys

    1
    
    cat ~/.ssh/authorized_keys
    
  2. 設定正確的檔案權限

    1
    2
    
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    

停用密碼登入

確認金鑰認證正常運作後,停用密碼登入可大幅提升安全性。

  1. 編輯 SSH 設定檔

    1
    
    sudo nano /etc/ssh/sshd_config
    
  2. 修改或新增以下設定

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    # 停用密碼認證
    PasswordAuthentication no
    
    # 停用空密碼
    PermitEmptyPasswords no
    
    # 停用挑戰回應認證
    ChallengeResponseAuthentication no
    
    # 啟用公鑰認證
    PubkeyAuthentication yes
    
    # 停用 root 直接登入
    PermitRootLogin no
    
  3. 檢查設定檔語法是否正確

    1
    
    sudo sshd -t
    
  4. 重新啟動 SSH 服務

    1
    
    sudo systemctl restart ssh
    

更改預設埠

將 SSH 從預設的 22 埠改為其他埠號,可以減少自動化掃描攻擊。

  1. 編輯 SSH 設定檔

    1
    
    sudo nano /etc/ssh/sshd_config
    
  2. 修改埠號(例如改為 2222)

    1
    
    Port 2222
    
  3. 更新防火牆規則

    1
    2
    3
    4
    5
    
    # 允許新埠號
    sudo ufw allow 2222/tcp
    
    # 移除舊的 SSH 規則(在確認新埠可用後)
    sudo ufw delete allow ssh
    
  4. 重新啟動 SSH 服務

    1
    
    sudo systemctl restart ssh
    
  5. 測試新埠連線(開啟新終端機測試,不要關閉現有連線)

    1
    
    ssh -p 2222 username@server_ip
    

Fail2ban 整合

Fail2ban 可以自動封鎖多次登入失敗的 IP 位址。

安裝 Fail2ban

1
2
sudo apt update
sudo apt install fail2ban -y

設定 Fail2ban

  1. 建立本地設定檔

    1
    
    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
    
  2. 編輯設定檔

    1
    
    sudo nano /etc/fail2ban/jail.local
    
  3. [sshd] 區段加入以下設定

    1
    2
    3
    4
    5
    6
    7
    8
    
    [sshd]
    enabled = true
    port = 2222
    filter = sshd
    logpath = /var/log/auth.log
    maxretry = 3
    findtime = 300
    bantime = 3600
    
    • maxretry: 最大嘗試次數
    • findtime: 計算嘗試次數的時間範圍(秒)
    • bantime: 封鎖時間(秒),設為 -1 表示永久封鎖
  4. 啟動並設定開機自動啟動

    1
    2
    
    sudo systemctl enable fail2ban
    sudo systemctl start fail2ban
    
  5. 檢查狀態

    1
    
    sudo fail2ban-client status sshd
    

管理被封鎖的 IP

1
2
3
4
5
# 查看被封鎖的 IP
sudo fail2ban-client status sshd

# 手動解除封鎖
sudo fail2ban-client set sshd unbanip <IP_ADDRESS>

其他安全建議

限制可登入的使用者

/etc/ssh/sshd_config 中加入

1
2
3
4
5
# 只允許特定使用者登入
AllowUsers user1 user2

# 或只允許特定群組
AllowGroups sshusers

設定連線逾時

1
2
3
4
5
6
7
8
# 客戶端存活間隔(秒)
ClientAliveInterval 300

# 最大存活次數
ClientAliveCountMax 2

# 登入寬限時間
LoginGraceTime 30

停用不安全的功能

1
2
3
4
5
6
7
8
# 停用 X11 轉發
X11Forwarding no

# 停用 TCP 轉發(如果不需要)
AllowTcpForwarding no

# 停用 Agent 轉發
AllowAgentForwarding no

使用更強的加密演算法

1
2
3
4
5
6
7
8
# 指定加密演算法
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com

# 指定 MAC 演算法
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com

# 指定金鑰交換演算法
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org

啟用雙因素認證(選用)

可以整合 Google Authenticator 實現雙因素認證

1
sudo apt install libpam-google-authenticator -y

定期檢查登入日誌

1
2
3
4
5
6
7
8
# 查看登入失敗記錄
sudo grep "Failed password" /var/log/auth.log

# 查看成功登入記錄
sudo grep "Accepted" /var/log/auth.log

# 即時監控
sudo tail -f /var/log/auth.log

完整的安全設定範例

以下是一份完整的 /etc/ssh/sshd_config 安全設定範例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 基本設定
Port 2222
Protocol 2
AddressFamily inet

# 認證設定
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
UsePAM yes

# 使用者限制
AllowUsers your_username

# 連線設定
ClientAliveInterval 300
ClientAliveCountMax 2
LoginGraceTime 30
MaxAuthTries 3
MaxSessions 2

# 安全功能
X11Forwarding no
AllowTcpForwarding no
AllowAgentForwarding no

# 加密設定
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org

# 日誌
LogLevel VERBOSE

結語

透過以上設定,您的 SSH 服務將具備更高的安全性。請記住:

  1. 一定要在設定完成後保持一個 SSH 連線測試新設定
  2. 定期更新系統和 SSH 套件
  3. 定期檢查日誌檔案
  4. 備份您的私鑰並妥善保管
  5. 考慮使用 SSH 跳板機(Jump Host)來進一步保護內部伺服器
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy