一、安裝OpenSSH伺服器
1、使用root用戶登錄到CentOS 7伺服器上。
$ ssh root@your_server_ip_address
2、使用以下命令安裝OpenSSH伺服器:
$ yum install openssh-server
3、啟動OpenSSH伺服器並將其設置為在系統啟動時自動啟動:
$ systemctl start sshd $ systemctl enable sshd
二、生成公鑰和私鑰
1、使用以下命令生成公鑰和私鑰:
$ ssh-keygen
2、按Enter鍵直接使用默認設置:
Generating 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:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx root@your_server_ip_address The key's randomart image is: +---[RSA 2048]----+ | | | . . o | | o. . o . | | .oo.. | | ..+S.+o | | .ooo=o*B.| | .o*.+==O= | | .. E=..=XO.| | . .ooo*=.| +----[SHA256]-----+
三、將公鑰添加到另一台CentOS伺服器上的authorized_keys文件
1、使用以下命令將公鑰添加到authorized_keys文件中:
$ cat ~/.ssh/id_rsa.pub | ssh user@your_server_ip_address "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
2、把用戶名和目標伺服器IP地址替換成你真實的用戶名和伺服器IP地址。
四、測試免密登錄
1、使用以下命令嘗試登錄到目標伺服器上:
$ ssh user@your_server_ip_address
2、如果成功登錄到目標伺服器,則表明CentOS 7免密登錄已經成功。
五、禁用密碼登錄(可選)
1、由於密碼登錄方式具有很大的安全風險,建議禁用密碼登錄方式,只允許公鑰登錄方式。
2、編輯SSH配置文件:
$ sudo vi /etc/ssh/sshd_config
3、找到以下行:
#PasswordAuthentication yes
4、將其改為:
PasswordAuthentication no
5、保存並退出文件。
6、重啟SSH服務:
$ sudo systemctl reload sshd
7、現在只能使用公鑰登錄方式來登錄伺服器。
六、總結
以上就是CentOS 7免密登錄的教程。通過免密登錄,可以方便快捷地遠程連接伺服器,並且更安全,強烈推薦使用。如果您想加強安全性,可以禁用密碼登錄方式,只允許公鑰登錄方式。希望這篇教程對您有所幫助。
原創文章,作者:CIVX,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/144315.html