質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
公開鍵認証

公開鍵認証とは、公開鍵と秘密鍵の2つの鍵の組を利用する、SSHで利用される認証方式です。

CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

さくらのVPS

さくらのVPSは、さくらインターネット社が提供するVPS(仮想専用サーバー)です。高速なSSDの選択や複数台構成も可能。利用者に応じた柔軟なプランが用意されています。大規模システムにも対応可能なスケーラビリティを備えたホスティングサービスです。

SSH

SSH(Secure Shell)は、セキュアチャネルを通してデータを交換するためのネットワークプロトコルです。リモートサーバーへのコマンド実行やファイル転送を行う時に一般的に使用されます。

ログイン

ログインは、ユーザーがコンピューターシステムにアクセスするプロセスの事を呼びます。

Q&A

解決済

3回答

6381閲覧

SSH鍵認証アクセス Permission denied

ryu2142

総合スコア18

公開鍵認証

公開鍵認証とは、公開鍵と秘密鍵の2つの鍵の組を利用する、SSHで利用される認証方式です。

CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

さくらのVPS

さくらのVPSは、さくらインターネット社が提供するVPS(仮想専用サーバー)です。高速なSSDの選択や複数台構成も可能。利用者に応じた柔軟なプランが用意されています。大規模システムにも対応可能なスケーラビリティを備えたホスティングサービスです。

SSH

SSH(Secure Shell)は、セキュアチャネルを通してデータを交換するためのネットワークプロトコルです。リモートサーバーへのコマンド実行やファイル転送を行う時に一般的に使用されます。

ログイン

ログインは、ユーザーがコンピューターシステムにアクセスするプロセスの事を呼びます。

0グッド

0クリップ

投稿2020/05/01 04:20

編集2020/05/01 05:52

さくらのVPS上のcentOSへのパスワードログインを不可にし、ssh公開鍵認証接続でログインするように設定したいのですが、Permission deniedされてしまいます。
サーバーOS、クライアントOS双方の.sshディレクトリ、id_rsa、id_rsa.pub、authorized_keys等のpermissionの設定はchmodを実行し完了済みです。
原因がわからず困っています。どうぞよろしくお願いします。
以下デバッグ実行結果。

me@MacBookAir ~ % ssh -vi ~/.ssh/id_rsa vpsuser@xxx.xx.xx.xx OpenSSH_8.1p1, LibreSSL 2.7.3 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 47: Applying options for * debug1: Connecting to xxx.xx.xx.xx [xxx.xx.xx.xx] port 22. debug1: Connection established. debug1: identity file /Users/me/.ssh/id_rsa type 0 debug1: identity file /Users/me/.ssh/id_rsa-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_8.1 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4 debug1: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002 debug1: Authenticating to xxx.xx.xx.xx:22 as 'vpsuser' debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ecdsa-sha2-nistp256 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: Server host key: ecdsa-sha2-nistp256 SHA256:O5rjMSVgOtd9w9KDInCQPi2dE/SIczfZWci81fCeniQ debug1: Host 'xxx.xx.xx.xx' is known and matches the ECDSA host key. debug1: Found key in /Users/me/.ssh/known_hosts:1 debug1: rekey out after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: rekey in after 134217728 blocks debug1: Will attempt key: /Users/me/.ssh/id_rsa RSA SHA256:UBU5Zm+lmOkiTOCYv4QxjDiZzkFmmVKLIvM6g9i3jVo explicit debug1: SSH2_MSG_EXT_INFO received debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512> debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Offering public key: /Users/me/.ssh/id_rsa RSA SHA256:UBU5Zm+lmOkiTOCYv4QxjDiZzkFmmVKLIvM6g9i3jVo explicit debug1: Authentications that can continue: publickey debug1: No more authentication methods to try. vpsuser@xxx.xx.xx.xx: Permission denied (publickey). me@MacBookAir ~ %

追記

yukky1201様

サーバーへ公開鍵を登録した手順は、
1.クライアントOS(Mac)でホームディレクトリに.sshディレクトリを作成
2..sshディレクトリにてssh-keygenを実行しキーペアを作成
3.サーバーOS(centOS)の一般ユーザー(vpsuser)のホームディレクトリに.sshディレクトリを作成
4.クライアントOSからscpでid_rsa.pubをauthorized_keysという名前でサーバーOS側の.sshに転送
5.必要なpermissionの設定をchmodで全て完了
という流れです。

