新聞中心
在我們平時(shí)使用Linux系統(tǒng)時(shí)候,通常使用的Linux SSH登錄方式是用戶名加密碼的登錄方式,今天來探討另外的一種相對(duì)安全的登錄方式——密鑰登錄。

創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、鐵嶺網(wǎng)絡(luò)推廣、微信小程序定制開發(fā)、鐵嶺網(wǎng)絡(luò)營銷、鐵嶺企業(yè)策劃、鐵嶺品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供鐵嶺建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
環(huán)境
客戶端:CentOS8 192.168.43.137
服務(wù)端:CentOS8 192.168.43.139
創(chuàng)建SSH公私鑰
通過輸入以下命令,生成新的4096位的SSH密鑰對(duì):
[root@localhost ~]# ssh-keygen -t rsa -b 4096
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:
SHA256:ycOtSDK8ud2kd6EH7OxoQuc1BFb1HJ3T/kvAQJt0LrI root@localhost.localdomain
The key's randomart image is: +---[RSA 4096]----+ | ...oo.o o | | o .+=.+ .| | . . . +=. o | | . o.oo .o .| | + .oSE. . .| | .*..=o. ..| | .oo.+o+ . . .| | .oo== o . | | .o+ooo | +----[SHA256]-----+
想要驗(yàn)證是否生成了新的SSH密鑰對(duì),使用ls -l命令查看~/.ssh目錄是否有剛才生成的文件:
[root@localhost ~]# ll ~/.ssh/
total 8
-rw------- 1 root root 3389 May 13 08:26 id_rsa
-rw-r--r-- 1 root root 752 May 13 08:26 id_rsa.pub
將公鑰復(fù)制到遠(yuǎn)程服務(wù)器,使用ssh-copy-id實(shí)用程序,輸入遠(yuǎn)程服務(wù)器的root密碼:
[root@localhost ~]# ssh-copy-id root@192.168.43.139
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.43.139 (192.168.43.139)' can't be established. ECDSA key fingerprint is SHA256:7O1oIOooh4NZG87aC3v1Zz/vcTXkjOhQBnlkY0CD4y0. Are you sure you want to continue connecting (yes/no)? 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 Password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.43.139'" and check to make sure that only the key(s) you wanted were added.
也可以使用以下命令復(fù)制公鑰:
[root@localhost .ssh]# cat ~/.ssh/id_rsa.pub | ssh root@192.168.43.139 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
使用密鑰登錄服務(wù)器
使用以下命令登錄ssh服務(wù)器:
[root@localhost ~]# ssh 192.168.43.139
Last login: Tue May 12 12:33:41 2020 from 192.168.43.137
在CentOS8中設(shè)置SSH密鑰在CentOS8中設(shè)置SSH密鑰
關(guān)閉密碼認(rèn)證
登錄服務(wù)器端,關(guān)閉密碼認(rèn)證:
[root@localhost ~]# ssh 192.168.43.139
Last login: Tue May 12 12:33:41 2020 from 192.168.43.137
[root@localhost ~]# vim /etc/ssh/sshd_config
搜索一下三條,將選項(xiàng)改為No
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
重啟sshd服務(wù):
[root@localhost ~]# systemctl restart sshd
總結(jié)
可以使用同一密鑰管理多個(gè)遠(yuǎn)程服務(wù)器。默認(rèn)情況下,SSH的端口是TCP 22。更改默認(rèn)SSH端口可降低自動(dòng)攻擊的風(fēng)險(xiǎn)。
分享名稱:CentOS8中如何設(shè)置SSH密鑰
轉(zhuǎn)載注明:http://www.5511xx.com/article/dhehgjd.html


咨詢
建站咨詢
