CentOS 7 用金鑰進行SSH遠端登入出現Server refused our key的解法

依照之前設定Ubuntu中設定SSH遠端登入用金鑰後,依樣畫葫蘆設定在CentOS居然登入失敗出現Server refused our key的訊息,以往在Ubuntu上的經驗是手殘金鑰複製不完全重作就好,結果CentOS的原因完全不一樣在此紀錄一下。

這個問題的相關內容可以看到Will保哥在部落格文中CentOS 6.4 安裝後以金鑰登入出現 Server refused our key中,解釋問題的原因是SELinux這個增強Linux安全性的模組所導致。

不過保哥本人是手動建立.ssh資料夾,我則是取巧以danny身分在CentOS中下了ssh指令

ssh localhost

再度遠端連線到本機,讓ssh自動幫我產生.ssh相關資料夾,所以我的資料夾與檔案的SELinux Tag應該是正確的。

但是要看SELinux標籤的話,ls要多下一個Z參數

[danny@localhost .ssh]$ ls -laFZ
drwx------. danny danny unconfined_u:object_r:ssh_home_t:s0 ./
drwx------. danny danny unconfined_u:object_r:user_home_dir_t:s0 ../
-rw-rw-r--. danny danny unconfined_u:object_r:ssh_home_t:s0 authorized_keys
-rw-r--r--. danny danny unconfined_u:object_r:ssh_home_t:s0 known_hosts

看起來標籤都正確,擔心自己眼殘,在家目錄下用以下指令重設SELinux安全強化標籤的預設值

restorecon -R -v .ssh/

使用後,依然無法登入。

乖乖地,先看SSH登入紀錄

CentOS7中要找ssh登入紀錄要去/var/log/secure這個記錄檔

記得切到root帳號,再用cat查看secure紀錄

cat /var/log/secure

可以看到最後一筆是剛剛ssh連線的紀錄

Jan  8 23:45:47 localhost sshd[147675]: Authentication refused: bad ownership or modes for file /home/danny/.ssh/authorized_keys

告訴我們驗證被拒絕:因為使用者danny的authorized_keys這個檔案所有權或模式錯誤。第一時間會想到是檔案權限的問題,查詢其他設定金鑰的教學設定會要求要設定authorized_keys權限為600,改成600後真的可以登入成功,至於為什麼呢?

SSH Authentication Refused: Bad Ownership or Modes for Directory有提到跟ssh_config裡面StrictModes這個選項有關。

StrictModes

看一下文件說明sshd_config — OpenSSH SSH daemon configuration file

StrictModes

         Specifies whether sshd(8) should check file modes and ownership of the user's files
         and home directory before accepting login.  This is normally desirable because
         novices sometimes accidentally leave their directory or files world-writable.  The
         default is “yes”.  Note that this does not apply to ChrootDirectory, whose
         permissions and ownership are checked unconditionally.

大意是講說避免金鑰被其他人讀取這種低級錯誤,StrictModes預設為true,sshd會對金鑰與上層檔案路徑的權限來比對,若不符合則禁止存取,但看了說明還是不曉得有多嚴格。

在嘗試修改StrictModes為no時,我的CentOS真的可以用金鑰登入,但這不是正確的解法,我不知到為什麼同樣的設定方式Ubuntu可以正常登入,而CentOS無法正常登入,所以我跑去StackexChange上詢問,得到網友回覆要下指令連同根目錄到.ssh的權限都要看。

Ubuntu:

danny@danny-ubuntu:~/.ssh$ ls -ld  / /home /home/danny /home/danny/.ssh  /home/danny/.ssh/authorized_keys

drwxr-xr-x 24 root  root  4096  1月  5 21:59 /
drwxr-xr-x  4 root  root  4096  1月  9 13:33 /home
drwxr-xr-x 22 danny danny 4096  1月  9 13:14 /home/danny
drwx------  2 danny danny 4096  1月  9 17:23 /home/danny/.ssh
-rw-rw-r--  1 danny danny  398  1月  8 09:52 /home/danny/.ssh/authorized_keys

CentOS:

[danny@localhost .ssh]$ ls -ld  / /home /home/danny /home/danny/.ssh  /home/danny/.ssh/authorized_keys

dr-xr-xr-x. 17 root  root  224  7月 29  2018 /
drwxr-xr-x.  3 root  root   19  7月 29  2018 /home
drwx------. 10 danny danny 240  1月  9 13:12 /home/danny
drwx------.  2 danny danny 150  1月  9 17:23 /home/danny/.ssh
-rw-rw-r--.  1 danny danny 809  1月  8 15:59 /home/danny/.ssh/authorized_keys

可以看到CentOS跟Ubuntu的預設家目錄權限就是不一樣的,就算我的新增authorized_keys後,兩個OS的預設權限都是644,.ssh資料夾都是700,但是我的家目錄不一樣就會影響到StrictModes嚴格模式的計算結果。所以同樣是Linux系統,同樣開啟嚴格模式,但是我在Ubuntu設定上面使用預設權限不用調整就可以成功登入,在CentOS方面就不一樣,導致不同教學文有不同的設定方式,建議是不管哪一個作業系統authorized_keys都設為600僅限自己可以讀寫,改成600後我的CentOS就可以正常地使用金鑰登入了。

如果有網友知道OpenSSH原始碼中,StrictModes的計算方式歡迎告訴小弟。

Facebook留言板