angel_p_57様

permission不備の確認のため、パスワード認証を有効にしssh -vでのログイン実行しました。
以下のように表示されました。

me@MacBookAir ~ % ssh -v vpsuser@xxx.xx.xx.xx OpenSSH_8.1p1, LibreSSL 2.7.3 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 47: Applying options for * debug1: Connecting to xxx.xx.xx.xx [xxx.xx.xx.xx] port 22. debug1: Connection established. debug1: identity file /Users/me/.ssh/id_rsa type 0 debug1: identity file /Users/me/.ssh/id_rsa-cert type -1 debug1: identity file /Users/me/.ssh/id_dsa type -1 debug1: identity file /Users/me/.ssh/id_dsa-cert type -1 debug1: identity file /Users/me/.ssh/id_ecdsa type -1 debug1: identity file /Users/me/.ssh/id_ecdsa-cert type -1 debug1: identity file /Users/me/.ssh/id_ed25519 type -1 debug1: identity file /Users/me/.ssh/id_ed25519-cert type -1 debug1: identity file /Users/me/.ssh/id_xmss type -1 debug1: identity file /Users/me/.ssh/id_xmss-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_8.1 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4 debug1: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002 debug1: Authenticating to xxx.xx.xx.xx:22 as 'vpsuser' debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ecdsa-sha2-nistp256 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: Server host key: ecdsa-sha2-nistp256 SHA256:O5rjMSVgOtd9w9KDInCQPi2dE/SIczfZWci81fCeniQ debug1: Host 'xxx.xx.xx.xx' is known and matches the ECDSA host key. debug1: Found key in /Users/me/.ssh/known_hosts:1 debug1: rekey out after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: rekey in after 134217728 blocks debug1: Will attempt key: /Users/me/.ssh/id_rsa RSA SHA256:UBU5Zm+lmOkiTOCYv4QxjDiZzkFmmVKLIvM6g9i3jVo debug1: Will attempt key: /Users/me/.ssh/id_dsa debug1: Will attempt key: /Users/me/.ssh/id_ecdsa debug1: Will attempt key: /Users/me/.ssh/id_ed25519 debug1: Will attempt key: /Users/me/.ssh/id_xmss debug1: SSH2_MSG_EXT_INFO received debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512> debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey,password debug1: Next authentication method: publickey debug1: Offering public key: /Users/me/.ssh/id_rsa RSA SHA256:UBU5Zm+lmOkiTOCYv4QxjDiZzkFmmVKLIvM6g9i3jVo debug1: Authentications that can continue: publickey,password debug1: Trying private key: /Users/me/.ssh/id_dsa debug1: Trying private key: /Users/me/.ssh/id_ecdsa debug1: Trying private key: /Users/me/.ssh/id_ed25519 debug1: Trying private key: /Users/me/.ssh/id_xmss debug1: Next authentication method: password vpsuser@xxx.xx.xx.xx's password: debug1: Authentication succeeded (password). Authenticated to xxx.xx.xx.xx ([xxx.xx.xx.xx]:22). debug1: channel 0: new [client-session] debug1: Requesting no-more-sessions@openssh.com debug1: Entering interactive session. debug1: pledge: network debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0 debug1: Remote: Ignored authorized keys: bad ownership or modes for directory /home/vpsuser/.ssh debug1: Sending environment. debug1: Sending env LANG = ja_JP.UTF-8 Last login: Fri May 1 14:26:51 2020 from softbank126147178223.bbtec.net SAKURA Internet [Virtual Private Server SERVICE] [vpsuser@tk2-212-15534 ~]$

また、ssh-keygen -y -f ~/.ssh/id_rsa を実行して、id_rsa.pubファイルと内容が一致するかを確認しましたが、きちんと一致しておりました。
Match条件について確認しましたが、sshd_configを見る限りそれでは無さそうです、、。

どうぞよろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

以下、CentOS2台での環境で鍵認証が成功したときのデバッグメッセージの抜粋です

