生成本地的ssh密钥(免密码登录)

Administrator
发布于 2025-05-27 / 1 阅读
0
0

生成本地的ssh密钥(免密码登录)

生成本地的ssh密钥

要将 SSH 密钥添加到服务器中,你需要将 公钥 添加到服务器的 ~/.ssh/authorized_keys 文件中,而 私钥 保持在你的本地计算机上。

步骤:

  1. 复制公钥到服务器: 使用以下命令将公钥复制到远程服务器上:

    ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_server_ip

    其中:

    • -i 参数后面跟的是你本地的公钥文件路径,默认是 ~/.ssh/id_rsa.pub

    • username 是远程服务器的用户名。

    • remote_server_ip 是远程服务器的 IP 地址。

    这个命令会将公钥添加到远程服务器的 ~/.ssh/authorized_keys 文件中。

  2. 手动添加公钥到服务器(如果 ssh-copy-id 无法使用): 如果你无法使用 ssh-copy-id,可以手动将公钥复制到服务器:

    • 打开你的公钥文件:

      cat ~/.ssh/id_rsa.pub

      复制输出的公钥内容。

    • 通过 SSH 登录到远程服务器:

      ssh username@remote_server_ip
    • 在远程服务器上,创建 .ssh 目录(如果尚不存在),并将公钥添加到 authorized_keys 文件:

      mkdir -p ~/.ssh
      echo "your_public_key" >> ~/.ssh/authorized_keys

      这里的 your_public_key 就是你从 cat ~/.ssh/id_rsa.pub 命令中复制的公钥内容。

  3. 设置正确的权限: 确保 ~/.ssh 目录和 authorized_keys 文件的权限正确:

    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys

总结:

  • 公钥:需要添加到服务器的 ~/.ssh/authorized_keys 文件中。

  • 私钥:保存在本地计算机上,不需要上传到服务器。你在连接服务器时会使用本地的私钥来进行身份验证。

添加公钥后,你就可以使用 SSH 私钥连接到远程服务器了,无需每次输入密码。

wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。 root@localhost:~# cat /ssh cat: /ssh: No such file or directory root@localhost:~# cat ssh cat: ssh: No such file or directory root@localhost:~# ssh-keygen -t rsa -b 4096 -C "xs7379@gmail.com" Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa Your public key has been saved in /root/.ssh/id_rsa.pub The key fingerprint is: SHA256:Sn9XkYeZ/A+MKdQHr5HCoiaRnHdjZ50InzLvxWf7ytA xs7379@gmail.com The key's randomart image is: +---[RSA 4096]----+ | . . | | . o + = = | | = . B X X .| | o + X o B + | | . + S o * * .| | + o . o = o.| | . . o o E .| | . . o . | | o..| +----[SHA256]-----+ root@localhost:~# ls -l ~/.ssh/ total 24 -rw------- 1 root root 565 Oct 25 21:49 authorized_keys -rw------- 1 root root 76 Oct 25 21:45 config -rw------- 1 root root 3381 Mar 3 10:46 id_rsa -rw-r--r-- 1 root root 742 Mar 3 10:46 id_rsa.pub -rw------- 1 root root 1990 Feb 10 11:18 known_hosts -rw------- 1 root root 870 Jan 15 18:11 known_hosts.old root@localhost:~#

公钥:cat ~/.ssh/id_rsa.pub

私钥:cat ~/.ssh/id_rsa

要将 SSH 密钥添加到服务器中,你需要将 公钥 添加到服务器的 ~/.ssh/authorized_keys 文件中,而 私钥 保持在你的本地计算机上。

步骤:

  1. 复制公钥到服务器: 使用以下命令将公钥复制到远程服务器上:

    ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_server_ip

    其中:

    • -i 参数后面跟的是你本地的公钥文件路径,默认是 ~/.ssh/id_rsa.pub

    • username 是远程服务器的用户名。

    • remote_server_ip 是远程服务器的 IP 地址。

    这个命令会将公钥添加到远程服务器的 ~/.ssh/authorized_keys 文件中。

  2. 手动添加公钥到服务器(如果 ssh-copy-id 无法使用): 如果你无法使用 ssh-copy-id,可以手动将公钥复制到服务器:

    • 打开你的公钥文件:

      cat ~/.ssh/id_rsa.pub

      复制输出的公钥内容。

    • 通过 SSH 登录到远程服务器:

      ssh username@remote_server_ip
    • 在远程服务器上,创建 .ssh 目录(如果尚不存在),并将公钥添加到 authorized_keys 文件:

      mkdir -p ~/.ssh
      echo "your_public_key" >> ~/.ssh/authorized_keys

      这里的 your_public_key 就是你从 cat ~/.ssh/id_rsa.pub 命令中复制的公钥内容。

  3. 设置正确的权限: 确保 ~/.ssh 目录和 authorized_keys 文件的权限正确:

    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys

总结:

  • 公钥:需要添加到服务器的 ~/.ssh/authorized_keys 文件中。

  • 私钥:保存在本地计算机上,不需要上传到服务器。你在连接服务器时会使用本地的私钥来进行身份验证。

添加公钥后,你就可以使用 SSH 私钥连接到远程服务器了,无需每次输入密码。如果你有任何问题,请随时告诉我!😊

一键导入免密码登录

1. 在客户端生成 SSH 密钥对

首先,在客户端机器上生成 SSH 密钥对(公钥和私钥)。

ssh-keygen -t rsa -b 4096

按提示操作,可以选择文件存储路径(默认是 ~/.ssh/id_rsa)和设置密码(如果不需要密码,可以直接按 Enter 跳过)。

2. 将公钥复制到服务器

生成密钥对后,将公钥 (id_rsa.pub) 复制到远程服务器上。可以使用 ssh-copy-id 命令来自动完成这一步:

ssh-copy-id username@server_ip

其中 username 是你的远程服务器用户名,server_ip 是服务器的 IP 地址。你会被要求输入远程服务器的密码。


评论