LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

【Mac】AWSのSSHのアクセス方法

Last updated at Posted at 2015-04-08

◆必要なもの
 ・ターミナル
 ・AWSでダウンロードした.pem [秘密鍵のフルパス]ここでは aws_ec2.pem
 ・AWSの接続先IPアドレス [Public IP]
 ・AWSのユーザ名 [ec2-user]

◆手順
 sshの秘密鍵は一般的にユーザのホームディレクトリ配下の .ssh ディレクトリに保存します。
 AWSでダウンロードした.pem(秘密鍵)の権限を所有者の読み取り専用に変更します。
 SSHの接続方法
 ssh -i ~/.ssh/AWSでダウンロードした.pem ec2-user@Public IP

1.ターミナルを起動します。

/アプリケーション/ユーティリティ/ターミナル

2.AWSでダウンロードした.pem(秘密鍵)を確認します。

コマンド1
ls -al ~/Downloads/aws_ec2.pem
コマンド1の結果
$ ls -al ~/Downloads/aws_ec2.pem 
-rw-r-----@ 1 midori  staff  1692 11  2 20:16 /Users/midori/Downloads/aws_ec2.pem

3.AWSでダウンロードした.pem(秘密鍵)をホームディレクトリの .ssh ディレクトリに保存します。

sshディレクトリ作成(ない場合)
 ホームディレクトリに移動します。

コマンド2
cd ~/

 .ssh ディレクトリを作ります。

コマンド3
mkdir .ssh

4.AWSでダウンロードした.pem(秘密鍵)をコピーします。

AWSでダウンロードした.pem(秘密鍵)のフルパスを指定して、.ssh ディレクトリの下へコピーします。

コマンド4
cp ~/Downloads/aws_ec2.pem ~/.ssh/aws_ec2.pem

5.AWSでダウンロードした.pem(秘密鍵)の権限を所有者の読み取り専用に変更します。

コマンド5
chmod 600 ~/.ssh/aws_ec2.pem
コマンド6
ls -al ~/.ssh/aws_ec2.pem
コマンド6の結果
$ ls -al ~/.ssh/aws_ec2.pem
-rw-------@ 1 midori  staff  1692 11  2 20:16 aws_ec2.pem

6.SSHにアクセス

接続情報は 秘密鍵のパス ユーザ名@接続先IPアドレス[Public IP]
下記が表示されてアクセス成功!

コマンド7
ssh -i aws_ec2.pem ec2-user@xx.xx.xx.xxx
#xx.xx.xx.xxx はAWSの接続先IPアドレス[Public IP]に読み替えてください。
コマンド7の結果
$ ssh -i aws_ec2.pem ec2-user@xx.xx.xx.xxx
The authenticity of host 'xx.xx.xx.xxx (xx.xx.xx.xxx)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:....
Are you sure you want to continue connecting (yes/no)?

yes/no? と聞かれた場合は yes と入力してエンターを押します。
(2回目からは表示されません。)

コマンド結果
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xx.xx.xx.xxx' (RSA) to the list of known hosts.

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

https://aws.amazon.com/amazon-linux-ami/2014.09-release-notes/
No packages needed for security; 7 packages available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-xxx-xx-xx-xxx ~]$ 

アクセスできました!

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