DRYな備忘録

Don't Repeat Yourself.

EC2インスタンスにsshが接続できない原因のまとめ

目次(随時追加)

  1. InternetGatewayがVPCに未追加
  2. Pemファイルのパーミッションが不適切
  3. sshを試みるUserが違う

当該SubnetのRouteTableに当該InternetGatewayが追加されていない: Operation timed out

問題

  • sshコマンドを打ってもOperationTimeoutになる
    • TCP的に到達していない感じ

原因

解決

  1. EC2インスタンス一覧
  2. 当該インスタンスSubnet IDをメモる
  3. VPC→Subnets→当該SubnetのRoute TableのリンクをクリックしてRoute Table設定へ行く
  4. 当該RouteTableのRoutesでEdit
  5. (便宜上とりあえず)Destination0.0.0.0/0として当該VPCにattachされているInternetGatewayを追加

ダウンロードしたKeyPairファイル(.pem)のアクセス権限: Permission denied (publickey).

問題

  • sshがネットワーク的に到達しているが以下のようなエラーが出る
% ssh -i /tmp/my-project.pem ec2-user@X.X.X.X
Permissions 0644 for '/tmp/my-project.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/tmp/my-project.pem": bad permissions
Permission denied (publickey).

原因

  • 最後の一文だけ見て焦るけど、おちついて全文読みましょう
  • pemファイルのファイルアクセス権限の問題なので、chmodの話

解決

% chmod 600 /tmp/my-project.pem

ローカルのデフォルトユーザでsshを試みている: Permission denied (publickey).

問題

Host my-test
    HostName 123.123.123.123
    IdentityFile ~/.ssh/tmp/my-test.pem

として、sshを試みるも、

% ssh my-test
Permission denied (publickey).

となる。

原因

  • 自動生成されるidentityは、ec2-user名義のpemであるので、Userを指定する必要がある。

解決

% ssh ec2-user@my-test

あるいは、~/.ssh/configにおいて、

Host my-test
    HostName 123.123.123.123
+    User ec2-user
    IdentityFile ~/.ssh/tmp/my-test.pem

とする。

雑感

  • 何事も、慣れだ

Amazon Web Services実践入門 (WEB+DB PRESS plus)

Amazon Web Services実践入門 (WEB+DB PRESS plus)