debug1: Next authentication method: publickey debug1: Offering public key: /home/me/.ssh/id_rsa RSA SHA256:LyGvplhzx+vHWpRjKgiQG7vFCUUV8iZ3wmO23UWKezU debug1: Server accepts key: /home/me/.ssh/id_rsa RSA SHA256:LyGvplhzx+vHWpRjKgiQG7vFCUUV8iZ3wmO23UWKezU debug1: Authentication succeeded (publickey).

質問記載にはServer acceptsがかえってきてないので、鍵ペアが不正なのではないでしょうか。

クライアントでの鍵の作成、サーバへの鍵の登録手順を正確に提示されることをおススメします

投稿2020/05/01 05:12

yukky1201

総合スコア2751

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ryu2142

2020/05/01 05:55

サーバーへ公開鍵を登録した手順は、 1.クライアントOS(Mac)でホームディレクトリに.sshディレクトリを作成 2..sshディレクトリにてssh-keygenを実行しキーペアを作成 3.サーバーOS(centOS)の一般ユーザー(vpsuser)のホームディレクトリに.sshディレクトリを作成 4.クライアントOSからscpでid_rsa.pubをauthorized_keysという名前でサーバーOS側の.sshに転送 5.必要なpermissionの設定をchmodで全て完了 という流れです!
yukky1201

2020/05/01 07:44

必要なpermissionの設定が誤っているのではないですか、現状の値を提示ください。 ・ディレクトリの所有者・権限 ・ファイルの所有者・権限
ryu2142

2020/05/01 07:55

/home/vpsuser/.sshのpermissionに設定漏れがありました、、。 確認不足でした、、。申し訳ありません。 どうもありがとうございました!!
guest

0

ベストアンサー

debug1: Offering public key: /Users/me/.ssh/id_rsa RSA SHA256:UBU5Zm+lmOkiTOCYv4QxjDiZzkFmmVKLIvM6g9i3jVo explicit

debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.

このログが出ている時点で、「この公開鍵でいけるかサーバに打診した、しかし拒否された」ということが分かります。しかし、それ以上はサーバ側でデバッグログを出さないと分かりません。
※ authorized_keysファイルのファイル名や内容がまずいのか、パーミッションに不備があるのか。判断ができない。

ちょうど先日以下のようなまとめを作っていますが、パスワード認証を有効にしておけば、公開鍵認証が失敗した原因がパーミッション不備なら、パスワード認証に通った直後にクライアント側で分かります。それを試した方がいいような気がします。
https://togetter.com/li/1501487

あともう一つの注意点は、念のためですが、クライアント側のid_rsaid_rsa.pubが対になっていることも確認した方がいいです。
コマンドとしては、ssh-keygen -y -f ~/.ssh/id_rsa を実行して、id_rsa.pubファイルと内容が一致するかを見る感じになります。
id_rsa.pubを使ってサーバに打診して、O.K.ならid_rsaを使った署名を送る、という流れなので

投稿2020/05/01 05:00

angel_p_57

総合スコア1672

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ryu2142

2020/05/01 05:56

permission不備の確認のため、パスワード認証を有効にしssh -vでのログイン実行しました。 追記のように表示されました。 また、ssh-keygen -y -f ~/.ssh/id_rsa を実行して、id_rsa.pubファイルと内容が一致するかを確認しましたが、きちんと一致しておりました。 Match条件について確認しましたが、sshd_configを見る限りそれでは無さそうです、、。
doda

2020/05/01 07:35

> debug1: Remote: Ignored authorized keys: bad ownership or modes for directory /home/vpsuser/.ssh /home/vpsuser/.ssh の所有者かpermissionがおかしいと出ていますね。 /home/vspuser/.ssh の所有者が自分になっているか、またgroupやotherに書き込み権が付いていないかを確認して、問題があったら修正してください。
ryu2142

2020/05/01 07:53

/home/vpsuser/.sshのpermissionに設定漏れがありました、、。 どうもありがとうございました!!
guest

0

あとはレアケースだと思いますが、サーバ設定ファイル sshd_config で Match条件を書いていてその内容に不備があった場合。表面上問題がないように見えても認証が弾かれるトラブルを引き起こすこともあります。
念のため、sshd_config に施した設定変更も吟味した方がよいと思います。
https://qiita.com/angel_p_57/items/b9625102defd375f4f09

投稿2020/05/01 05:08

angel_p_57

総合スコア1672

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問