博客建站4 - ssh远程连接服务器

1. 什么是SSH?

SSH(Secure Shell)是一种加密的网络协议,用于在不安全的网络中安全地远程登录到其他计算机系统。它提供了对远程服务器的安全访问,并支持加密数据传输。SSH协议通过使用公钥加密技术来确保通信的安全性,防止中间人攻击和数据泄露。

{% asset_img 2024-07-17-12-13-15.png ssh_connet %}

2. 下载shh客户端

shh是一个远程连接的软件,有服务端server和客户端client两部分组成。因为我们是要连接远端服务器,所以只需要client。

Ubuntu和macOS应该会默认安装ssh,当然也可以使用第三方的SSH客户端,如OpenSSH 或 PuTTY。

检查是否支持ssh指令

ssh -V

如果输出类似以下的版本号,则说明支持ssh指令。

OpenSSH_9.6p1, LibreSSL 3.3.6

如果提示找不到ssh命令,则需要安装ssh客户端。

Ubuntu:

sudo apt-get install openssh-client

macOS:

brew install openssh

Windows:

下载并安装putty

3. 配置ssh密钥

如果你买的是阿里云的ECS服务器,且是第一次连接服务器,需要重置远程连接的密码:

云服务器 ECS -> 实例与镜像 -> 实例 -> 实例详情 -> 基本信息 -> 重置密码

输入“新密码”和“确认密码”,然后点击确认修改。

4. 连接服务器

ssh username@hostname
# hostname: 远程服务器的IP地址或域名
# username: 远端服务器的登录用户名

5. 常见问题

5.1. IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY

登录时类似如下报错:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ED25519 key sent by the remote host is
SHA256:PeAn4uPscmMHuETVY27M0HCMoE3LAGLVGowJN9/nJoo.
Please contact your system administrator.
Add correct host key in /Users/spencer/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/spencer/.ssh/known_hosts:5
Host key for 120.xxx.xxx.xxx has changed and you have requested strict checking.
Host key verification failed.

警告:远程主机标识已更改!

这个报错主要是因为远程主机的ssh公钥发生了变化,两边不一致导致的

删除本地对应ip的在known_hosts相关信息

known_hosts文件路径: ~/.ssh/known_hosts

可以直接删除此文件(删除之前最好先拷贝一个备份)。

删除后再次登录,应该就正常了。

posted @ 2024-07-22 21:46  陌尘(MoChen)  阅读(7)  评论(0编辑  收藏  举报