SSH建立金鑰對

2022-08-30 18:36:14 字數 1423 閱讀 1226

a伺服器登入到b伺服器,但為了安全起見,不想輸入b伺服器的使用者帳號密碼,該如何實現?

可以使用金鑰對驗證,實現在a伺服器無需b伺服器的使用者帳號密碼即可登入到b伺服器。

登入驗證物件:伺服器中的本地使用者帳號

登入驗證方式:

密碼驗證:核對使用者名稱、密碼是否匹配

金鑰對驗證:核對客戶的私鑰、服務端公鑰是否匹配

#vim /etc/ssh/sshd_config

passwordauthentication yes   // 密碼驗證

pubkeyauthentication yes   //金鑰對驗證

authorizedkeysfile .ssh/authorized_keys  // 金鑰儲存地方

#systemctl restart ssh.service

構建金鑰對驗證的ssh體系:

第一步:建立金鑰對(在客戶端建立,建立後包含私鑰和公鑰,形成乙個金鑰對)

私鑰檔案:id_rsa

公鑰檔案:id_rsa.pub

#ssh-keygen -t rsa

// 設定金鑰儲存位置

// 設定金鑰口令

【私鑰許可權為:600

公鑰許可權為:644

此加密方式為非對稱加密,私鑰檔案一定不能公開】

第二步:上傳公鑰檔案到服務端

第三步:匯入公鑰檔案

以下命令把第二步和第三步合成一起實現上傳並匯入:

# ssh-copy-id remote_user_name@remote_ip_address

例子:匯入公鑰檔案前,使用guest使用者切換到student使用者:

[root@localhost ~]# ssh [email protected]

[email protected]'s password:

開始上傳並匯入公鑰檔案:

使用guest使用者上傳並匯入公鑰檔案到student使用者,上傳並匯入成功後,使用guest使用者切換到student使用者,無需student的密碼即可登入到student(如果guest使用者建立金鑰對時候建立了密碼,則需輸入金鑰口令):

[root@localhost ~]# ssh-copy-id [email protected]

// 輸入student的密碼

// 上傳並匯入成功,在student使用者的 ~/.ssh 目錄下有乙個檔案 authorized_keys ,即上傳並匯入後的公鑰檔案

// 使用guest使用者切換到user001,無需user001的密碼即可登入

[root@localhost ~]# ssh [email protected]

enter passphrase for key '/home/guest/.ssh/id_rsa':

Linux 建立ssh金鑰對

先檢查一下伺服器的ssh配置檔案 etc ssh sshd config rsaauthentication yes 啟用 rsa 認證 預設為 yes pubkeyauthentication yes 啟用公鑰認證,預設為yes 執行命令建立公私鑰 ssh keygen t rsa 檢視建立好對公...

建立SSH金鑰對並匯入

在使用者主目錄下,看看有沒有.ssh目錄,如果有,再看看這個目錄下有沒有 id rsa 和 id rsa.pub 這兩個檔案如果已經有了,可直接跳到下一步如果沒有,開啟shell windows下開啟git bash 建立ssh key ssh keygen t rsa c youremail ex...

建立SSH金鑰

ssh 無密碼登入要使用公鑰與私鑰。linux下可以用用ssh keygen生成公鑰 私鑰對,伺服器為centos,客戶端為ubuntu centos的ip位址192.168.1.240 1.在ubuntu下生成公鑰 私鑰對。chenlb a ssh keygen t rsa p p表示密碼,p 就...