前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Use ssh to connect to AWS EC2 instance

Use ssh to connect to AWS EC2 instance

作者头像
alanzeng
发布于 2025-01-14 13:44:51
发布于 2025-01-14 13:44:51
6900
代码可运行
举报
文章被收录于专栏:alanzeng423alanzeng423
运行总次数:0
代码可运行

使用ssh连接AWS EC2实例

1: Create a new key pair

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. In the navigation pane, choose Key Pairs.
  3. Choose Create key pair.
  4. For Key pair name, enter a name for the new key pair, and then choose Create key pair.
  5. Your browser will download the private key file automatically. The private key file is automatically downloaded by your browser. The base file name is the name you specified as the name of your key pair, and the file name extension is .pem. Save the private key file in a safe place.
  6. Choose Close.
  7. Important: You can create a key pair only once. Be sure to save the private key file to your computer. You’ll need to provide the name of your key pair when you launch an instance and the corresponding private key each time you connect to the instance.

2: Connect to your instance

  1. Open a terminal window.
  2. Use the cd command to navigate to the directory where your private key file is located.
  3. Use the following command to set the permissions of your private key file so that only you can read it:
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
chmod 400 /path/my-key-pair.pem
  1. Use the following command to connect to your instance. Replace ec2-user with the appropriate user name for your AMI.
  • For Amazon Linux 2 or the Amazon Linux AMI, the user name is ec2-user.
  • For a CentOS AMI, the user name is centos.
  • For a Debian AMI, the user name is admin.
  • For a Fedora AMI, the user name is ec2-user or fedora.
  • For a RHEL AMI, the user name is ec2-user or root.
  • For a SUSE AMI, the user name is ec2-user or root.
  • For an Ubuntu AMI, the user name is ubuntu. Here my OS is Ubuntu.
  • Otherwise, if ec2-user and root don’t work, check with your AMI provider.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ssh -i /path/my-key-pair.pem user_name@ip_address

3: (Optional) Connect to your instance using a password

Open a terminal window.

Use the following command to connect to your instance.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ssh user_name@my-instance-ip
I meet Permission denied (publickey) error.

To fix it.

Step1: Firstly I need to use the previous command to connect to my instance.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ssh -i /path/my-key-pair.pem user_name@ip_address
Step2: Set up a password for the user using passwd command along with the username.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
sudo passwd ubuntu
Step 3: Edit sshd_config file.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
sudo vim /etc/ssh/sshd_config

Find the Line containing PasswordAuthentication parameter and change its value from no to yes.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
PasswordAuthentication yes

If you want to set up root login, find PermitRootLogin parameter and change its value from prohibit-password to yes

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
PermitRootLogin yes

After this changes save file and exit.

Step 4: Restart the SSH service.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
service ssh restart ## for ubuntu

service sshd restart ## for centos
Step 5: Add the .pem file to Local ssh.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
chmod 400 /path/my-key-pair.pem
ssh-add -k /path/my-key-pair.pem
Step 6: Now you can connect to your instance using a password.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ssh user_name@my-instance-ip

文章作者: Alan Zeng

原始链接: https://alanzeng.com/blogs/16582/

版权说明:本博客所有文章除特别声明外,均采用BY-NC-SA 4.0许可协议。获得许可后,要求转载时注明文章出处和网站链接,谢谢!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-08-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1: Create a new key pair
  • 2: Connect to your instance
  • 3: (Optional) Connect to your instance using a password
    • I meet Permission denied (publickey) error.
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档