2025-09-28
告别密码:使用 SSH 密钥实现更安全、更便捷的服务器登录

前言

在日常的服务器管理和开发工作中,我们每天可能需要无数次地登录远程服务器。传统的密码登录方式不仅繁琐——需要我们记住并输入复杂的密码,更重要的是,它将我们的服务器暴露在被暴力破解的风险之下。

有没有一种方法,既能让我们安全地连接服务器,又能免去重复输入密码的烦恼呢?

答案是肯定的。那就是使用 SSH 密钥对进行身份验证。这篇博客将带你一步步配置 SSH 密钥登录,让你的服务器连接既安全又高效。

核心概念:公钥与私钥

在开始操作之前,我们先花一分钟理解一下核心概念。SSH 密钥认证基于一种名为“非对称加密”的技术,它会生成一对密钥:

  1. 公钥 (Public Key):可以把它想象成一把。你可以把这把锁安装到任何你想要进入的服务器的“门”上。这把锁是公开的,给谁都无所谓。
  2. 私钥 (Private Key):这是唯一能打开这把锁的钥匙。它必须由你本人严格保管,绝不能泄露给任何人

登录过程就像这样:你尝试连接服务器时,服务器会用你之前安装的“锁”(公钥)向你发出一个挑战,只有你本地电脑上持有配对的“钥匙”(私钥)才能正确回应这个挑战,从而证明你的身份,服务器便会允许你登录。

SSH Key Analogy
(一个简单的锁和钥匙比喻)

第一步:在你的本地电脑上生成密钥对

首先,我们需要在自己的电脑上(客户端)生成这对“锁和钥匙”。

打开你的终端(无论是 Windows 的 WSL/Git Bash,还是 macOS/Linux 的 Terminal),输入以下命令:

1
2
3
4
5
6
7
8
9
10
11
12
ssh-keygen -t ed25519 -C "your_email@example.com"
````

> **命令解析:**
>
> * `-t ed25519`: 指定使用 `ed25519` 算法。这是目前最推荐的算法,比传统的 `RSA` 更安全、性能更好。如果你的系统非常老旧,可以换成 `-t rsa -b 4096`。
> * `-C "your_email@example.com"`: 添加一个注释,通常是你的邮箱,方便你识别这个密钥的用途。

执行后,系统会向你提问:

