為什麼 chown 600 id_rsa 可以修復權限問題?

為什麼 chown 600 id_rsa 可以修復權限問題?

chown我最近將密鑰導入到一台新機器(Mac)上,卻忘記將權限修改為 600 chmod。奇怪的是,這解決了權限問題,如下所示:

% git pull origin develop
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/m/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/Users/m/.ssh/id_rsa": bad permissions
git@<redacted>: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
%
% sudo chown 600 ~/.ssh/id_rsa
Password:
%
% ls -l ~/.ssh/id_rsa
-rw-r--r--@ 1 600  staff  1679 Mar 25 15:14 /Users/m/.ssh/id_rsa
%
% git pull origin develop
From <redacted>
 * branch            develop    -> FETCH_HEAD
Already up to date.
%
% ssh <redacted>
The authenticity of host '<redacted> (<redacted>)' can't be established.
ECDSA key fingerprint is <redacted>.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '<redacted>' (ECDSA) to the list of known hosts.
Warning: your password will expire in 5 days
Last login: Fri Jul 27 19:51:32 2018 from <redacted>
-bash-4.2$

為什麼會這樣呢?

答案1

根據開放SSH原始碼,ssh僅檢查私鑰檔案的權限(如果該檔案的擁有者是正在執行的使用者)ssh。如果該檔案屬於另一個用戶,那麼 ssh 將允許任何可以讀取該檔案的人使用它。

if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
     ^^^^^^^^^^^^^^^^^^^--owner ^^^^^^^^^^^^^^^^--permissions
    error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    error("@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @");
    error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    ...

這似乎沒有在手冊頁

原始碼包含以下註釋,表示該行為是故意的:

/*
 * if a key owned by the user is accessed, then we check the
 * permissions of the file. if the key owned by a different user,
 * then we don't care.
 */

實際上,檢查屬於另一個使用者的金鑰檔案的權限不會提高安全性。如果您可以讀取該文件,則可以透過製作該文件的副本並限制副本的權限來解決權限檢查問題。

答案2

chmod 指令代表“更改模式”,允許更改檔案和資料夾的權限,在 UNIX 中也稱為“模式”。 chown 命令代表“更改所有者”,允許更改給定檔案或資料夾的擁有者,可以是使用者和群組。

相關內容