使用SSH密鑰認證
1、加密簡介
一、加密方式
對稱密鑰算法(DES,3DES,AES)
使用相同的密鑰和算法來加解密。
缺點:共享密鑰需雙方互換,密鑰太多,不支持數字簽名和不能否認性。
優勢:速度快,安全,緊湊,長度小於等於8字節
非對稱密鑰算法(RSA,DHKipsec ***,EC)
1.每個用戶進入一個加密系統,都須要產生一對公私密密鑰。
2.公鑰共享給全部人,私鑰需嚴格保密,公鑰沒法推出私鑰。
3.公鑰加密的文件須要私鑰才解密(加密的過程)。
4.私鑰加密的文件須要公鑰才能解密(簽名的過程)。
優勢:安全,加解密雙方不須要交換密鑰,無需擔憂密鑰被截取。
缺點:很是慢,密文會變長
在linux下的密鑰認證就是典型的非對稱加密算法。
二、SSH認證簡介
若是主機A想要經過ssh登陸到主機B,而且想要經過密鑰認證的方式,首先你須要在主機A上生成一對密鑰,將生成的公鑰部分上傳至主機B,此時當主機A發起ssh到主機B時,主機B會將一個隨機數用主機A給他的公鑰來加密,加密後的數據會發送給主機A,主機A 用本身保存的私鑰解密後會獲得這個隨機數,讓後將這個隨機數發送給主機B,主機B發現這個隨機數是正確那麼就表示該認證經過。私鑰必須妥善保存。
2、配置過程
一、生成密鑰對(機器A配置)
[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:
5f:81:36:b6:71:77:c1:26:56:cb:0a:14:fd:64:89:89 root@localhost.localdomain
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
| o+ =o.|
| .E *.*o|
| *.+ Bo.|
| o =.o.o |
| S . .. |
| . . |
| . |
| |
| |
+-----------------+
注:ssh-keygen -t rsa這裏直接回車便可。若是輸入密碼的話,在後面使用密鑰鏈接時也會提示輸入密碼。
二、查看密鑰對權限
[root@localhost ~]# ll -d .ssh/
drwx------. 2 root root 4096 5月 22 07:40 .ssh/ #權限爲700
[root@localhost ~]# ll .ssh/
總用量 12
-rw-r--r--. 1 root root 408 5月 22 07:40 id_rsa.pub #權限爲644
-rw-------. 1 root root 1675 5月 22 07:29 id_rsa #權限爲600
-rw-r--r--. 1 root root 786 5月 22 07:41 known_hosts
三、修改公鑰名稱
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# mv id_rsa.pub authorized_keys
[root@localhost .ssh]# ls
authorized_keys id_rsa known_hosts
注:authorized_keys:與ssh配置文件中AuthorizedKeysFile .ssh/authorized_keys 相對應。
四、修改ssh配置文件
[root@localhost ~]# vim /etc/ssh/sshd_config
RSAAuthentication yes #使用密鑰驗證
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys #驗證文件的名稱
----------------------------可選配置--------------------------------------
Protocol 2 #修改後變爲此狀態,僅使用SSH
ServerKeyBits 1024 #修改後變爲此狀態,將ServerKeyBits改成1024比特
PermitRootLogin no #不容許用Root進行登陸
PasswordAuthentication no #不容許密碼方式的登陸
PermitEmptyPasswords no #禁止空密碼進行登陸
[root@localhost ~]# vi /etc/hosts.deny
sshd: ALL 最後添加這一行,屏蔽來自全部的SSH鏈接請求
[root@localhost ~]#vi /etc/hosts.allow
sshd: 192.168.0.1 最後添加這一行,只容許來自內網的SSH鏈接請求
注:hosts.allow和hosts.deny同時設置時hosts.allow生效
------------------------------------------------------------------------
三、機器B配置與機器A的配置徹底相同
一、生成密鑰對
二、查看密鑰對權限
三、修改公鑰名稱
四、修改ssh配置文件
這裏就再也不多寫了、、、、、
3、公鑰設置
一、公鑰相互對換
將機器A和機器B的公鑰裏面的內容拷貝出來相互對換,這樣作的好處在於不會出現權限問題。
二、啓動服務
[root@localhost ~]#service sshd restart
機器A和機器B都要從新啓動服務
4、測試
一、測試
[root@localhost ~]# ssh root@172.16.0.11
Last login: Tue May 22 07:41:20 2012 from 172.16.0.12
[root@localhost ~]# ssh 172.16.0.12
Last login: Tue May 22 08:13:49 2012 from 172.16.0.11
兩臺機器均可使用密鑰登陸。
若是隻須要機器A可使用密鑰認證登陸機器B能夠將機器B上的密鑰刪除或是備份便可。
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# mv id_rsa id_rsa_bak
[root@localhost .ssh]# ls
authorized_keys id_rsa_bak known_hosts
[root@localhost .ssh]# ssh 172.16.0.11
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).