```text
> Enter file in which to save the key (/Users/yourname/.ssh/id_ed25519):

直接按回车,将其保存在默认位置即可。

1
> Enter passphrase (empty for no passphrase):

强烈建议在这里设置一个密码 (passphrase)!这个密码是用来加密你的私钥文件的。即使某天你的电脑文件被盗,没有这个密码,黑客也无法使用你的私钥。

输入密码并确认后,你的密钥对就生成完毕了!它们位于 ~/.ssh/ 目录下:

  • id_ed25519: 你的私钥(钥匙),请妥善保管。
  • id_ed25519.pub: 你的公钥(锁),准备把它安装到服务器上。

第二步:将“锁”(公钥)安装到服务器上

现在,我们需要把公钥上传到目标服务器。这里介绍两种方法。

方法一:使用 ssh-copy-id (强烈推荐)

这是最简单、最不容易出错的方法。它会自动完成所有配置。

假设你的服务器用户名是 root,IP 地址是 123.45.67.89,执行:

1
ssh-copy-id root@123.45.67.89

系统会要求你输入一次服务器的登录密码。输入正确后,你的公钥就被自动添加到了服务器的信任列表 ~/.ssh/authorized_keys 中,并且权限也设置妥当了。

方法二:手动复制粘贴

如果你的本地电脑没有 ssh-copy-id 工具,也可以手动操作。

  1. 在本地电脑,显示并复制你的公钥内容:

    1
    cat ~/.ssh/id_ed25519.pub

    复制终端输出的那一长串 ssh-ed25519 ... 字符。

  2. 用密码登录服务器

    1
    ssh root@123.45.67.89
  3. 在服务器上,将公钥追加到 authorized_keys 文件中:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 确保 .ssh 目录存在
    mkdir -p ~/.ssh

    # 将你的公钥粘贴到这里,并追加到文件中
    echo "在这里粘贴你复制的公钥内容" >> ~/.ssh/authorized_keys

    # 修复文件权限(至关重要!)
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys

    注意:权限设置错误是导致密钥登录失败最常见的原因。SSH 要求 .ssh 目录和 authorized_keys 文件有严格的权限。

第三步:测试你的新“钥匙”

现在,退出服务器,然后尝试重新登录:

1
ssh root@123.45.67.89

如果一切顺利,系统将不再询问服务器的密码,而是提示你输入私钥的密码(就是你在第一步设置的 passphrase)。输入正确后,你就成功登录了!

第四步:安全加固 - 禁用密码登录

为了让服务器彻底免疫密码暴力破解,当你确认密钥登录工作正常后,就应该禁用传统的密码登录方式。

  1. 登录到你的服务器。
  2. 编辑 SSH 配置文件:
    1
    sudo nano /etc/ssh/sshd_config
  3. 找到并修改以下几项,确保它们的值如下(如果行首有 #,请删除):
    1
    2
    3
    PubkeyAuthentication yes
    PasswordAuthentication no
    ChallengeResponseAuthentication no
  4. 保存文件,然后重启 SSH 服务使配置生效:
    1
    sudo systemctl restart sshd

⚠️ 安全警告: 在执行此操作并断开连接之前,请务必打开一个新的终端窗口,再次测试你的 SSH 密钥登录是否正常。如果配置有误,禁用密码登录可能会导致你被锁在服务器之外!

进阶技巧:管理多个 SSH 密钥

如果你需要管理多个服务器(比如公司的、私人的)或多个 Git 账户,你可能会有多个密钥对。

  • 生成特定密钥:使用 ssh-keygen -f ~/.ssh/work_key 来生成名为 work_key 的新密钥。
  • 上传特定公钥:使用 -i 参数指定要上传的公钥:
    1
    ssh-copy-id -i ~/.ssh/work_key.pub user@work-server.com
  • 使用特定私钥登录:同样使用 -i 参数:
    1
    ssh -i ~/.ssh/work_key user@work-server.com

总结

恭喜你!你已经成功地为你的服务器配置了 SSH 密钥登录。回顾一下,我们完成了:

  1. 生成了专属的公钥和私钥。
  2. 上传了公钥到服务器。
  3. 测试了密钥登录的有效性。
  4. 禁用了密码登录,提升了服务器的安全性。

这是一项一劳永逸的投资。从现在开始,享受更安全、更便捷的服务器管理体验吧!

Read More

2024-11-20
Use ssh to connect to Google Virtual Machine

谷歌云服务器默认登录方式是browser ssh,但是有时候我们需要第三方客户端使用ssh连接服务器。
本文介绍如何使用ssh连接Google Cloud平台的虚拟机(云服务器)

Read More

2024-08-20
Use ssh to connect to AWS EC2 instance

1: Create a new key pair

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. In the navigation pane, choose Key Pairs.
  3. Choose Create key pair.
  4. For Key pair name, enter a name for the new key pair, and then choose Create key pair.
  5. Your browser will download the private key file automatically. The private key file is automatically downloaded by your browser. The base file name is the name you specified as the name of your key pair, and the file name extension is .pem. Save the private key file in a safe place.
  6. Choose Close.
  7. Important: You can create a key pair only once. Be sure to save the private key file to your computer. You’ll need to provide the name of your key pair when you launch an instance and the corresponding private key each time you connect to the instance.

2: Connect to your instance

  1. Open a terminal window.
  2. Use the cd command to navigate to the directory where your private key file is located.
  3. Use the following command to set the permissions of your private key file so that only you can read it:
1
chmod 400 /path/my-key-pair.pem
  1. Use the following command to connect to your instance. Replace ec2-user with the appropriate user name for your AMI.
  • For Amazon Linux 2 or the Amazon Linux AMI, the user name is ec2-user.
  • For a CentOS AMI, the user name is centos.
  • For a Debian AMI, the user name is admin.
  • For a Fedora AMI, the user name is ec2-user or fedora.
  • For a RHEL AMI, the user name is ec2-user or root.
  • For a SUSE AMI, the user name is ec2-user or root.
  • For an Ubuntu AMI, the user name is ubuntu. Here my OS is Ubuntu.
  • Otherwise, if ec2-user and root don’t work, check with your AMI provider.
1
ssh -i /path/my-key-pair.pem user_name@ip_address

3: (Optional) Connect to your instance using a password

  1. Open a terminal window.
  2. Use the following command to connect to your instance.
    1
    ssh user_name@my-instance-ip

I meet Permission denied (publickey) error.

To fix it.

Step1: Firstly I need to use the previous command to connect to my instance.
1
ssh -i /path/my-key-pair.pem user_name@ip_address
Step2: Set up a password for the user using passwd command along with the username.
1
sudo passwd ubuntu
Step 3: Edit sshd_config file.
1
sudo vim /etc/ssh/sshd_config

Find the Line containing PasswordAuthentication parameter and change its value from no to yes.

1
PasswordAuthentication yes

If you want to set up root login, find PermitRootLogin parameter and change its value from prohibit-password to yes

1
PermitRootLogin yes

After this changes save file and exit.

Step 4: Restart the SSH service.
1
2
3
service ssh restart ## for ubuntu

service sshd restart ## for centos
Step 5: Add the .pem file to Local ssh.
1
2
chmod 400 /path/my-key-pair.pem
ssh-add -k /path/my-key-pair.pem
Step 6: Now you can connect to your instance using a password.
1
ssh user_name@my-instance-ip
Read More