如何在EC2上SSH到Ubuntu服务器

378 阅读6分钟

连接到云中的一个实例可能有点棘手。然而,能够通过SSH进入你的运行机器是必须的,因为你需要做一些配置、维护和调试。在这篇文章中,我将向你展示如何从命令行连接到你基于Ubuntu的EC2实例。在Windows上,我们可以使用Git Bash或Cygwin这样的工具来获得类似Unix的环境。在Mac和Linux上,你可以使用内置的终端,没有什么问题。

什么是SSH?

SSH是Secure Shell或Secure Socket Shell的缩写,它是一种网络协议,为系统管理员提供了一种高度安全的方法来通过各种类型的计算机网络访问计算机。其中一些网络可能不完全安全,所以需要像安全套接字壳这样的解决方案。SSH也指实现SSH协议的工具套件。

SSH密钥对

成功的SSH通信的关键是密钥对。我们在创建Ubuntu服务器的教程中看到了如何生成一个密钥对。这将公钥放在服务器上,而私钥被下载到你的本地机器上。确保你保持这个密钥的安全性和私密性,以确保你在云中运行的实例的安全性

储存私有.pem文件

当你创建密钥对时,从亚马逊网络服务下载的文件是PEM格式的。PEM文件由证书容器连接在一起,当形成完整链条的多个证书作为一个文件被导入时,证书容器通常用于证书安装。PEM文件在RFCs 1421到1424中被定义为标准。

将我们命名为 "lemp.pem "的私有文件放在我们选择的目录中是非常合理的,在我们的例子中,这个目录被简单命名为 "aws"。好了,Ubuntu实例正在运行,你也有了你的私钥文件,现在怎么办?好吧,接下来让我们点击AWS仪表板上的实例,我们会看到一个 "连接 "的选项。

当你点击这个选项时,你会得到一些很棒的选项来连接到你正在运行的Ubuntu服务器。注意,EC2实例连接只适用于官方的亚马逊AMI图像,所以这不是一个选项。默认情况下,你有SSH客户端,这就是我们现在要做的选择。

你可能会注意到使用公共DNS连接到你的实例的选项,例如在我们的例子中,ec2-54-165-120-73.compute-1.amazonaws.com。

我们可以马上试试,你会得到一些反馈,比如"

主机'ec2-54-165-120-73.computing-1.amazonaws.com (54.165.120.73)'的真实性不能被确定。ED25519密钥的指纹是SHA256:cqmUzwyi/Ib8AcM2g4Zoy8j3rlei8P0C6GTFxfTjyD0。
这个密钥没有任何其他名字
你确定要继续连接(是/否/[指纹])?"

我们输入 ""。输入后,我们看到一条令人震惊的信息

Warning: Permanently added ‘ec2-54-165-120-73.compute-1.amazonaws.com’ (ED25519) to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for ‘lemp.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 “lemp.pem”: bad permissions
ubuntu@ec2-54-165-120-73.compute-1.amazonaws.com: Permission denied (publickey).

Chmod 400

这就是为什么你必须一步一步地按照指示操作!下面是完整的步骤。

  1. 打开一个SSH客户端。
  2. 找到你的私钥文件。用来启动这个实例的密钥是lemp.pem
  3. 运行这个命令,如果有必要,以确保你的密钥不被公开查看。
  4. chmod 400 lemp.pem
  5. 使用公共DNS连接到你的实例。
  6. ec2-54-165-120-73.compute-1.amazonaws.com

SSH连接成功

一旦我们按照上面的步骤操作,我们就会得到熟悉的Ubuntu服务器的欢迎界面,看起来可能是这样的。

Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-1011-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Tue Sep 13 21:00:02 UTC 2022

  System load:  0.0               Processes:             98
  Usage of /:   19.1% of 7.58GB   Users logged in:       0
  Memory usage: 21%               IPv4 address for eth0: 172.31.93.141
  Swap usage:   0%

0 updates can be applied immediately.


The list of available updates is more than a week old.
To check for new updates run: sudo apt update


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

登录后的Ubuntu导航

一旦你登录了,就可以很容易地使用你熟悉的Linux命令在机器上导航了。

ubuntu@ip-172-31-93-141:~$ pwd
/home/ubuntu
ubuntu@ip-172-31-93-141:~$ ls -la
total 28
drwxr-x--- 4 ubuntu ubuntu 4096 Sep 13 21:00 .
drwxr-xr-x 3 root   root   4096 Sep 13 20:38 ..
-rw-r--r-- 1 ubuntu ubuntu  220 Jan  6  2022 .bash_logout
-rw-r--r-- 1 ubuntu ubuntu 3771 Jan  6  2022 .bashrc
drwx------ 2 ubuntu ubuntu 4096 Sep 13 21:00 .cache
-rw-r--r-- 1 ubuntu ubuntu  807 Jan  6  2022 .profile
drwx------ 2 ubuntu ubuntu 4096 Sep 13 20:38 .ssh
ubuntu@ip-172-31-93-141:~$ cd /
ubuntu@ip-172-31-93-141:/$ ls -la
total 72
drwxr-xr-x  19 root root  4096 Sep 13 20:38 .
drwxr-xr-x  19 root root  4096 Sep 13 20:38 ..
lrwxrwxrwx   1 root root     7 Jun  9 11:21 bin -> usr/bin
drwxr-xr-x   4 root root  4096 Jun  9 11:25 boot
drwxr-xr-x  17 root root  3220 Sep 13 20:38 dev
drwxr-xr-x  98 root root  4096 Sep 13 20:38 etc
drwxr-xr-x   3 root root  4096 Sep 13 20:38 home
lrwxrwxrwx   1 root root     7 Jun  9 11:21 lib -> usr/lib
lrwxrwxrwx   1 root root     9 Jun  9 11:21 lib32 -> usr/lib32
lrwxrwxrwx   1 root root     9 Jun  9 11:21 lib64 -> usr/lib64
lrwxrwxrwx   1 root root    10 Jun  9 11:21 libx32 -> usr/libx32
drwx------   2 root root 16384 Jun  9 11:23 lost+found
drwxr-xr-x   2 root root  4096 Jun  9 11:21 media
drwxr-xr-x   2 root root  4096 Jun  9 11:21 mnt
drwxr-xr-x   2 root root  4096 Jun  9 11:21 opt
dr-xr-xr-x 159 root root     0 Sep 13 20:37 proc
drwx------   4 root root  4096 Sep 13 20:38 root
drwxr-xr-x  26 root root   840 Sep 13 21:00 run
lrwxrwxrwx   1 root root     8 Jun  9 11:21 sbin -> usr/sbin
drwxr-xr-x   8 root root  4096 Jun  9 11:25 snap
drwxr-xr-x   2 root root  4096 Jun  9 11:21 srv
dr-xr-xr-x  13 root root     0 Sep 13 20:37 sys
drwxrwxrwt  12 root root  4096 Sep 13 20:38 tmp
drwxr-xr-x  14 root root  4096 Jun  9 11:21 usr
drwxr-xr-x  13 root root  4096 Jun  9 11:22 var

请注意,这个特定的Ubuntu实例的所有信息现在已经被删除,因为它只是在本教程中用于演示的目的。