Centos7 服务器部署ssh证书授权登录

在当前服务器生成密钥对 ssh-keygen –t rsa –P ”

ssh-keygen –t rsa –P ''
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
59:17:be:64:5e:23:d3:76:eb:8f:87:5a:e8:8f:d9:b5
root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
......

确认一路回车,命令默认会在用户家目录下生成.ssh目录,有两密钥对文件

[root@Centos6 ~]# cd .ssh/
[root@Centos6 .ssh]# ll
total 16
-rw-r--r--. 1 root root  416 Nov  7 08:09 authorized_keys
-rw-------. 1 root root 1671 Nov  7 08:06 id_rsa
-rw-r--r--. 1 root root  416 Nov  7 08:06 id_rsa.pub
-rw-r--r--. 1 root root  784 Nov  7 08:21 known_hosts
[root@Centos6 .ssh]#
//id_rsa是私钥,id_rsa.pub是公钥

接着在master节点上做如下配置,把id_rsa.pub追加到授权的key里面去。

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
[root@Centos6 ~]# 非root用户则需要授权

服务器改SSH配置文件”vim sshd_config”修改如下内容(去掉前面的注释)

RSAAuthentication yes # 启用 RSA 认证
PubkeyAuthentication yes # 启用公钥私钥配对认证方式
AuthorizedKeysFile .ssh/authorized_keys # 公钥文件路径同上
# 有了证书登录了,就把密码登录禁用,安全要紧
PasswordAuthentication no

最后重启ssh服务

systemctl restart sshd.service
测试登录
ssh localhost
[root@Centos6 ~]# ssh localhost
Last login: Mon Nov  7 08:34:38 2016 from localhost
[root@Centos6 ~]#
第一次要确认输入yes,以后就不用。
因为无法确认host主机的真实性,只知道它的公钥指纹,问你还想不想继续
继续,会将localhost加入known hosts中,后面就不会再出现这种情况了。

Centos7 配置用于节点鉴别权限的SSH密钥

功能同上面的一样,只是这种方法我在ansible上用就记下来了。怕忘

root@localhost ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:
da:7a:b6:8c:e3:05:da:d4:04:0a:a4:43:58:f0:e1:33 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|o==   .          |
|o+ o . .         |
|o E .   .        |
| . o   o         |
|      o S        |
|     + +         |
|    . o o        |
|      .=o        |
|     .++o.       |
+-----------------+
[root@localhost ~]#

使用ssh-copy-id命令来复制Ansible公钥到每个节点中。

[root@localhost ~]# ssh-copy-id -i root@10.1.252.205
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.1.252.205's password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'root@10.1.252.205'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]#

还有很多都可以复制到节点上的 
scp、ansible all -m copy -a (copy是ansible命令里的一个模块)

生成一个文件
ansible all -m copy -a "src=/root/.ssh/id_rsa.pub dest=/tmp/id_rsa.pub" --ask-pass -c paramiko
再拷贝到远程服务器上
ansible all -m shell -a "cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys" --ask-pass -c paramiko

原创文章,作者:51eA,如若转载,请注明出处:http://www.178linux.com/58401

(0)
51eA51eA
上一篇 2016-12-05 17:03
下一篇 2016-12-05 17:03

相关推荐

  • 第五周作业

    1、简述rpm与yum命令的常见选项,并举例 rpm 命令 linux的的包管理器,用于对rpm软件包,进行安装、升级、卸载、查询、校验等操作。 使用语法: rpm [OPTIONS] [PACKAGE_FILE] 常用选项: 安装: -i,–install 升级: -U,–update,-F,–freshen 卸载: -…

    Linux干货 2017-12-14
  • Linux发展史

    一、简单的说: 一个名叫Linus Torvalds的芬兰大学生想要了解Intel的新CPU386。他认为比较好的学习方法是自己编写一个操作系统的内核。出于这种目的,加上他对当时Unix 变种版本(即Minix)对于80386类机器的脆弱支持十分不满,他决定要开发出一个全功能的、支持POSIX标准的、类Unix的操作系统内核,该系统吸收了BSD和S…

    Linux干货 2016-10-20
  • 马哥教育网络21期+第十二周练习博客中

    4、建立httpd服务器(基于编译的方式进行),要求:  提供两个基于名称的虚拟主机: (a)www1.stuX.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1.err,访问日志为/var/log/httpd/www1.access; (b)www2.stuX.com,页面文件目录为/web…

    Linux干货 2016-10-17
  • Linux三剑客之grep使用入门指南

    Linux的grep是一个具有强大功能的文本搜索工具,正确的学习和使用,能很大程度上提高工作效率,减轻运维工作所面临的压力。

    2017-09-09
  • linux上的文件查找工具:locate和find

    一、非实时超找工具 locate     1、locate是查询系统上预建的文件索引数据库/var/lib/mlocate/mlocate.db文件索引实在系统比较空闲的时候自动进行的,是系统的周期性任务,因为是周期性任务,所以有些短期的数据无法更新到数据库,如果需要马上应用,乣管理员手动更新数据库,用命令updatedb来执行。同时索引…

    Linux干货 2016-08-16
  • 马哥教育网络班21期-第一周课程练习

    第一周课程练习 1、描述计算机的组成及其功能?         1.1 现代主流的计算机(冯·诺依曼体系)主要由五部分组成,分别是控制器、运算器、存储器、输入和输出设备。控制器和运算器对应我们现实计算机看到的CPU,存储器对应内存和硬盘等存储设备,输入设备对应鼠标键盘等其它外设,输出设备对应显示器、音响、打印机等其它外…

    Linux干货 2016-06-26