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

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

新規登録して質問してみよう
ただいま回答率
85.50%
SSH

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

ログイン

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

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

3回答

2055閲覧

EC2にログインしたい ssh

NEMO2

総合スコア6

SSH

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

ログイン

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

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

1クリップ

投稿2020/03/23 12:37

編集2022/01/12 10:55

以前作ったものを同じPCではあるのですが一度決してしまっており、gitからcloneしたものをデプロイしようとしてEC2にログインしようといつもどうりsshで起動しようとすると

ユーザー$ ssh -i keyname ec2-user@<ipアドレス> Permission denied (publickey)

という状態で弾かれてしまい、ずっとこのエラーで止まってます
WARNING: UNPROTECTED PRIVATE KEY FILE! ではないので誰でも編集可能な状態ではないのですが
、色々と調べて
ec2-userの部分が変わっていたのかと思い別の admin,root,centosなども入れてみましたが、変わりありませんでした
一応当時のインタンスIDとIPアドレスは一旦解放して、新規で作成しました

(base) username:.ssh username$ ssh -vT git@github.com OpenSSH_7.9p1, LibreSSL 2.7.3 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 48: Applying options for * debug1: Connecting to github.com port 22. debug1: Connection established. debug1: identity file /Users/username/.ssh/id_rsa type 0 debug1: identity file /Users/username/.ssh/id_rsa-cert type -1 debug1: identity file /Users/username/.ssh/id_dsa type -1 debug1: identity file /Users/username/.ssh/id_dsa-cert type -1 debug1: identity file /Users/username/.ssh/id_ecdsa type -1 debug1: identity file /Users/username/.ssh/id_ecdsa-cert type -1 debug1: identity file /Users/username/.ssh/id_ed25519 type -1 debug1: identity file /Users/username/.ssh/id_ed25519-cert type -1 debug1: identity file /Users/username/.ssh/id_xmss type -1 debug1: identity file /Users/username/.ssh/id_xmss-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_7.9 debug1: Remote protocol version 2.0, remote software version babeld-d48c3acd debug1: no match: babeld-d48c3acd debug1: Authenticating to github.com:22 as 'git' debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: rsa-sha2-512 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: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8 debug1: Host 'github.com' is known and matches the RSA host key. debug1: Found key in /Users/username/.ssh/known_hosts:1 debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: rekey after 134217728 blocks debug1: Will attempt key: /Users/username/.ssh/id_rsa RSA SHA256:EEPRVYIS243vMV3TcxeHEfBMlwf52L3VJYlE9ioAF4I agent debug1: Will attempt key: /Users/username/.ssh/id_dsa debug1: Will attempt key: /Users/username/.ssh/id_ecdsa debug1: Will attempt key: /Users/username/.ssh/id_ed25519 debug1: Will attempt key: /Users/username/.ssh/id_xmss debug1: SSH2_MSG_EXT_INFO received debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-dss> debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Offering public key: /Users/username/.ssh/id_rsa RSA SHA256:EEPRVYIS243vMV3TcxeHEfBMlwf52L3VJYlE9ioAF4I agent debug1: Server accepts key: /Users/username/.ssh/id_rsa RSA SHA256:EEPRVYIS243vMV3TcxeHEfBMlwf52L3VJYlE9ioAF4I agent debug1: Authentication succeeded (publickey). Authenticated to github.com ([52.69.186.44]:22). debug1: channel 0: new [client-session] debug1: Entering interactive session. debug1: pledge: network debug1: Sending environment. debug1: Sending env LANG = ja_JP.UTF-8 debug1: client_input_channel_req: channel 0 rtype exit-status reply 0 Hi GITHUB名! You've successfully authenticated, but GitHub does not provide shell access. debug1: channel 0: free: client-session, nchannels 1 Transferred: sent 3452, received 2484 bytes, in 0.4 seconds Bytes per second: sent 8077.7, received 5812.6 debug1: Exit status 1

とsshがあることは
debug1: Connecting to github.com port 22.
debug1: Connection established.
でわかるのですが、それが承認されていません

lsの中身も

設定してある.pem id_rsa id_rsa.pub known_hosts

で一応あります

https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html#TroubleshootingInstancesConnectingSSH
↑参考にしたAWSサイト
AWSの公式のとうりにインスタンス停止後、IPアドレス確認、インスタンスID確認、デタッチ、バージョン変更、アタッチ、インスタンス開始、で確認はしましたが、変わりませんでした

最終的にはポートフォリオ用としてデプロイをしたいので助けてもらいたいです。

追記 3/24 sshの versionは OpenSSH_7.9p1, LibreSSL 2.7.3 です

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

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

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

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

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

guest

回答3

0

ec2-userを使用しているということは、アクセスしたいEC2インスタンスがAmazon Linux/Amazon Linux2であるということでしょうか。
そして、元々そのユーザと鍵ではSSHでログインできていたのでしょうか?

もし他のOSであればデフォルトのユーザ名が異なるので個別に確認してください。
また、デフォルトのユーザに設定されているキーペアはEC2のコンソールで、該当のインスタンスのキーペアの項目で確認できるので、きちんと一致した鍵を使っているかを確認してください(手動で設定した鍵についてはその限りではありません)

