摘要
一般為了方便運維管理都會配置 ssh 免密登入,ssh 免密登入實現也很方便。今天遇到一個完成了配置了卻不能生效的問題。
思考
遇到這個問題一般有以下幾點:

authorized_keys 檔案是否啓用
.ssh 和 authorized_keys 檔案許可權問題

排查
檢查 AuthorizedKeysFile 配置是否啓用 authorized_keys
root@pts/1 $ cat /etc/ssh/sshd_config |egrep AuthorizedKeysFile
AuthorizedKeysFile .ssh/authorized_keys

沒有問題,繼續檢查.ssh (700) 和 authorized_keys(644) 許可權
root@pts/1 $ getfacl /root/.ssh/
getfacl: Removing leading ‘/’ from absolute path names
# file: root/.ssh/
# owner: root
# group: root
user::rwx
group::—
other::—

root@pts/1 $ getfacl /root/.ssh/authorized_keys
getfacl: Removing leading ‘/’ from absolute path names
# file: root/.ssh/authorized_keys
# owner: root
# group: root
user::rw-
group::—
other::—

authorized_keys 許可權不對,修改一下 chmod 644 authorized_keys
再次嘗試結果發現還是不行。但是該設定的許可權都設定了。既然.ssh 目錄和其下檔案的許可權都 OK 了,那就檢查下其父目錄的許可權,也就是這裏的/root 的許可權
root@pts/1 $ getfacl /root/
getfacl: Removing leading ‘/’ from absolute path names
# file: root/
# owner: ftpuser
# group: ftpuser
user::r-x
group::r-x
other::—

發現這裏/root 的屬主都發生了變化。為了不影響別的業務情況,保留這裏的 ftpuser 許可權,利用 setfacl 新增特殊 ACL 許可權
root@pts/1 $ chown -R root:root /root/
root@pts/1 $ setfacl -m u:ftpuser:rwx /root/

root@pts/1 $ getfacl /root/
getfacl: Removing leading ‘/’ from absolute path names
# file: root/
# owner: root
# group: root
user::rwx
user:ftpuser:rwx #effective:r-x
group::r-x
mask::r-x
other::r-x

附加

許可權問題

/root 775
/root/.ssh 700
/root/.ssh/authorized_keys 644

開啓檔案 AuthorizedKeysFile .ssh/authorized_keys