您好,登錄后才能下訂單哦!
如何進行Linux系統SSH服務中的sshkey密鑰認證實戰,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
SSH服務一些特性及其簡單配置,在實際的生產環境中,經常會用到sshkey密鑰認證實行數據分發數據等操作,還可以批量操作內網服務器,實行免密認證進行推送分發數據
實際生產結構拓撲圖如下
整個配置過程
整個配置環境只用一兩臺服務器
一、實際環境查看
分發服務器
[root@Centos ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@localhost ~]# uname -r
2.6.32-431.el6.x86_64
節點服務器
[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@localhost ~]# uname -r
2.6.32-431.el6.x86_64
二、服務器添加系統賬號
分發服務端配置賬號與密碼
[root@Centos ~]# useradd fenfa
[root@Centos ~]# echo "123456"|passwd --stdin fenfa
Changing password for user fenfa.
passwd: all authentication tokens updated successfully.
節點服務端配置賬號與密碼
[root@localhost ~]# useradd fenfa
[root@localhost ~]# echo "123456"|passwd --stdin fenfa
Changing password for user fenfa.
passwd: all authentication tokens updated successfully
三、生成密鑰對
注意此操作需切換到創建的用戶(分發服務器操作)
[root@Centos ~]# su - fenfa
[fenfa@Centos ~]$ whoami
fenfa
[fenfa@Centos ~]$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/fenfa/.ssh/id_dsa):
key存儲到這個文件
Created directory '/home/fenfa/.ssh'. 系統自動創建這個目錄
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/fenfa/.ssh/id_dsa. 私鑰
Your public key has been saved in /home/fenfa/.ssh/id_dsa.pub.公鑰
The key fingerprint is:
07:06:7a:22:ec:11:72:ae:06:36:de:1c:17:15:90:50 fenfa@Centos
The key's randomart image is:
+--[ DSA 1024]----+
|. o.oE=o. |
| = . o.. |
|.o=.o.. o |
|+o+ooo . . |
|.o.o S . |
|. . |
| |
| |
| |
+-----------------+
[fenfa@Centos ~]$ cd /home/fenfa/.ssh
[fenfa@Centos ~]$ ls -ld .ssh
drwx------. 2 fenfa fenfa 4096 Aug 27 17:09 .ssh 權限700
[fenfa@Centos .ssh]$ ls -ll
total 8
-rw-------. 1 fenfa fenfa 672 Aug 27 16:47 id_dsa
-rw-r--r--. 1 fenfa fenfa 602 Aug 27 16:47 id_dsa.pub
此處注意兩個文件的權限:id_dsa 600 id_dsa.pub 644
[fenfa@Centos ~]$ cat /home/fenfa/.ssh/id_dsa
-----BEGIN DSA PRIVATE KEY-----
MIIBvAIBAAKBgQCtz936+YRJaeXBdaVxAtCXOy7IzpUbKSLLA+IC2bg6xLOGS8U+
5qvI73eQqr2yB0HIowEfVSX51zAZShj3SPe3dO89UjD2R+PHL9ORPx2MNLed9MVS
gVFOd12OmsAJ+CDsoFV8J1iF0rjbfZ1XDwCPtktWfG5xnOwjtKSHt9aNVQIVAJ4j
t8J7fuQq40QH6KQ3iEwrupVHAoGAXO3Gel3jQTjN4HJAoWW1zloHlZiD8IkJ6Uz+
LJl6uKFh3klhT+2G9ndOezQlpcMPh/8EOADPZwo+wFRCcKrXoe9ugl/YKb0ERZrn
7ZwEe/uvg1ciW9bCeigni24PMEtiPCd8vDpaSKCkdp7EEvAX/HjaVkUNdALqMl0j
s93UAlICgYEAhQEfTIzZitAyx72z7Bdwtc3FLiPgIGu2vX2mu0xRz4sMHbBOBRi2
a+LFvrwCSfizXO1HBB+giAH2GUFu4BRfxfwHcBRtGtynjMsNlfvU72JBvVVE8puv
BW4A3+5dSUW1skllBuFzkbLQOnhi7SFboSGIXqRSI3NAfutPJ/5Id8MCFQCNVrXo
HcHsd+7aKJql1oW10MHxBQ==
-----END DSA PRIVATE KEY-----
四、分發密鑰(公鑰)
分發的命令格式
ssh-copy-id -i 密鑰名稱 用戶@遠端主機IP地址-------------用于ssh是默認端口
ssh-copy-id -i 密鑰名稱 “-p port 用戶@遠端主機IP地址”--用于ssh非默認端口
[fenfa@Centos ~]$ cd .ssh
[fenfa@Centos .ssh]$ ssh-copy-id -i id_dsa.pub fenfa@192.168.1.3
The authenticity of host '192.168.1.3 (192.168.1.3)' can't be established.
RSA key fingerprint is 86:41:46:5c:d9:e0:98:a5:15:ee:b4:01:a5:37:49:c4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.3' (RSA) to the list of known hosts.
fenfa@192.168.1.3's password:
Now try logging into the machine, with "ssh 'fenfa@192.168.1.3'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
節點服務器測試是否分發成功
[root@localhost ~]# tree /home/fenfa/.ssh/
/home/fenfa/.ssh/
+-- authorized_keys
0 directories, 1 file
分發成功
五、分發數據
手工分發
[fenfa@Centos ~]$ scp -P22 -r -p /tmp/text/ fenfa@192.168.1.3:~
123.txt 100% 0 0.0KB/s 00:00
節點服務器測試
[root@localhost fenfa]# tree /home/fenfa/
/home/fenfa/
+-- text
+-- 123.txt
1 directory, 1 file
腳本分發
可以將命令寫進腳本中進行執行
[fenfa@Centos ~]$ vi fenfa.sh
scp -P22 -r -p /tmp/text/ fenfa@192.168.1.3:/tmp/fenfadir/
[fenfa@Centos ~]$ ./fenfa.sh
123.txt 100% 0 0.0KB/s 00:00
[fenfa@localhost fenfadir]$ pwd
/tmp/fenfadir
[fenfa@localhost fenfadir]$ tree
.
+-- text
+-- 123.txt
1 directory, 1 file
分發成功
如果是生產環境中每天都要分發一些數據到節點服務器,可以將此腳本放進定時任務,每天系統自動執行,不過最好定時執行的時間是服務并發量不高的時候,以免影響正常的業務訪問
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。