また、念の為ですが、鍵が配置されているディレクトリと鍵そのもののパーミッションに問題ないか確認してみてください

※追記
SSHログインできない問題を解消するものではないですが、EC2インスタンスにセッションマネージャで入り、そこでgit cloneする、という方法で一応目的は達成できるかと思います。

投稿2020/03/24 02:30

編集2020/03/24 04:27
yu_1985

総合スコア7427

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

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

NEMO2

2020/03/24 03:44

ec2インスタンスはAmazon Linux/Amazon Linux2で間違いありません、 今回のものはclone前のものでは入れていましたが、clone後のものではまだ入っていないです。
Takumiboo

2020/03/24 03:46

確かにローカル側鍵ファイル周りのパーミッションの可能性もありましたね…。
Takumiboo

2020/03/24 03:48

ああ、でもGithub.comへのアクセスは出来てるから、ディレクトリは問題ないんですかね。
yu_1985

2020/03/24 04:10 編集

キーペアは正しかったですか?そこを確認してみてください。 > 今回のものはclone前のものでは入れていましたが、clone後のものではまだ入っていないです。 いえ、ソースは関係がありません。 今回使用している鍵で該当のEC2インスタンスに入ったことがありますか、ということです。
yu_1985

2020/03/24 04:24

失礼、確認済みでしたね > インスタンスのキーペア名も確認しましたがssh内の名前と一致していて
NEMO2

2020/03/25 18:31

一応 sshのリスト内のキーの中身をみたのですが ``` base) usernamenoMacBook-Air:.ssh username$ ssh -v -i free_nemo ec2-user@18.178.170.159 Warning: Identity file free_nemo not accessible: No such file or directory. OpenSSH_7.9p1, LibreSSL 2.7.3 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 48: Applying options for * debug1: Connecting to 18.178.170.159 [18.178.170.159] port 22. debug1: Connection established. debug1: identity file /Users/username/.ssh/id_rsa type 0 debug1: identity file /Users/username/.ssh/id_rsa-cert type -1 debug1: identity file /Users/username/.ssh/id_dsa type -1 debug1: identity file /Users/username/.ssh/id_dsa-cert type -1 debug1: identity file /Users/username/.ssh/id_ecdsa type -1 debug1: identity file /Users/username/.ssh/id_ecdsa-cert type -1 debug1: identity file /Users/username/.ssh/id_ed25519 type -1 debug1: identity file /Users/username/.ssh/id_ed25519-cert type -1 debug1: identity file /Users/username/.ssh/id_xmss type -1 debug1: identity file /Users/username/.ssh/id_xmss-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_7.9 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 18.178.170.159:22 as 'ec2-user' 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:4AfTTRoON/Qf51w8RP0V3d+FLleysqu5450sbOCUrws debug1: Host '18.178.170.159' is known and matches the ECDSA host key. debug1: Found key in /Users/username/.ssh/known_hosts:4 debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: rekey after 134217728 blocks debug1: Will attempt key: /Users/username/.ssh/id_rsa RSA SHA256:EEPRVYIS243vMV3TcxeHEfBMlwf52L3VJYlE9ioAF4I agent debug1: Will attempt key: /Users/username/.ssh/id_dsa debug1: Will attempt key: /Users/username/.ssh/id_ecdsa debug1: Will attempt key: /Users/username/.ssh/id_ed25519 debug1: Will attempt key: /Users/username/.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 debug1: Next authentication method: publickey debug1: Offering public key: /Users/username/.ssh/id_rsa RSA SHA256:EEPRVYIS243vMV3TcxeHEfBMlwf52L3VJYlE9ioAF4I agent debug1: Authentications that can continue: publickey debug1: Trying private key: /Users/username/.ssh/id_dsa debug1: Trying private key: /Users/username/.ssh/id_ecdsa debug1: Trying private key: /Users/username/.ssh/id_ed25519 debug1: Trying private key: /Users/username/.ssh/id_xmss debug1: No more authentication methods to try. ec2-user@18.178.170.159: Permission denied (publickey). ``` となっており 同じリストないの秘密鍵のlsの中身も ``` (base) usernamenoMacBook-Air:.ssh username$ ssh -v -i id_rsa ec2-user@18.178.170.159 OpenSSH_7.9p1, LibreSSL 2.7.3 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 48: Applying options for * debug1: Connecting to 18.178.170.159 [18.178.170.159] port 22. debug1: Connection established. debug1: identity file id_rsa type 0 debug1: identity file id_rsa-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_7.9 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 18.178.170.159:22 as 'ec2-user' 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:4AfTTRoON/Qf51w8RP0V3d+FLleysqu5450sbOCUrws debug1: Host '18.178.170.159' is known and matches the ECDSA host key. debug1: Found key in /Users/username/.ssh/known_hosts:4 debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: rekey after 134217728 blocks debug1: Will attempt key: id_rsa RSA SHA256:EEPRVYIS243vMV3TcxeHEfBMlwf52L3VJYlE9ioAF4I explicit agent 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: id_rsa RSA SHA256:EEPRVYIS243vMV3TcxeHEfBMlwf52L3VJYlE9ioAF4I explicit agent debug1: Authentications that can continue: publickey debug1: No more authentication methods to try. ec2-user@18.178.170.159: Permission denied (publickey). ``` となっており SSH2_MSG_NEWKEYS SSH2_MSG_KEXINIT SSH2_MSG_SERVICE_ACCEPT Authentications that can continue: publickey もともとデプロイの設定は別の方がやっており、今はもう連絡が取れない状態です デプロイする予定だったファイルの中身も ```aws: # access_key_id: 123 # secret_access_key: 345 # Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies. secret_key_base: 色々すでに記入されていたもの payjp: public_key: pk_test_71652c9d19ffd414661cd4d8 private_key: sk_test_cc5d4836d7164da4ecb18f04 db: production: username: root password: aaaa_sample_userName staging: username: root password: stg_aaaa_sample_userName basic: user: userName password: aaaa(仮)``` の様な感じで自分がsshで設定しているpasswordとは違うものが入っている状態です どこから手をつければ良いのか分からない状況で、Qiitaなどでみましたが、新規からのものがほぼ全てで、すでに情報があってしまうものの変更方法があまり乗っていなかったので、 今までの質問の中でもありました、ターミナル側とAWSのサーバー側のキーが一致しているかの確認手順だけでも知りたいです
yu_1985

