本地CentOS8 配置ssh免密登錄服務器


准備工作:
1、確認本機sshd的配置文件(需要root權限)

  $ vi /etc/ssh/sshd_config
  

  找到以下內容,並去掉注釋符”#“
  AuthorizedKeysFile .ssh/authorized_keys (網上教程這么寫的,然而好像的默認也沒注釋,而且注釋的許多也是默認開啟的)

找到此文解答了些疑惑:https://www.cnblogs.com/Leroscox/p/9627809.html
  

  2、如果修改了配置文件需要重啟sshd服務 (需要root權限)
 
  $ systemctl restart sshd
  

配置SSH無密碼登錄需要3步:
1.生成公鑰和私鑰
2.導入公鑰到認證文件,更改權限
3.測試

1.生成公鑰和私鑰 (這里不需要root權限,使用的賬戶執行)

ssh-keygen -t rsa (我直接Enter,簡單粗暴)

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/mycent/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/mycent/.ssh/id_rsa.
Your public key has been saved in /home/mycent/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:vQUMp+pZITtKIYynLJ/fYuR5nqPQkfijmWOBpvucCVY xxxxx@xxxxxxxx
The key's randomart image is:
+---[RSA 3072]----+
|        . .      |
| o       =       |
|. + . . o o      |
|.o o o + o .     |
|oo.E+ + S . .    |
|ooo=.+ +   o     |
|+.++=.o   .      |
|oooO=oo.         |
|.+Ooo=+.         |
+----[SHA256]-----+

 

默認在 ~/.ssh目錄生成兩個文件:

id_rsa :私鑰
id_rsa.pub :公鑰

2.導入公鑰到認證文件,更改權限
2.1 導入本機
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

2.2 導入要免密碼登錄的服務器
首先將公鑰復制到服務器
scp ~/.ssh/id_rsa.pub xxx@host:/home/id_rsa.pub
然后,將公鑰導入到認證文件(這一步的操作在服務器上進行)
cat /home/id_rsa.pub >> ~/.ssh/authorized_keys

2.3 在服務器上更改權限 (很重要,如果不這么設置,就是不讓你免密登錄)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

最后在本地登錄服務器

ssh -v hostname@hostip


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM