iT邦幫忙

2025 iThome 鐵人賽

DAY 11
0
Cloud Native

我在 CKS 考完只拿 47% 後痛定思痛決定好好準備內容系列 第 11

[Day11] 3-1. Minimize host OS footprint (reduce attack surface)

  • 分享至 

  • xImage
  •  

在 OS 中可以限制哪些使用者可以訪問
(1) 大致可以分類為 User Account, Superuser Account, System Account, Service Account
(2) user 登入 設定會存於 /etc/passwd, /etc/shadow, /etc/group
(3) 可設定 user nologin, 或者將不適用的 user 進行刪除

今天主要會針對 ssh 介紹

SSH Hardening

  1. ssh 登入方式
    (1) 使用 hostname 以及 ip 登入
ssh <hostname/IP>

(2) 透過指定 User 名稱登入

ssh <User>@<hostname/IP>

(3) 使用 key 登入

ssh -i <key.pem> <User>@<hostname/IP>
  1. 建立 key
ssh-keygen -t iroman
  1. public key 放置位置
~/.ssh/authorized_keys
  1. ssh 配置建立
# /etc/ssh/sshd_config
PermitRootLogin no         # 無法使用 PermitRootLogin
PasswordAuthentication no  # 無法使用 password 登入
  1. 額外內容
    除了正常的 ssh key 登入外,可以額外透過 google-authenticator 使用 2FA 登入,以下是配置說明:

(1) 安裝 google-authenticator

sudo apt install libpam-google-authenticator

(2) 修改配置檔

# vim /etc/ssh/sshd_config

PasswordAuthentication no #可以不用使用密碼
ChallengeResponseAuthentication yes #允許驗證
UsePAM yes  #可以使用PAM
AuthenticationMethods publickey,keyboard-interactive:pam #可以使用public key 及PAM

# vim /etc/pam.d/sshd

確保文件中包含以下行(通常在頂部附近):

nullok 參數允許沒有設置 Google Authenticator 的用戶仍然可以登錄。
在同一個文件中,找到並註釋掉(在行首添加 #)要求輸入密碼的行。通常是這樣的:
Copy# @include common-auth

加上
auth required pam_google_authenticator.so

(3) 重啟 sshd

sudo systemctl restart sshd

(4) 設定 MFA

google-authenticator

(5) 輸入後會出現 MFA 的QR code,與一般 MFA 綁定過程相同

參考資料
https://stackoverflow.com/questions/71657716/2fa-is-not-working-with-ubuntu-aws-instance


上一篇
[Day10] 2-4. Upgrade Kubernetes to avoid vulnerabilities
系列文
我在 CKS 考完只拿 47% 後痛定思痛決定好好準備內容11
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言