Technical Anshu
Tutorials Blog
🐧 SSH 安全

使用密钥和无密码登录在全新 Linux 服务器上保护 SSH

The automated setup gave your server an SSH door so you can manage it from your desk.此页面正确锁定了那扇门:按键登录,密码猜测 - 以安全顺序完成,不会将您锁定在自己的服务器之外。

✍️ Technical Anshu🐧 Linux Server⏱️ 9 minute read
🎥 视频中的您的服务器已经运行了 SSH — 此页面是每次全新安装后都应遵循的安全通行证

SSH 是几乎所有 Linux 服务器的前门,互联网不断地敲门。自动机器人会在暴露的 SSH 服务器出现后数小时内尝试使用常见密码。密码——即使是一个像样的密码——也是一个可以被猜测、泄露或重复使用的秘密。 SSH 密钥 是一对匹配的密钥:私有的一半永远不会离开您的计算机,公共的一半则存在于服务器上。没有钥匙,就无法进入,也没有什么可猜测的。这是整个强化顺序,按照唯一安全的顺序。

Terminal — creating and installing an SSH key (example)
$ ssh-keygen -t ed25519 Your public key has been saved in id_ed25519.pub $ ssh-copy-id [email protected] Number of key(s) added: 1 $ ssh [email protected] Welcome to your server (no password asked)
在您自己的计算机上创建和安装密钥。地址和用户名是示例 - 使用您服务器的真实地址和用户名。
1

第 1 步 — 在您自己的计算机上创建密钥对

在您的机器上运行它,而不是服务器。如果计算机是共享的,请接受默认位置并在密钥上设置密码。

  1. ssh-keygen -t ed25519 creates a modern, strong key pair
  2. 出现两个文件:私钥(切勿共享它,切勿将其上传到任何地方)和 .pub 公钥(这一半可以安全地复制到服务器)
  3. 已经拥有之前工作的密钥?重复使用它——一个身份密钥适用于所有服务器是正常的
2

第 2 步 — 在服务器上安装公钥

While password login still works, copy the public half across.在 Linux 和 macOS 上,该工具是内置的; on Windows, the same command ships with the OpenSSH client, or you can paste the public key into the file by hand.

  1. ssh-copy-id [email protected] — 使用您的真实用户名和服务器地址
  2. 它最后一次询问服务器密码,并将您的密钥附加到服务器上的 ~/.ssh/authorized_keys 中
  3. 使用 ssh [email protected] 再次登录 — 如果没有询问密码,则密钥有效
3

第 3 步 — 在更改任何内容之前在第二次会议中证明这一点

This is the step that separates a secure server from a locked-out admin.打开新终端窗口并在第一个会话保持打开状态时使用密钥登录。 Only when the second login succeeds do you touch the settings.如果出现任何问题,您的第一次会议仍然可以解决它。

4

步骤 4 — 关闭密码登录和直接 Root 登录

Edit the SSH server settings on the server with sudo nano /etc/ssh/sshd_config, set the two lines below, then restart the service with sudo systemctl restart ssh.在关闭旧会话之前,在另一个新会话中进行测试。

  1. PasswordAuthentication no — 密码不再适用于 SSH;仅钥匙
  2. PermitRootLogin prohibit-password — 根本无法通过 SSH 使用密码访问 root 帐户;您以自己的身份登录并使用 sudo
  3. 诚实的说明:经验丰富的管理员有时会将使用密钥直接 root 登录保留在救援系统上。对于普通的办公室或家庭服务器,您的命名帐户加上 sudo 是更安全、更负责任的模式
5

第五步——你保留的安全网

保留一种不依赖于 SSH 的本地方式:计算机自己的屏幕和键盘、虚拟机控制台或托管提供商的 Web 控制台。服务器机房和 VPS 面板都提供其中之一。 As long as that exists, even a total SSH mistake is a five-minute fix, not a disaster.

🔴 永远不要在单独的会话中测试密钥登录之前关闭密码登录。大多数人就是因为这个单一的订购错误而无法获得新的服务器——而且这是完全可以避免的。

🎥 观看视频中的完整设置

The video’s server is reachable by SSH the moment setup ends. Do this hardening pass right after your first login, while the steps are fresh.

▶️ Watch Full Video
❓ 常见问题(FAQ)

我丢失了我的私钥。难道我就永远被锁在外面了吗?

No, as long as you still have the local console or provider web console. Log in there, add a new public key to ~/.ssh/authorized_keys, and remove the old one. This is why keeping a non-SSH way into the machine matters.

我可以在多台服务器上使用相同的密钥吗?

是的。通常的模式是每人一对密钥,在他们管理的每台服务器上使用。如果一台笔记本电脑丢失,您可以从每台服务器上删除该公钥,并且丢失的笔记本电脑的访问权限将在任何地方终止。

我是否还应该将 SSH 端口更改为随机端口?

It reduces background noise in the logs but adds almost no real security — bots find open ports on any number. Keys plus password login off is the protection that matters. Change the port only if you enjoy quieter logs, and update your firewall rule to match.

本指南基于上面链接的技术安数视频 • 卡在某个步骤上?在视频评论中提问 — 答案就在那里🙏