您好,登錄后才能下訂單哦!
今天小編給大家分享一下SSH無密碼怎么實現(xiàn)安全登錄的相關(guān)知識點,內(nèi)容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
今天我們通過使用ssh-kengen命令生成私鑰&公鑰對,目的:免密碼登錄SSH。其算法有兩種,分別是RSA和DSA。
RSA 是非對稱加密算法,可以用來加密和簽名。
DSA(Digital Signature Algorithm) 只能用來數(shù)字簽名的算法。
以下操作適用于OS:Centos 7、Ubuntu 17,其他系統(tǒng)沒測,理論上都可以使用。
10.10.204.63
10.10.204.64
1.如何生成ssh公鑰
登錄10.10.204.63服務(wù)器生成公私密鑰對:
[root@10-10-204-63 ~]# ssh-keygen -b 4096 -t rsaGenerating 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: SHA256:qLcoj2nSzq6G9ZpFQZ/OFqFT+oBDf3ousHkt82F1/xM root@10-10-204-63.10.10.204.63 The key's randomart image is: +---[RSA 4096]----+ | . . o | | . + = o | | o B = | | . X o | | . o B S . | | .= * . . . E | |.oo.B * . . | |oo+*.O o .. | |o*O+o o .. | +----[SHA256]-----+ 三次回車即可生成 ssh key。
注解:
-b 指定密鑰長度。對于RSA密鑰,最小要求768位,默認是2048位,最長4096字節(jié)。
-t 指定要創(chuàng)建的密鑰類型??梢允褂茫骸眗sa1″(SSH-1) “rsa”(SSH-2) “dsa”(SSH-2)。
2.查看生成的文件
[root@10-10-204-63 ~]# ll .ssh/total 8 -rw------- 1 root root 3243 Nov 25 15:58 id_rsa -rw-r--r-- 1 root root 758 Nov 25 15:58 id_rsa.pub 說明: id_rsa 私鑰 id_rsa.pub 公鑰
3.將公鑰上傳到10.10.204.64
[root@10-10-204-63 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.204.64/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"The authenticity of host '10.10.204.64 (10.10.204.64)' can't be established. ECDSA key fingerprint is SHA256:/YI/L4RT1QH7lkfxMCAkKnvniQslyUl15mOUKUo8K3k. ECDSA key fingerprint is MD5:6d:b6:f3:93:8e:48:53:24:9d:5d:c2:2a:5f:28:f4:d2. Are you sure you want to continue connecting (yes/no)? yes【輸入yes回車】 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /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.10.204.64's password:【輸入服務(wù)器密碼回車】 Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@10.10.204.64'"and check to make sure that only the key(s) you wanted were added.
上傳成功。
4.修改SSH配置文件
登錄10.28.204.64修改,操作如下:
$ vim /etc/ssh/sshd_config 去除以下注釋: RSAAuthentication yes PubkeyAuthentication yes
5.重啟SSH服務(wù)
$ systemctl restart sshd
6.測試免密碼登錄10.10.204.64
[root@10-10-204-63 ~]# ssh 'root@10.10.204.64'Last failed login: Sat Nov 25 16:09:48 CST 2017 from 83.234.149.66 on ssh:notty There was 1 failed login attempt since the last successful login. Last login: Sat Nov 25 15:57:33 2017 from 36.7.69.84 [root@10-10-204-64 ~]#
在不輸入密碼的情況下成功登錄。
登陸成功后,建議在10.10.204.64服務(wù)器上也生成ssh公鑰,并上傳到10.10.204.63服務(wù)器,這樣以來我們就可以相互免密碼SSH登陸。多臺服務(wù)器亦是如此。
7.查看公鑰
[root@10-10-204-64 ~]# ll /root/.ssh/total 8 -rw------- 1 root root 758 Nov 25 16:08 authorized_keys -rw-r--r--. 1 root root 175 Aug 9 09:19 known_hosts
authorized_keys是剛上傳過來的公鑰名稱
8.如果公鑰丟失,可以使用私鑰再次生成公鑰,命令如下:
[root@10-10-204-63 ~]# ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub
以上就是“SSH無密碼怎么實現(xiàn)安全登錄”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。