Centos7 公鑰登陸

2021-09-03 08:30:30 字數 1898 閱讀 6723

1、登陸到伺服器修改sshd_config

登入伺服器,輸入命令vi /etc/ssh/sshd_config修改配置檔案

找到rsaauthentication、pubkeyauthentication、authorizedkeyfiles、strictmodes所在的行,如果被#注釋了,就取消#號,改為如下配置:

rsaauthentication yes

pubkeyauthentication yes

authorizedkeyfiles .ssh/authorized_keys

strictmodes no

這裡解釋一下:

rsaauthentication設定為yes,意思是設定開啟使用rsa演算法的基於rhosts的安全驗證;

pubkeyauthentication設定為yes,意思是設定開啟公鑰驗證;

authorizedkeyfiles後面的目錄,是你上傳的公鑰所儲存的檔案;

strictmodes設定為no,意思是設定關閉ssh在接收登入請求之前先檢查使用者家目錄和rhosts檔案的許可權和所有權。

修改好ssh的配置檔案後儲存並退出,使用systemctl restart sshd.service重啟ssh服務

2、客戶端生成公鑰

首先我們要在我們的mac上電腦上生成公鑰和私鑰,在終端中輸入以下命令:

cd ~/.ssh

首先進入~/.ssh目錄,之後:

ssh-keygen -t rsa

或給你的key取個名字  ssh-keygen -t rsa -c "[email protected]"

之後就可以一路回車,一般都不設定密碼,即可在~/.ssh目錄中生成私鑰檔案(id_rsa)和公鑰檔案(id_rsa.pub)

3、服務端加入客戶端公鑰

copy 客戶端生成好的公鑰檔案  id_rsa.pub  到伺服器的。~/.ssh/  目錄下,並且改名  authorized_keys

如果已經存在authorized_keys 檔案  那麼需要將你的新公鑰追加到後面

cat id_rsa.pub >> authorized_keys

然後刪除id_rsa.pub

然後修改許可權,這部很重要

.ssh目錄許可權一般為755或者700。

authorized_keys許可權一般為644

現在,我們就可以正常的在mac 終端中使用ssh來登入伺服器了,無需輸入密碼。

4、客戶端設定別名

進入客戶端  ~/.ssh    建立config檔案

host hostname1(暫時叫它1號伺服器)

hostname ***.***.***.***

user username

port 埠號

host hostname2(暫時叫它2號伺服器)

hostname ***.***.***.***

user root

然後  ssh hostname1 直接登陸

總結:如果自動登入失敗可以查詢伺服器的日誌  /var/log/secure

一般會出現錯誤:  authentication refused: bad ownership or modes for directory /home/使用者

第一反覆檢查目錄和檔案的許可權是否正確,這也是網上流傳的最多的解決方案,但是我第一次就出現了這個問題,這個方案沒有解決

第二檢查服務的sshd_config配置,

rsaauthentication設定為yes,意思是設定開啟使用rsa演算法的基於rhosts的安全驗證;

pubkeyauthentication設定為yes,意思是設定開啟公鑰驗證;

authorizedkeyfiles後面的目錄,是你上傳的公鑰所儲存的檔案;

strictmodes設定為no,  (我就是掉到這個坑里的)

redhat 使用公鑰登陸

本文以securecrt 為例給出公鑰登陸的過程及原理。過程 引用自 1 securecrt設定 quick connect authentiation public key properties create identity file dsa rsa set passphrase done 完成...

SecureCRT公鑰登陸配置

建立公鑰 生存公鑰名字可以自定義生成的.pub為公鑰 我這裡設定的是普通使用者lijq使用的ssh2協議,在linux伺服器執行如下操作lijq 192 168 2 222 mkdir home lijq ssh 將剛才生成的.pub檔案上傳到伺服器,我這裡已經root連線上了所有直接用rz上傳即可...

centos7修改埠登陸

1 安裝ssh服務 yum install y openssh server 2 修改預設埠並儲存 vim etc ssh sshd config 3 開啟防火牆 service firewalld start 4 開放指定埠 firewall cmd zone public add port 19...