ESC

开始输入,可搜索发票、服务、域名、工单,以及 更多...

搜索... Ctrl+K
新手入门

如何用 SSH 登录 Linux 服务器:Windows、Mac、Linux 远程连接教程

6 个步骤 8 分钟阅读 42 次阅读 0
本文目录

SSH 是管理 Linux 服务器最常用的方式。本文介绍如何用 SSH 登录 Linux 服务器:Windows 10/11 可直接使用 PowerShell 或 Windows Terminal 自带的 ssh 命令,也可以使用 PuTTY、Xshell 等图形工具;macOS 与 Linux 直接在终端中连接。文中命令适用于 Debian、Ubuntu、CentOS、Rocky Linux、AlmaLinux 等主流发行版的 IMIDC VPS 与独立服务器。

步骤 1:准备 SSH 登录信息

登录前请确认四项信息:服务器公网 IP、SSH 端口(默认 22)、用户名(通常为 root,部分 Ubuntu 镜像为 ubuntu)以及密码或私钥。这些信息可以在开通邮件中找到,也可以登录客户中心 → 我的产品与服务 → 选择服务器 → 管理 页面查看。下文以示例 IP 203.0.113.10 演示,请替换为你自己的 IP。

IP address:  203.0.113.10
SSH port:    22
Username:    root
Password:    (from the welcome email or client area)

步骤 2:Windows 用 PowerShell / Windows Terminal 自带 ssh 登录

Windows 10 1809 及以上版本、Windows 11 已内置 OpenSSH 客户端。按 Win + X 打开“终端”或 PowerShell,执行以下命令即可连接。如果修改过 SSH 端口,用 -p 指定端口。

# Check that the built-in OpenSSH client is available
ssh -V

# Connect with the default port 22
ssh [email protected]

# Connect with a custom port, e.g. 2222
ssh -p 2222 [email protected]

如果提示“ssh 不是内部或外部命令”,说明系统未安装 OpenSSH 客户端,可用管理员身份运行 PowerShell 安装:

# Run in PowerShell as Administrator only if "ssh" is not recognized
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Client*'
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

步骤 3:使用 PuTTY 或 Xshell 图形客户端登录

PuTTY:打开后在 Host Name 中填写 IP,Port 填写端口,Connection type 选择 SSH,点击 Open,然后在“login as”处输入用户名,再输入密码。可在 Saved Sessions 中保存会话,下次双击即可连接。Xshell:新建会话,填写主机与端口,在“用户身份验证”中填写用户名与密码(或选择密钥),保存后双击连接。两者都只需要下面这些基本参数:

Host:  203.0.113.10
Port:  22
Connection type: SSH
Login as: root

步骤 4:Mac 与 Linux 终端 SSH 登录

macOS 打开“终端”(Terminal)或 iTerm2,Linux 打开任意终端,直接使用 ssh 命令。绝大多数系统已预装客户端,极少数精简系统可按发行版安装:

# macOS / Linux terminal
ssh [email protected]
ssh -p 2222 [email protected]

# Debian / Ubuntu: install the client if missing
sudo apt update && sudo apt install -y openssh-client

# CentOS / Rocky / AlmaLinux: install the client if missing
sudo dnf install -y openssh-clients

步骤 5:首次登录确认主机指纹

第一次连接某台服务器时,客户端会显示主机密钥指纹并询问是否继续。输入 yes 后指纹会保存到本地 known_hosts 文件,以后连接不再提示。对安全要求较高时,可以先通过 VNC 控制台登录服务器查看真实指纹,确认一致后再输入 yes。输入密码时屏幕不会显示任何字符,这是正常的保护机制,输完直接回车即可。

The authenticity of host '203.0.113.10 (203.0.113.10)' can't be established.
ED25519 key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

# On the server (e.g. via the VNC console) print the real fingerprint to compare
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub

步骤 6:SSH 登录常见报错与处理

报错常见原因与处理
Permission denied用户名或密码错误,或服务器已禁用密码/root 登录;核对大小写,必要时在客户中心重置密码。
Connection refused端口不对或 sshd 未运行;确认端口,或通过 VNC 控制台检查 sshd 服务。
Connection timed outIP 错误、服务器未开机、防火墙拦截或本地网络限制。
REMOTE HOST IDENTIFICATION HAS CHANGED服务器重装系统后主机密钥变化,删除本地旧记录即可。
# Remove the old host key after a reinstall, then connect again
ssh-keygen -R 203.0.113.10

# Show verbose debug output when a connection fails
ssh -v [email protected]
如果连续多次输错密码后突然无法连接,可能是服务器上的 fail2ban 等工具封禁了你的 IP,请稍后再试或通过 VNC 控制台解封。

常见问题

输入密码时为什么没有任何显示?

SSH 出于安全考虑不回显密码,也不显示星号。直接输入(或右键粘贴)后回车即可,粘贴时注意不要带上多余的空格。

SSH 连接一段时间不操作就断开怎么办?

多数是中间网络设备清理了空闲连接。可在本地 SSH 配置文件中添加心跳参数,让客户端定期发送保活包:

# ~/.ssh/config  (Windows: C:\Users\<you>\.ssh\config)
Host *
    ServerAliveInterval 30
    ServerAliveCountMax 4

Windows 自带 ssh 和 PuTTY、Xshell 哪个更好?

功能上都能满足日常管理。自带 ssh 无需安装、命令与 Mac/Linux 一致,便于配合密钥使用;PuTTY、Xshell 更适合需要保存大量会话、习惯图形界面的用户。

如按以上步骤操作后仍无法解决,欢迎提交工单联系 IMIDC 7×24 技术支持。提交时请注明服务器 IP、操作系统、已执行的命令和报错截图,工程师可以更快定位问题。

这篇文章有帮助吗?

相关教程