網頁

2021/11/1

AWS EC2 Mac使用SSH連線到EC2 instance

AWS Mac使用SSH連線到EC2 instance。


本篇連線到「AWS EC2 啟用instance範例」啟用的Linux 2 instance。

Mac SSH連線到EC2 instance需要以下資料:

  1. key pair .pem檔 - 建立instance時產生並下載
  2. instance的使用者名稱 - Linux AMI預設為ec2-user
  3. instance的Public IPv4 DNS名稱 - AWS EC2 instance頁面
  4. instance的Public IPv4 address - AWS EC2 instance頁面


準備key-pair.pem

啟用EC2 instance的過程中把產生新的key pair檔ec2-demo.gem放到特定資料夾,例如這邊放在~/Documents/secret

在終端機(termimal)把工作目錄移到~/Documents/secret,然後輸入chmod 400 ec2-demo.pem限縮此key pair金鑰檔的權限。

~/Documents/secret$ chmod 400 ec2-demo.pem

若沒修改key pair檔案權限可能會出現UNPROTECTED PRIVATE KEY FILE錯誤。

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'ec2-demo.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 "ec2-demo.pem": bad permissions


Security Group設定

在連線的EC2 instance的Security Group新增允許本機IP的SSH連線的inbound rules


連線

輸入ssh -i <key-pair.gem> <username>@<public_ipv4_address>SSH連線到EC2 Linux instance。

  • <key-pair.gem> - 指向key-pair檔的路徑。範例工作目錄在~/Documents/secret所以是ec2-demo.pem
  • <username> - Linux 2 AMI預設為ec2-user
  • <public_ipv4_address> - EC2 instance的Public IPv4 address(或是Public IPv4 DNS)。可在EC2 instance摘要資訊找到IP位址(注意IP位址是變動IP)。

執行出現EC2的ASCII圖案代表SSH連線成功。

以Public IPv4 address位置連線輸入ssh -i ec2-demo.pem ec2-user@52.197.65.174

~/Documents/secret$ ssh -i ec2-demo.pem ec2-user@52.197.65.174
Last login: Mon Nov  1 13:47:29 2021 from ec2-3-112-23-2.ap-northeast-1.compute.amazonaws.com

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

https://aws.amazon.com/amazon-linux-2/
3 package(s) needed for security, out of 15 available
Run "sudo yum update" to apply all updates.
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[ec2-user@ip-172-31-21-26 ~]$

以Public IPv4 DNS位置連線輸入ssh -i ec2-demo.pem ec2-user@ec2-52-197-65-174.ap-northeast-1.compute.amazonaws.com

~/Documents/secret$ ssh -i ec2-demo.pem ec2-user@ec2-52-197-65-174.ap-northeast-1.compute.amazonaws.com
Last login: Mon Nov  1 13:47:29 2021 from ec2-3-112-23-2.ap-northeast-1.compute.amazonaws.com

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

https://aws.amazon.com/amazon-linux-2/
3 package(s) needed for security, out of 15 available
Run "sudo yum update" to apply all updates.
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[ec2-user@ip-172-31-21-26 ~]$

輸入pwd顯示所在目錄路徑,預設為/home/ec-user

[ec2-user@ip-172-31-21-26 ~]$ pwd
/home/ec2-user


沒有留言:

張貼留言