調整SSH服務進階設定,
以提高SSH連線安全性
                林國龍 Bill Lin
- 精誠資訊/恆逸教育訓練中心-資深講師
- 技術分類:資訊安全
SSH安全協議是遠端管理Linux主機或網路設備最常使用的方式,除了在網路層的防火牆(主機型或閘道型)上設定過濾來源IP的ACL規則外,我們也可以在應用層進行使用者連線的管制。管制的範圍可以包含:時間、地點(來源IP)、人員、群組、加密協議與參數…。本文的內容將介紹使用者與群組的管理設定方式。
首先,我們只允許使用者 bill 從任何來源IP位址登入,利用AllowUsers參數,只需在該參數後列出允許登入的所有帳號名稱,AllowUsers參數一旦有設定,未在表列中的帳號,包含 root 帳號都將無法登入。
- 
                        在【centos1】主機上建立使用者帳號 bill 並修改其密碼為 Pa55w.rd,同時也建立使用者帳號 student 並修改其密碼為 student[root@centos1 ~]# useradd bill 
 [root@centos1 ~]# echo Pa55w.rd | passwd --stdin bill
 [root@centos1 ~]# useradd student
 [root@centos1 ~]# echo student | passwd --stdin student
- 
                        在【workstation】主機確認可以利用 student與 bill 成功以ssh方式登入【centos1】主機。[root@workstation ~]# ssh bill@centos1 
 bill@centos1's password:
 [bill@centos1 ~]$ exit
 [root@workstation ~]# ssh student@centos1
 student@centos1's password:
 [student@centos1 ~]$ exit
- 
                        使用 vi 在 【centos1】主機修改主要設定檔,在設定檔的最下方加入:[root@centos1 ~]# vi /etc/ssh/sshd_config 
 AllowUsers bill
- 
                        修改完成設定檔後,請重新啟動sshd服務,以確認設定修改生效:[root@centos1 ~]# systemctl restart sshd.service 
- 
                        當我們再度利用 【workstation】 主機使用 student與 bill 以ssh 方式登入【centos1】 主機,畫面摘要如下:[root@workstation ~]# ssh bill@centos1 
 bill@centos1's password:
 [bill@centos1 ~]$ exit
 [root@workstation ~]# ssh student@centos1
 student@centos1's password:
 Permission denied, please try again.
 student@centos1's password:
- 
                        使用 student 與 root 等非 bill 帳號登入時會出現密碼正確,卻無法登入的情況!
- 
                        調整設定,允許 root 從來源IP:172.16.0.250【workstation】的主機登入,這項設定必須搭配 PermitRootLogin yes 參數:
- 
                        請使用 vi 在 【centos1】 修改主要設定檔,在設定檔的最下方加入:[root@centos1 ~]# vi /etc/ssh/sshd_config 
 AllowUsers root@172.16.0.250
- 
                        修改完成設定檔後,請重新啟動服務,以確認設定修改生效:[root@centos1 ~]# systemctl restart sshd.service 
- 
                        請再度利用 【workstation】 主機使用 root 以ssh方式登入【centos1】 主機,此時可以正常登入:[root@workstation ~]# ssh root@centos1 
 root@centos1's password:
 Activate the web console with: systemctl enable --now cockpit.socket
- 
                         若利用其他主機 【centos2】 主機使用 root 以ssh方式登入 【centos1】 主機:[root@centos2 ~]# ssh root@centos1 
 root@centos1's password:
 Permission denied, please try again.登入時會出現密碼正確,卻無法登入的情況! 
- 
                        若利用其他主機 【centos2】 主機使用 root 以ssh方式登入
 【centos1】 主機:[root@centos2 ~]# ssh root@centos1 
 root@centos1's password:
 Permission denied, please try again.登入時會出現密碼正確,卻無法登入的情況! 
- 允許整個 172.16.0.0/24 網段使用 root 登入:
- 
                        請使用 vi 在 【centos1】 修改主要設定檔,在設定檔的最下方加入:[root@centos1 ~]# vi /etc/ssh/sshd_config 
 AllowUsers root@172.16.0. *
- 
                        修改完成設定檔後,請重新啟動服務,以確認設定修改生效:[[root@centos1 ~]# systemctl restart sshd.service 
- 
                        請再度使用 【workstation】 與 【centos2】 主機使用 root 以ssh方式登入
 【centos1】 主機,此時兩台主機都可以正常登入。
- 
                        若需要在同一行指定多個帳戶與來源 IP 位址登入:
- 
                        請使用 vi 在 【centos1】 修改主要設定檔,在設定檔的最下方加入:[root@centos1 ~]# vi /etc/ssh/sshd_config 
 AllowUsers root@172.16.0.250 student@172.16.0.102
- 
                        修改完成設定檔後,請重新啟動服務,以確認設定修改生效:[root@centos1 ~]# systemctl restart sshd.service 
- 
                        我們再度使用 【workstation】 與 【centos2】 主機,利用 root與 student帳號以ssh 方式登入 【centos1】 主機,此時 【workstation】主機只能用 root 登入成功、【centos2】主機只能使用 student帳號正常登入。
- 
                        使用群組限制ssh登入,只允許群組 ssh-users 成員登入:
- 
                        首先,利用 groupadd ssh-users 建立使用者群組
- 
                        將 student 帳號加入 ssh-users 群組[root@centos1 ~]# usermod -aG ssh-users student 
- 
                        使用 id student 確認該帳號確實有加入ssh-users 群組[root@centos1 ~]# id student 
 uid=1000(student) gid=1000(student)
 groups=1000(student),10(wheel),1001(ssh-users)
- 
                        在 【centos1】主機修改主要設定檔,在設定檔的最下方加入:[root@centos1 ~]# vi /etc/ssh/sshd_config 
 AllowGroups ssh-users
- 
                        修改完成設定檔後,請重新啟動服務,以確認設定修改生效:[root@centos1 ~]# systemctl restart sshd.service 
- 
                        請再度使用 【workstation】主機,利用 root與 student 以ssh方式登入【centos1】 主機,此時 【workstation】主機只能用 student 登入成功 
- 
                        同時使用 AllowGroups 與 AllowUsers 時,必須兩者條件同時成立才能正常登入:
- 
                        使用 usermod -aG ssh-users bill 將 bill 帳號加入 ssh-users 群組
- 
                        使用 id bill 確認該帳號確實有加入ssh-users 群組[root@centos1 ~]# usermod -aG ssh-users bill 
 [root@centos1 ~]# id bill
 uid=1001(bill) gid=1002(bill) groups=1002(bill),1001(ssh-users)
- 
                        使用 vi 在 【centos1】 修改主要設定檔,在設定檔的最下方加入:[root@centos1 ~]# vi /etc/ssh/sshd_config 
 AllowUsers root student
 AllowGroups ssh-users
- 
                        修改完成設定檔後,請重新啟動服務,以確認設定修改生效:[root@centos1 ~]# systemctl restart sshd.service 
請再度使用 【workstation】主機,利用 root、bill與student以 ssh方式登入 【centos1】主機,此時【workstation】主機只能用 student 登入成功,因為 root 未加入 ssh-users 群組,bill 則是未列在 AllowUsers 清單中,證明同時使用 AllowGroups 與 AllowUsers 參數時,必須兩者條件同時成立才能正常登入。
 
                    