前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ssh-key生成密钥及SSH无密码登录的配置

ssh-key生成密钥及SSH无密码登录的配置

作者头像
foochane
发布2019-07-02 18:00:26
3K0
发布2019-07-02 18:00:26
举报
文章被收录于专栏:foochanefoochane

文章作者:foochane

原文链接:https://foochane.cn/article/2019061601.html

1 ssh-keygen命令

ssh-keygen命令说明:

  • -t :指定加密类型(如:rea,dsa)
  • -C : 指定注释,用于识别这个密钥

其他参数具体可以查看帮助

代码语言:javascript
复制
$ ssh-keygen help
Too many arguments.
usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa]
                  [-N new_passphrase] [-C comment] [-f output_keyfile]
       ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]
       ssh-keygen -i [-m key_format] [-f input_keyfile]
       ssh-keygen -e [-m key_format] [-f input_keyfile]
       ssh-keygen -y [-f input_keyfile]
       ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]
       ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]
       ssh-keygen -B [-f input_keyfile]
       ssh-keygen -F hostname [-f known_hosts_file] [-l]
       ssh-keygen -H [-f known_hosts_file]
       ssh-keygen -R hostname [-f known_hosts_file]
       ssh-keygen -r hostname [-f input_keyfile] [-g]
       ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]
       ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]
                  [-j start_line] [-K checkpt] [-W generator]
       ssh-keygen -s ca_key -I certificate_identity [-h] [-U]
                  [-D pkcs11_provider] [-n principals] [-O option]
                  [-V validity_interval] [-z serial_number] file ...
       ssh-keygen -L [-f input_keyfile]
       ssh-keygen -A
       ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]
                  file ...
       ssh-keygen -Q -f krl_file file ...

实际情况中也用不到那么多参数,指定加密类型和注释即可。 例如:

代码语言:javascript
复制
$ ssh-keygen -t rsa -C "myname@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\fucheng/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\fucheng/.ssh/id_rsa.
Your public key has been saved in C:\Users\fucheng/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:9OlHGn5uIlELfGIYXdWectiEV5XS2quWpD1qpd2QJC8 myname@163.com
The key's randomart image is:
+---[RSA 2048]----+
|       . ....o..=|
|      . .   ..+o |
|       +.    *+. |
|      ..=.oooo=. |
|       .S=+.=o. .|
|        .o.E * . |
|         .+ @ =  |
|        . .B.B . |
|         ..++ .  |
+----[SHA256]-----+

一般情况下不需要输入密码,直接回车即可。

执行完ssh-keygen之后会在,用户目录下的.ssh文件下,生成一个id_rsa文件和id_rsa.pub文件。

  • id_rsa文件是私钥,要保存好,放在本地,私钥可以生产公钥,反之不行。
  • id_rsa.pub文件是公钥,可以用于发送到其他服务器,或者git上。

2 ssh设置无密码登录服务器

将之前在本地生成的公钥id_rsa.pub,发送到需要无密码登录的服务器,然后将id_rsa.pub的内容追加到服务器的~/.ssh/authorized_keys文件中即可。

如果没有.ssh目录,创建一个就好,或者执行ssh localhost登录本地,ssh会自动创建。

可以使用如下命令进行操作:

代码语言:javascript
复制
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys 

另外如果想要无密码登录本地localhost,那么在本地执行上面的命令即可,执行之后再 ssh localhost 就不需要输入密码了。

3 设置ssh无密码访问git仓库

注意这里访问的主要是私有仓库。

github为例,找到个人主页,点击[settings],找到[SSH and GPG keys] ,新建SSH keys,将本地id_rsa.pub的内容复制到key里面,tittle可以随便填写,这样就配置好了。

找到要访问的仓库主页,点击Clone or Downloaduse Http换成use SSH,然后就会显示对应的仓库地址如:git@github.com:uername/xxxxx.git

使用该地址就可以在本地进行无密码访问仓库了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.06.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 ssh-keygen命令
  • 2 ssh设置无密码登录服务器
  • 3 设置ssh无密码访问git仓库
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档