tkuchikiの日記

新ブログ https://blog.tkuchiki.net

パスワードを設定していないユーザで ssh ログインしようとすると公開鍵認証でもログインに失敗する

公開鍵認証で ssh ログインできるように設定していたが、
なぜか Permission denied でログインできない。
id_rsa や id_rsa.pub 、authorized_keys 、それらの Permission を見なおしても特におかしいところが見当たらない。
ssh の -v で debug message を出力しても、
公開鍵認証をしようとしているのに、失敗してパスワード認証をしようとしていてよくわからない。

そこで、先輩に設定を確認していただいたところ、
vim /var/log/secure に以下の記述を発見。

User your_username not allowed because account is locked

これは、sshd_config の設定によっては、パスワードを設定していないユーザが ssh でログインしようとするとそれを弾くようになっているそう。
パスワードを設定すると無事ログインできるようになった。


今後のために自分でも調べて見たところ、以下の様なページを発見。
git - SSH Key asks for password - Stack Overflow
GSI-Enabled OpenSSH Server Troubleshooting

OpenSSH now checks for locked accounts by default.
On Linux systems, locked accounts are defined as those that have !! in the password field of /etc/shadow.
This is the default entry for accounts created with the useradd command.
Even if you are using GSI authentication and do not need local passwords, sshd won't let the user login with this message:

Regardless of the authentication type, the account is checked to ensure that it is accessible. An account is not accessible if it is locked, listed in DenyUsers or its group is listed in DenyGroups. The definition of a locked account is system dependant. Some platforms have their own account database (eg AIX) and some modify the passwd field ( "*LK*" on Solaris and UnixWare, "*" on HP-UX, containing "Nologin" on Tru64, a leading "*LOCKED*" on FreeBSD and a leading "!!" on Linux). If there is a requirement to disable password authentication for the account while allowing still public-key, then the passwd field should be set to something other than these values (eg "NP" or "*NP*" ).

Linux だと、/etc/shadow の USERNAME:の次に「!!」と書いて有る場合は lockされているっぽい(FreeBSD だと 「*LOCKED*」)。
確かに、/etc/shadow に !! と書いてあった。
パスワードを設定したら、!! が ランダム文字列に置き換わっていた。

よくわからない理由で ssh がこける場合は、
/etc/shadow と /var/log/secure を確認すると良いと思った。

他にも、/etc/ssh/sshd_config に、

UsePam yes

と設定すれば対応することができるそう(sshの"User hoge not allowed because account is locked - うまい棒の断片 - 断片部)。


ただ、UsePam yes にすると危ないようなので 黙ってパスワードを設定したほうが良いかもしれない(sshの認証を公開鍵認証だけにしたい時にUsePAMの設定をどうすべきか: よしなしごと)。

また、以下の設定値を 両方 no にしておかないときかない?(puttyでssh接続(2):PasswordAuthentication no が効かない件について - Lyo.blog)

PasswordAuthentication no
ChallengeResponseAuthentication no