前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux之sshkey密钥认证实战

Linux之sshkey密钥认证实战

作者头像
星哥玩云
发布2022-07-12 19:35:12
4.4K0
发布2022-07-12 19:35:12
举报
文章被收录于专栏:开源部署开源部署

在实际的生产环境中,经常会用到sshkey密钥认证实行数据分发数据等操作,还可以批量操作内网服务器,实行免密认证进行推送分发数据。

1、环境查看

分发服务器

节点服务器

2、服务器添加系统账号

3、生成密钥对

[root@localhost1 ~]# su - linuxidc [fenfa@localhost1 ~]$ whoami linuxidc

[fenfa@localhost1 ~] ssh-keygen -t dsa Generating public/private dsa key pair.Enter file in which to save the key (/home/linuxidc/.ssh/id_dsa): Created directory '/home/linuxidc/.ssh'.  #创建目录Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/linuxidc/.ssh/id_dsa.Your public key has been saved in /home/linuxidc/.ssh/id_dsa.pub.The key fingerprint is:2c:33:62:96:d5:5a:56:a8:19:63:29:de:63:35:83:f1 linuxidc@localhost1The key's randomart image is:+--[ DSA 1024]----+|      .+ ..      ||    . *o=.      ||  . +.*Eo      ||    .o*=        ||    =.=.S        ||  o . +        ||                ||                ||                |+-----------------+[linuxidc@localhost1 ~] cd /home/linuxidc/.ssh/[linuxidc@localhost1 .ssh] ls -ld .sshls: cannot access .ssh: No such file or directory[linuxidc@localhost1 .ssh] ls -ld /home/linuxidc/.ssh/drwx------ 2 linuxidc fenfa 4096 Feb 21 00:25 /home/linuxidc/.ssh/[linuxidc@localhost1 .ssh]

[linuxidc@localhost1 .ssh]$ cat /home/linuxidc/.ssh/id_dsa -----BEGIN DSA PRIVATE KEY----- MIIBuwIBAAKBgQDLu2NAZzbTkIb5qAXlFEEud/Ka3dS37dSzIgpkWeX5M65niDgJ NoXybn5t14YC5Ur7ef1MXQWsTJlzz1Y6+2MXNUnqnttIsbFGqPFCxfITDCryE9xZ L16LF7LLFcYyAiXDNJApQXRfnt8p4+5NYEx7WUtRnFhcAyolGCKGV9FlpQIVAPd4 UIqd7o3o3z+R8JbB0kB7uQmzAoGAb7DhH9aXMrDCERjr3u3kb6/P5yKl1h4Bx/Il k6Ye/hmly5jPk2ylwifPb0iVHVfKvREVGEQ84SUkYzGhAkws1hcrEp9auunzYDNP Sw8rTRwnGHe+jeSJpMFyE/XiAdLm9dlvNubgcprAhrY7j2dgM6lMzI4Wzx64yPm5 qqEF3AwCgYAswJiFXDeTFDumtSeAYwukCbuYFmuOZFep17Vo+5GO/EYmhv0WLkry mfkWIgzeq9RfiDj2jVGxMPwRdfIqqeThzrs8nJKNgIwhI8yN/EcjwdHA9iwkMWng +eXweI3JLGwAwOi8K0E5daJe6QyXZO67nlZt6RPsnnOZzsgdqIrtkwIVAK4QeeT/ lrNTWfo0hWEh7DPEGHsx -----END DSA PRIVATE KEY-----

4、分发密钥

分发的命令格式 ssh-copy-id -i 密钥名称 用户@远端主机 IP 地址--------------用于 ssh 是默认端口 ssh-copy-id -i 密钥名称 “-p port 用户@远端主机 IP 地址”--用于 ssh 非默认端口

[linuxidc@localhost1 .ssh]$ ssh-copy-id -i id_dsa.pub linuxidc@192.168.181.129  #分发的命令 The authenticity of host '192.168.181.129 (192.168.181.129)' can't be established. RSA key fingerprint is 9e:81:e9:02:86:a0:24:37:2b:d0:4e:ae:d4:41:6f:0d. Are you sure you want to continue connecting (yes/no)? y Please type 'yes' or 'no': yes Warning: Permanently added '192.168.181.129' (RSA) to the list of known hosts. linuxidc@192.168.181.129's password: Permission denied, please try again. linuxidc@192.168.181.129's password: Permission denied, please try again. linuxidc@192.168.181.129's password: Now try logging into the machine, with "ssh 'linuxidc@192.168.181.129'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[linuxidc@localhost1 .ssh]$ ssh-copy-id -i id_dsa.pub linuxidc@192.168.181.129 Now try logging into the machine, with "ssh 'linuxidc@192.168.181.129'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

查看是否分发成功

[linuxidc@localhost2 ~]$ tree /home/linuxidc/.ssh/ /home/linuxidc/.ssh/ └── authorized_keys  分发成功

0 directories, 1 file

5、分发数据

不需要输入密码

当每天都需要分发数据时,可以把命令写入脚本中,再加入定时任务,就可以自动分发数据。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
全站加速网络
全站加速网络(Enterprise Content Delivery Network,以下简称 ECDN)为您提供稳定高效的网络加速服务,适用于动静混合、纯动态、跨国、上传等多种加速场景。ECDN 网络资源丰富,同时融合静态缓存、智能路由、协议优化、多路传输、抗抖动等自研技术,加速效果更加显著;接入便捷,功能配置灵活多样,可满足您个性化的业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档