SSH 公開鍵認証 - 物理ログイン後にのみ有効です。

SSH 公開鍵認証 - 物理ログイン後にのみ有効です。

公開鍵認証を使用してSSH経由でUbuntuサーバーに接続しようとしています。何らかの理由で「権限が拒否されました(公開鍵)」というメッセージが表示されます。クライアントで実行するたびに

ssh -i ~/.ssh/id_rsa <username>@<ip> -p <port>

私のサーバーのauth.logには、次の出力があります。

sshd[1425]: Connection closed by <client-ip> [preauth]

ただし、同じユーザー名を使用してサーバーに物理的にログインすると、クライアントの次のSSH接続は成功します。ただし、物理的にログアウトすると、クライアントの次のSSHセッションは失敗します。

/etc/ssh/sshd_config

# What ports, IPs and protocols we listen for
Port <port>
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no

PermitEmptyPasswords no
ChallengeResponseAuthentication no

PasswordAuthentication no

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

UsePAM no

ClientAliveInterval 30
ClientAliveCountMax 99999    

なぜこれが起こるのかについての手がかりはありますか?それともセキュリティに関する提案がありますか?ありがとうございます!

ベストアンサー1

コメントで述べたように、暗号化されたホームディレクトリを使用しており、おそらく次のものを使用します。pam_mountインストールしてください。
pam_mount は、ログイン時に取得したパスワードを使用してパーティションをマウントします。 SSH公開鍵でログインしようとしているので、2つの問題があります。

  1. 公開鍵認証中にパスワードは送信されないため、それを使用してホームディレクトリをマウントすることはできません。
  2. pam_mount を使用すると、ホームディレクトリがマウントされます。後ろにログインしましたが、ファイルをインポートする必要がありsshdますauthorized_keys今後ログインするとインストールされません。

これらの問題のいずれかが動作を妨げるのに十分です。

唯一の解決策は、ホームディレクトリから公開鍵を取得することです。これは実際には非常に簡単です。

まず、authorized_keysホームディレクトリからファイルをコピーします。

cp -a /home/$USER/.ssh/authorized_keys /home/$USER-authorized_keys

次に、sshd以下を追加してファイルの使用を示します/etc/ssh/sshd_config(既存のエントリがある場合は置き換えます)。

AuthorizedKeysFile .ssh/authorized_keys /home/%u-authorized_keys

そしてバウンスsshd

ただし、これはホームディレクトリをマウントしないことに注意してください。ホームディレクトリの復号化にはまだパスワードが必要です。 pam_mountの設定方法によっては、パスワードの入力を求められたり、シェルに入って家のマウントが解除されることがあります。

おすすめ記事