LoginSignup
1
2

More than 3 years have passed since last update.

【AWS】MacからEC2インスタンス(AmazonLinux)へSSHする方法

Posted at

はじめに

MacからEC2インスタンス(AmazonLinux)へSSHする方法をアウトプットします。
※Windowsとは接続方法が違います。

自宅環境

項目 説明
自宅PC  MacOS Catalina バージョン10.15.5
ターミナル Macのターミナル

基盤環境

項目 説明
OS Amazon Linux 2 AMI (HVM), SSD Volume Type
Size t2.micro

前提

  • EC2インスタンス作成時、下記画面にて「新しいキーペアの作成」を実施している。
  • キーペアをダウンロード済である。

スクリーンショット 2020-07-01 21.03.18.png

手順

①ターミナルを起動

下記のどちらかの方法で起動

  • タスクバー
    スクリーンショット 2020-07-01 21.11.27.png

  • Launchpad(その他の中)
    スクリーンショット 2020-07-01 21.14.46.png

②キーペアが保存されているディレクトリに移動

cd キーペアが保存されているディレクトリ

※ターミナルにcdと打ち込んだ後に、保存ディレクトリをターミナルへドラックすると楽です。
(個人情報は伏せています。)

スクリーンショット 2020-07-01 21.20.55.png

③EC2インスタンスへSSH接続

sudo ssh -i キーペア ec2-user@グローバルIPアドレス

実行例

satton@sattonMBP AWS_SSH % sudo ssh -i testkey.pem ec2-user@3.112.4.99
The authenticity of host '3.112.4.99 (3.112.4.99)' can't be established.
ECDSA key fingerprint is SHA256:dTS2Al40C6Ewa/hi//jd50WDbbAUVJsGnrC6Y0z/6x8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '3.112.4.99' (ECDSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
13 package(s) needed for security, out of 22 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-10-0-0-124 ~]$ 

※キーペアの絶対パス指定でも可能

satton@sattonMBP ~ % sudo ssh -i /Users/satton/Desktop/AWS_SSH/testkey.pem ec2-user@3.112.4.99
Last login: Wed Jul  1 12:44:28 2020 from om126204165127.6.openmobile.ne.jp

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
13 package(s) needed for security, out of 22 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-10-0-0-124 ~]$ 


sudoを付けない場合

ファイルの権限の関係でSSHログインができません。

satton@sattonMBP ~ % ssh -i /Users/satton/Desktop/AWS_SSH/testkey.pem ec2-user@3.112.4.99
The authenticity of host '3.112.4.99 (3.112.4.99)' can't be established.
ECDSA key fingerprint is SHA256:dTS2Al40C6Ewa/hi//jd50WDbbAUVJsGnrC6Y0z/6x8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Failed to add the host to the list of known hosts (/Users/satton/.ssh/known_hosts).
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/satton/Desktop/AWS_SSH/testkey.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 "/Users/satton/Desktop/AWS_SSH/testkey.pem": bad permissions
ec2-user@3.112.4.99: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
satton@sattonMBP ~ % 

EC2のグローバルIPアドレスってどこ?

ここの部分になります。
(AWSマネジメントコンソール→EC2→インスタンス一覧)

スクリーンショット 2020-07-01 21.49.57.png

参考

AWSのEC2にmacからSSHする方法

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2