2020/03/26 02:06

> 自分がsshで設定しているpassword これはSSHの認証時にパスワード(パスフレーズ?)を使用しているということですか? そこに記載されているパスワードはSSHとは関係ないとは思いますが…。 デプロイされているファイルの設定内容が認識と異なるのは確かに問題ですが、それはSSHできないのとはまた別の問題なので切り分けましょう。 回答に追記済みですが、目的を果たしたいのならセッションマネージャで該当のEC2インスタンスに入ってgit clone(もしくはgit pull?)をすればいいです。 そして > ssh -v -i id_rsa この指定の仕方だと鍵を置いてある場所ではなく自分のホームディレクトリを指定していませんか?
Takumiboo

2020/03/26 06:15

> yu_1985さん EC2について不勉強で申し訳ないのですが、「セッションマネージャ」というのはインスタンスのシェルに入れるということになるのでしょうか? であれば、例えばクライアント側で新たに認証鍵ペアを作成して、公開鍵を登録するということも可能なんですかね?
yu_1985

2020/03/26 06:31

その認識で正しいです。 詳しくは回答に追記したリンクをご参照ください。 IAMロールを適切に設定する必要がありますが、ちゃんと設定してあれば今はEC2のコンソールから接続ボタンを押すとセッションマネージャが選択肢に出てきます。
guest

0

キーペアは合っているのでしょうか。

ssh -i keyname ec2-user@<ipアドレス>

のkeynameで指定しているキーファイルが正しいことは確認できていますか?

投稿2020/03/23 12:58

Takumiboo

総合スコア2534

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

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

Takumiboo

2020/03/23 12:58

あ、かぶりましたね、すみません
NEMO2

2020/03/23 20:42

はい、AWSのインスタンスのキーペア名: abce(仮)と ターミナルの ``` (base) usernameMacBook-Air:.ssh username$ ls abce(仮}.pem id_rsa id_rsa.pub known_hosts ``` で一致しています 同じものじゃない場合publicyエラーの前に ``` Warning: Identity file キーペア名 not accessible: No such file or directory. ``` が出ています
Takumiboo

2020/03/24 00:23

ファイル名は別に関係ない(ファイル名が同じでも中身が違う可能性があり、ファイル名が違っても中身が同じである可能性がある)ので、そこで判断しないでください。 「No such file or directory.」はファイルが見つからないというエラーなので、そもそもキー指定のファイルパスが間違っています。
NEMO2

2020/03/24 00:54

具体的に一致しているかの確認方法などありますでしょうか。
guest

0

Permission denied (publickey)

接続鍵が違う場合に出るエラーです
そこらへん確認してみては

投稿2020/03/23 12:56

y_waiwai

総合スコア87719

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

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

NEMO2

2020/03/23 20:53

ssh -i キーペア名pem ec2-user@IP で入力しているのですが -i の後に/home/path/なども追記しても Warning: Identity file キーペア名 not accessible: No such file or directory. が追加されるのみで 追記しない場合は Permission denied (publickey) だけ表示されています、接続鍵が違うというので、インスタンスのキーペア名も確認しましたがssh内の名前と一致していて、それ以外の別のキーペア名も消している状態です。 AWS内にもないです。 接続鍵が違いというのはターミナルではなくAWS側での確認でしょうか?
y_waiwai

2020/03/23 22:02

ターミナル側とAWS側の接続鍵が食い違っている、ということです。
heri3x

2020/03/24 03:50 編集

ディレクトリとファイル権限の問題かも。下を試してみては $ chmod 700 /home/path/ $ chmod 600 /home/path/キーペア名
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問