【问题标题】:How to generate ssh keys (for github)如何生成 ssh 密钥(对于 github)
【发布时间】:2022-04-21 22:33:03
【问题描述】:

问题:如何使用命令行生成 ssh 私钥和公钥(用于 GitHub/GitLab)。

下面的命令会产生错误

sh.exe": syntax error near unexpected token '('

我使用的是 Windows XP。

$ ssh-keygen -t rsa -C "xxxx@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/xxxx/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/xxxx/.ssh/id_rsa.
Your public key has been saved in /c/Users/xxxx/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db xxxx@gmail.com

【问题讨论】:

标签: git ssh github ssh-keys


【解决方案1】:

要运行的命令只有

ssh-keygen -t rsa -C "you@example.com"

从脚本第 2 行开始的所有其余部分都是 ssh-keygen 的输出。

并将 you@example.com 替换为您的电子邮件地址。

查看manual for ssh-keygen 以查找其他选项。您可能应该通过将-b 4096 添加到选项列表来使用更长的密钥。

【讨论】:

  • 这条线(/c/Users/Tekkub/.ssh/id_rsa)是否也保持不变?
  • 这是命令的输出。后面可以输入自己的路径,也可以保留默认。
  • 包含-b 4000 这样你的密钥会不会更好?
  • 如何在 Ubuntu OS 中生成它?
  • @Juniar 如果你安装了包openssh-client,然后运行命令。
【解决方案2】:

这是命令

ssh-keygen -t rsa -b 4096 -C "[your github's email]"
# Creates a new ssh key
# Generating public/private rsa key pair.

这将为您生成一个密钥。您必须将其复制并插入到您的 Github 帐户中(只需一次)。

Steps how to do It

【讨论】:

  • 最佳答案。您的答案值得打上绿色勾号。
【解决方案3】:

第 1 步:生成 SSH 密钥

$ ssh-keygen -t rsa -b 4096 -C "example@example.com"

第 2 步:使用密钥

$ eval $(ssh-agent -s)

然后添加我们刚刚生成的密钥。如果您选择了与默认路径不同的路径,请务必在命令中替换该路径。

ssh-add ~/.ssh/id_rsa

第 3 步:在 GitHub 上添加 SSH 密钥

clip < ~/.ssh/id_rsa.pub

如果没有找到剪辑,则添加下一个命令

cat ~/.ssh/id_rsa.pub

最终在你的 cmd 上得到类似的结果

ssh-rsa AAAAB3NzaC1yc2EAAAAD173Oe1kp0Ojnsn7sRWt/XT5nFH3CSTv6VWyxq4YUJ4ZykWa5opyiAJmvtjxOMCmVTuX2r1T4Livn0foHGt7+66FJXrXUQgyJ4RXanufb3bAekxbFPg3S6Gyr2kk+I3TYWcFsLLwjU0VVJwodQkpyygAUzhomx9OQ0FMpfkQa5VrZnaSLjjtNOUSAaq30e7JWYxtoVih2HfVkcmqc53QjY4T3Xb0cmLZF3EmwCeB66dgJtpTNMvM54ceD30INsaMFNqG8XjbJtGUCHYEIR5l/LI20K5F25BRTnCzRV9dB5FUJ8taVMGRHJob9PDUdxpA2HEJYilm8ZbLbRmKJtU+H91WNCG6pvy9Yf9MREkOZQHcWIuAsH8uJvTTbqm1eAY2g34FjarRdqZIpvdxkrErDHqh4k42owNEmHjSaAw53Y8M54fjBdFHTjGDqHBamlKcIzQbin/czFq1a+pgopIANDhZjWZNvMiTak7BjVrVOciKD3Pa/KKy03nSEfEGExLbFEi1Q8QEGd6idtUAjL7fyVPWatRwCnOvGLMPTk73m7t0IAYTm4n7dO0S5OeWTUPTT+8vIfFCmE9OT2aJDIpZY1+f8Q== youremail@gmail.com

从您的 cmd 复制并转到您的 GitHub 帐户 设置查找 ssh 和 gpg 密钥

【讨论】:

    【解决方案4】:

    解决方案ssh-keygen -t rsa

    说明ssh-keygen 是一种用于为 SSH 创建新的身份验证密钥对的工具。此类密钥对用于自动登录、单点登录和验证主机

    (例如,将项目从 Github 上的私人仓库直接克隆到您的 aws 机器)。

    选项:您可以执行更复杂的操作并使用标志来根据您的用例生成量身定制的密钥,扩展功能更强大和安全。基本标志是:bits(使 RSA 密钥不易受到攻击且难以破解的大整数)、passphrase(类似于密码)、type (dsa/ecdsa/ed25519/rsa)、comment对于特定的 ssh 令牌(电子邮件或用户名)和 output key(默认存储在 ~/.ssh/ 路径上)

    简介:ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa] [-N new_passphrase] [-C comment] [-f output_keyfile]

    示例

    ssh-keygen -b 4096 -t rsa -n "tHiSiaPasSWoRd" -c "johnDoe@gmail.com" -f ~/.ssh/id_rsa
    

    【讨论】:

      【解决方案5】:

      使用以下命令生成 SSH 密钥

      ssh-keygen -trsa -C user@xyz.com
      

      只需按回车键即可为其询问的所有输入应用默认值

      输出将是

      Generating public/private rsa key pair.
      Enter file in which to save the key (/home/user/.ssh/id_rsa): 
      /home/user/.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 /home/user/.ssh/id_rsa
      Your public key has been saved in /home/user/.ssh/id_rsa.pub
      The key fingerprint is:
      SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx user@xyz.com
      The key's randomart image is:
      +---[RSA xxxx]----+
      | ..++xX*Ox        |
      | x..xx/+.x. + .   |
      |.. +..*+xx x +    |
      |... +x+o x.       |
      |x.. + +..x        |
      |xx.. x            |
      |...               |
      |x x               |
      |x  x.             |
      +----[SHA256]------+
        
      

      只需使用cat 命令在终端上记录 ssh 密钥文件内容(此处使用您自己的路径)。

      cat /home/user/.ssh/id_rsa.pub
      

      输出将是

      ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx user@xyz.com

      复制内容并转到您的 github 帐户转到帐户设置下的设置>SSH 和 GPG 密钥单击New SSH key,提供您想要的名称并将复制的内容粘贴到值中并保存。就是这样,您准备好提交您的更改,而无需每次都使用用户名和密码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-14
        • 2011-01-28
        • 1970-01-01
        • 2021-12-08
        • 2022-01-19
        • 2021-02-23
        • 2013-03-17
        • 2022-01-18
        相关资源
        最近更新 更多