歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux服務 >> 通過SSH進行密鑰認證自動登錄Linux服務器

通過SSH進行密鑰認證自動登錄Linux服務器

日期:2017/3/1 17:38:07   编辑:Linux服務

傳統的網絡服務程序,SSH的英文全稱是 Secure Shell,通過使用ssh,可以對所有的傳輸的數據進行加密,這樣既可以防止攻擊又可以防止IP欺騙。

SSH 提供2種級別的安全驗證

1,基於口令的安全驗證,這也是我們常用的一種,只要知道用戶名和密碼,就可以遠程登陸到遠程主機上。

2,基於密鑰的安全認證,就是說用戶必須為自己創建一對密鑰,並把公用密鑰放到需要訪問的服務器上。

2種安全級別的驗證,後者相對比前者更安全一些,第二種級別不需要在網絡上傳遞口令。

SSH密鑰認證登錄配置

原理:用戶首先需要為自己創建一對密鑰:公鑰(用在登錄的服務器上)和私鑰。OPENSSH 公開的密鑰的密碼體質有RSA,DSA等,這裡就用RSA。

客戶端ip:192.168.72.11

服務器ip:192.168.72.129

密鑰認證的生成


復制代碼代碼如下:[root@xyly ~]# ifconfig eth0 | awk ‘/inet addr/{print }’
inet addr:192.168.72.11 Bcast:192.168.72.255 Mask:255.255.255.0</p> <p>[root@xyly ~]# 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:
04:c5:7a:57:f6:2e:9c:1f:b5:e7:45:b3:11:f3:c7:18 root@xyly

公鑰已經生成

現在把公鑰上傳到另一台服務器上去。


復制代碼代碼如下:[root@xyly ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
The authenticity of host ’192.168.72.129 (192.168.72.129)’ can’t be established.
RSA key fingerp
rint is 3b:26:19:2e:51:ca:cc:de:ac:bc:00:09:f0:7c:7d:f1.
Are you sure you want to continue connecting (yes/no)? yes #由於是第一次登錄,服務器要進行確認
Warning: Permanently added ’192.168.72.129′ (RSA) to the list of known hosts.
Address 192.168.72.129 maps to localhost, but this does not map back to the address – POSSIBLE BREAK-IN ATTEMPT!
[email protected]′s password:
Now try logging into the machine, with “ssh ‘[email protected]′“, and check in:</p> <p>.ssh/authorized_keys #把公鑰上傳的位置和公鑰的文件名</p> <p>to make sure we haven’t added extra keys that you weren’t expecting.

登錄到服務器上,查看公鑰是否上傳


復制代碼代碼如下:[root@localhost ~]# ll .ssh/authorized_keys
-rw——- 1 root root 391 Aug 4 18:31 .ssh/authorized_keys

說明公鑰上傳成功了。

修改ssh配置文件,設置公鑰認證登錄

將下邊2行的注釋去掉,重啟ssh服務


復制代碼代碼如下:RSAAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys</p> <p>/etc/init.d/sshd restart

配置完畢,現在開始登錄一下


復制代碼代碼如下:[root@xyly ~]# ssh 192.168.72.129
Address 192.168.72.129 maps to localhost, but this does not map back to the address – POSSIBLE BREAK-IN ATTEMPT!
Last login: Thu Aug 4 18:08:05 2011 from 192.168.72.1
[root@localhost ~]#

現在不用輸入密碼,就可以登錄了。退出登錄


復制代碼代碼如下:[root@localhost ~]# exit
logout
Connection to 192.168.72.129 closed.

linux ssh 密鑰認證無需輸入密碼即可登錄,在設置密鑰的同時,也可以輸入密碼,即可密碼+密鑰認證就可以完成!

Copyright © Linux教程網 All Rights Reserved