EC2 instance
丢失.pem 文件
丢失了用于连接 AWS EC2 实例的 .pem 文件,您无法直接恢复该文件,因为 AWS 不会保存私钥。要重新创建一个新的 .pem 文件
只能创建现有实例的映像(AMI)来生成一个新的实例,并在此过程中创建新的密钥对
建立 EC2 instance 的步驟:
-
登入 AWS Console,找到 EC2 服務
-
點擊橘色的 "Launch Instance" 按鈕
-
基本設定:
-
Name: 為您的實例取個名字
-
選擇 AMI (作業系統映像檔):
-
Amazon Linux 2023 (推薦新手使用,用這個)
-
Ubuntu
-
其他選項...
-
-
-
Instance Type 選擇:
-
t2.micro (免費層級可用)
-
或根據需求選擇其他規格
-
-
Key Pair 設定:(或引用現成的)
-
Create new key pair
-
輸入 key pair 名稱
-
選擇 RSA 和 .pem 格式
-
下載金鑰檔案並安全保存
-
-
Network Settings:
-
VPC: 選擇預設 VPC
-
Auto-assign Public IP: Enable
-
Security Group:
-
允許 SSH (Port 22)
-
如需要網頁服務,加入 HTTP (80) 和 HTTPS (443)
-
-
-
Storage 配置:
-
預設 8GB 通常足夠
-
可根據需求調整
-
-
檢查設定並點擊 "Launch Instance"
NewKeyPair
左侧菜单中点击 Key Pairs(密钥对)。 点击 Create Key Pair 按钮,填写名称并选择 pem 格式,点击 Create。 下载生成的 NewKeyPair.pem 文件并保存好。
.pem 檔案的權限設定問題
Warning: Permanently added 'ec2-3-25-51-202.ap-southeast-2.compute.amazonaws.com' (ED25519) to the list of known hosts.
Bad permissions. Try removing permissions for user: NT AUTHORITY\\Authenticated Users (S-1-5-11) on file D:/AWS/YT1/NewKeyPair.pem.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions for 'NewKeyPair.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 "NewKeyPair.pem": bad permissions
ubuntu@ec2-3-25-51-202.ap-southeast-2.compute.amazonaws.com: Permission denied (publickey).
-
右鍵點擊 NewKeyPair.pem 檔案
-
選擇 "Properties" (內容)
-
點擊 "Security" (安全性) 標籤
-
點擊 "Advanced" (進階)
-
點擊 "Disable inheritance" (停用繼承)
- 選擇 "Remove all inherited permissions" (移除所有繼承的權限)
-
點擊 "Add" (新增)
-
選擇 "Select a principal" (選擇主體)
-
輸入您的 Windows 使用者名稱
cmd輸入whoami系統會顯示您的使用者名稱
-
只給予 "Read" 和 "Read & Execute" 權限
-
-
如果覺得權限設定太麻煩
- 把 .pem 檔案移到
C:/Users/[您的使用者名稱]/.ssh/目錄下
- 把 .pem 檔案移到
連線前需要注意的事項
當 EC2 實例的 Status check 顯示 "Initializing" 時,代表實例還在初始化過程中,這時候確實可能無法通過 SSH 連接。
需要等待直到 Status check 顯示 "2/2 checks passed" 才能成功連接。
Inbound rules (入站規則)
Instances > Security Groups > Inbound rules (入站規則)
除了常见的选项如 SSH(端口 22)和 HTTP(端口 80) 对于其他服务(例如 MySQL、Redmine 等),需要选择 Custom TCP 选项来手动配置特定端口
以MySQL、Redmine為例,Add rule(添加规则)。在 Type 下拉菜单中选择 Custom TCP。 在 Port range 中输入相应的端口号,例如:对于 MySQL,输入 3306。对于 Redmine,通常使用 3000 或者根据您的配置输入相应的端口。
在 Source 中选择合适的来源:如果希望允许所有 IP 访问,可以选择 0.0.0.0/0(不推荐用于生产环境)。
如果只希望特定 IP 访问,可以选择 My IP 或者手动输入特定 IP 地址。