公众号旧文

SSH 登录 Linux 服务器完整教程

作者:胖子阅读 —

SSH,全称Secure Shell,是一种通过网络远程登录服务器的安全协议。

如果你购买了一台 Linux 云服务器、VPS,通常服务商会给你这些信息:

  • 服务器公网 IP
  • SSH 端口
  • 用户名
  • 密码,或者 SSH 私钥

有了这些信息,就可以通过 SSH 登录服务器。


一、最基本的 SSH 登录命令

假设服务器信息如下:

IP:192.0.2.10

用户名:root

SSH 端口:22

最简单的登录命令是:

ssh root@192.0.2.10

如果服务器使用默认 SSH 端口 22,不需要额外指定端口。

第一次连接时,可能看到:

The authenticity of host '192.0.2.10' can't be established.

Are you sure you want to continue connecting (yes/no/[fingerprint])?

输入:

yes

然后系统会提示输入密码:

root@192.0.2.10's password:

输入服务器密码后按回车。

注意:

Linux SSH 输入密码时,屏幕不会显示任何字符,也不会显示*。

这是正常现象。

密码正确后,就会进入服务器。

例如:

Linux server 6.1.0-amd64
root@server:~#

看到类似:

root@server:~#

就表示已经成功登录。


二、SSH 端口不是 22 怎么办?

很多 VPS 为了安全,或者因为 NAT 映射,会使用其他端口。

例如:

IP:198.51.100.20

SSH端口:20100

用户名:root

登录命令:

ssh -p 20100 root@198.51.100.20

这里:

-p 20100

表示指定 SSH 端口。

完整格式:

ssh -p 端口 用户名@服务器IP

例如:

ssh -p 2222 root@203.0.113.10

三、Windows 怎么登录 SSH?

Windows 10 和 Windows 11 通常已经自带 OpenSSH 客户端。

可以打开:

PowerShell

或者:

Windows Terminal

然后直接执行:

ssh root@服务器IP

例如:

ssh root@198.23.190.46

如果 SSH 端口为 20100:

ssh -p 20100 root@198.23.190.46

然后输入密码即可。


四、Linux 和 macOS 登录方法

Linux 和 macOS 基本都自带 SSH。

打开终端:

ssh root@服务器IP

例如:

ssh root@203.0.113.10

指定端口:

ssh -p 2222 root@203.0.113.10

五、使用 SSH 密钥登录

相比密码登录,SSH 密钥更加安全,也更适合长期管理服务器。

SSH 密钥一般分为:

私钥

公钥

常见文件:

id_ed25519

id_ed25519.pub

其中:

id_ed25519

是私钥。

必须保密。

而:

id_ed25519.pub

是公钥,可以放到服务器上。


六、生成 SSH 密钥

推荐使用 Ed25519。

在 Windows、Linux 或 macOS 执行:

ssh-keygen -t ed25519

系统会提示:

Enter file in which to save the key:

直接回车即可。

一般会生成:

~/.ssh/id_ed25519

~/.ssh/id_ed25519.pub

Windows 通常位于:

C:\Users\你的用户名\.ssh\

Linux/macOS:

~/.ssh/

查看公钥:

cat ~/.ssh/id_ed25519.pub

Windows PowerShell:

type $HOME\.ssh\id_ed25519.pub

输出类似:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIxxxxxxxxxxxx user@computer

七、把 SSH 公钥添加到服务器

服务器允许哪些 SSH Key 登录,通常由这个文件控制:

~/.ssh/authorized_keys

如果是 root 用户:

/root/.ssh/authorized_keys

先创建目录:

mkdir -p ~/.ssh

设置权限:

chmod 700 ~/.ssh

然后编辑:

nano ~/.ssh/authorized_keys

把自己的公钥:

ssh-ed25519 AAAAC3...

粘贴进去。

保存之后执行:

chmod 600 ~/.ssh/authorized_keys

以后登录:

ssh root@服务器IP

通常就不需要输入服务器密码了。


八、指定 SSH 私钥登录

如果私钥不是默认文件名,可以通过 -i 指定。

例如:

ssh -i ~/.ssh/server_key root@203.0.113.10

Windows:

ssh -i C:\Users\你的用户名\.ssh\server_key root@203.0.113.10

如果还有自定义端口:

ssh -i ~/.ssh/server_key -p 2222 root@203.0.113.10

九、推荐使用 SSH Config

