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
[email protected]
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 [email protected]
/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 '[email protected]'"
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

相关推荐

  • python之psutil模块

    python 安装psutil 来实现获取系统信息  # yum -y install python*pip # yum -y groupinstall “Development Tools # yum -y install python34-devel.x86_64 # pip3 install –upgrade pip # pip3 inst…

    Linux干货 2014-01-22
  • 马哥教育21期网络班—第六周课程+练习—-成长进行时

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp/rc.sysinit&nbs…

    Linux干货 2016-08-03
  • 关于IO的同步,异步,阻塞,非阻塞

    上次写了一篇文章:Unix IO 模型学习。恰巧在这次周会的时候,@fp1203 (goldendoc成员之一) 正好在讲解poll和epoll的底层实现。中途正好讨论了网络IO的同步、异步、阻塞、非阻塞的概念,当时讲下来,大家的理解各不相同,各执己见。搜索了网络上的一些文章,观点也各不相同,甚至连wiki也将异步和非阻塞当成一个概念在解释。  &…

    Linux干货 2015-04-02
  • Linux基础知识(四)

    本文主要讲述:Linux上用户和组的基本管理,具体包括一下内容 1、复制/etc/skel目录到/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 2、编辑/etc/group文件,添加组hadoop。 3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id…

    Linux干货 2016-10-16
  • 正则表达式—grep的用法详解

    grep与sed、awk共称为文字处理三剑客,支持正则表达式语句,具体用法如下: grep [options] PATTERN [file]     #常用选项:     –color=auto  #对匹配到的文本着色显示 &n…

    Linux干货 2016-07-04
  • linux 生产常用命令

    hostname 显示当前的主机名pwd 显示当前工作的目录wwhowhoami显示当前用户及已建立的ssh链接useradd 新建一个普通用户passwd 修改用户的密码which 查看系统命令所对应的路径type 查询命令是否属于内部命令或者外部命令hash 哈希 把一个命令一个文本通过某种算法生产一串字符df 查看系统分区hash -l 加快命令查询执…

    Linux干货 2017-03-28