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.www58058.com/58401

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

相關推薦

  • Linux基礎 文件系統目錄標準FHS

    概述 本文前半部分介紹了 Linux 文件系統目錄標準 FHS (Filesystem Hierarchy Standard)。后半部分整理了個人學習 Linux 過程中涉及到的部分文件和路徑,激勵自己堅持更新,以期完善。 背景 由于Linux是遵循開源協議,任何人都可以根據Linux的核心代碼制作和發行版本。如果每個人都按自己的喜好,在/目錄下創建目錄、存…

    Linux干貨 2016-08-07
  • 一周作業體會

    1、描述計算機的組成及其功能 CPU+控制器+RAM+輸入設備+輸出設備 2、按系列羅列Linux的發行版,并描述不同發行版之間的聯系與區別 答:最主流的發行版主要有: Debian/Slackware/Redhat,我們日常最常使用的操作系統大多是上述發行版的二次再發行版,例如:基于Debian的二次發行版Ubuntu和Knopix,基于Slac…

    Linux干貨 2016-10-31
  • 使用httpd反向代理模塊實現tomcat負載均衡集群(下)

    上一篇講解了http使用mod_http和mod_ajp代理模塊實現tomcat負載均衡,下面我們來講解使用http的mod_jk實現taomcat的負載均衡集群: 注意:http的mod_jk是第三方擴展模塊,在新http版本中以不支持,在httpd 1.3和2.0效果較好 6、使用mod_jk實現tomcat負載均衡集群 6.1安裝mod_jk [roo…

    Linux干貨 2015-07-21
  • 關于大型網站技術演進的思考(十四)–網站靜態化處理—前后端分離—上(6)

    原文出處: 夏天的森林  前文講到了CSI技術,這就說明網站靜態化技術的講述已經推進到了瀏覽器端了即真正到了web前端的范疇了,而時下web前端技術的前沿之一就是前后端 分離技術了,那么在這里網站靜態化技術和前后端分離技術產生了交集,所以今天我將討論下前后端分離技術,前后端分離技術討論完后,下一篇文章我將會以網站 靜態化技術的角度回過頭來…

    Linux干貨 2015-02-26
欧美性久久久久