如果服务器很多,每次输入:

ssh -p 20100 root@198.23.190.46

会比较麻烦。

可以配置 SSH Config。

Linux/macOS:

~/.ssh/config

Windows:

C:\Users\你的用户名\.ssh\config

例如加入:

Host myserver

    HostName 198.23.190.46

    User root

    Port 20100

以后只需要:

ssh myserver

即可登录。

如果使用私钥:

Host myserver

    HostName 198.23.190.46

    User root

    Port 20100

    IdentityFile ~/.ssh/id_ed25519

管理很多服务器时,这种方式非常方便。

例如:

Host jp-pro

    HostName 203.0.113.10

    User root

Host jp-pro2

    HostName 203.0.113.20

    User root

Host us-server

    HostName 198.51.100.10

    User root

    Port 20100

以后:

ssh jp-pro

或者:

ssh jp-pro2

即可。


十、如何退出 SSH?

登录服务器后执行:

exit

或者:

logout

也可以按:

Ctrl + D

退出远程服务器。


十一、SSH 登录失败常见问题

1. Connection refused

例如:

ssh: connect to host 203.0.113.10 port 22: Connection refused

通常说明:

  • SSH 服务没有启动
  • SSH 端口不是 22
  • 防火墙拒绝连接

登录服务器控制台检查:

systemctl status ssh

Debian / Ubuntu 通常是:

systemctl status ssh

部分系统可能是:

systemctl status sshd

查看监听端口:

ss -lntp | grep ssh

2. Connection timed out

例如:

ssh: connect to host 203.0.113.10 port 22: Connection timed out

一般需要检查:

服务器是否开机

IP 是否正确

SSH 端口是否正确

云厂商安全组

iptables

nftables

防火墙

NAT 端口映射

3. Permission denied

例如:

Permission denied, please try again.

可能是:

  • 密码错误
  • 用户名错误
  • SSH 禁止密码登录
  • SSH Key 错误

例如有些服务器默认用户不是:

root

而可能是:

ubuntu

debian

centos

ec2-user

例如:

ssh ubuntu@203.0.113.10

4. Permission denied (publickey)

看到:

Permission denied (publickey)

说明服务器要求密钥登录,但客户端没有提供正确的私钥。

尝试:

ssh -i ~/.ssh/id_ed25519 root@服务器IP

也可以用调试模式查看:

ssh -v root@服务器IP

如果需要更多信息:

ssh -vv root@服务器IP

甚至:

ssh -vvv root@服务器IP

十二、查看当前 SSH 登录用户

登录服务器后执行:

who

或者:

w

查看当前登录会话:

who

查看 SSH 连接:

ss -tnp | grep ssh

查看最近登录记录:

last

十三、查看服务器允许哪些 SSH Key

以 root 用户为例:

cat /root/.ssh/authorized_keys

或者搜索所有用户:

find /root /home -type f -name authorized_keys -print 2>/dev/null

同时显示文件内容:

find /root /home -type f -name authorized_keys -print -exec cat {} \; 2>/dev/null

这样就可以检查服务器上有哪些 SSH 公钥。


十四、SSH 安全方面的几个建议

如果服务器长期运行在公网,建议至少做到:

  1. 优先使用 SSH Key,而不是纯密码登录。
  2. 不要泄露 SSH 私钥。
  3. 定期检查:
~/.ssh/authorized_keys

SSH 配置文件通常是:

/etc/ssh/sshd_config

修改完成后先检查配置:

sshd -t

确认没有错误,再重启:

systemctl restart ssh

十五、最常用的 SSH 命令总结

默认端口登录:

ssh root@IP

指定端口:

ssh -p 端口 root@IP

指定私钥:

ssh -i 私钥文件 root@IP

指定私钥和端口:

ssh -i 私钥文件 -p 端口 root@IP

调试连接:

ssh -v root@IP

退出服务器:

exit

写在最后

SSH 可以说是 Linux 服务器管理中最基础、也是最重要的工具之一。

刚开始接触服务器时,只需要先记住这一条:

ssh 用户名@服务器IP

如果不是默认的 22 端口,就变成:

ssh -p 端口 用户名@服务器IP

等服务器数量逐渐增多以后,再学习:

SSH Key

authorized_keys

SSH Config

跳板机

端口转发

ProxyJump

就可以逐渐搭建出一套更加方便、安全的服务器管理方式。