天天看點

Mac使用秘鑰登入Linux伺服器

簡介

在 Mac 上配置 SSH 密鑰登入遠端的 Linux

相關配置

1.建立本地的 SSH 密鑰

本地 生成秘鑰對

ssh-keygen -t rsa -C '[email protected]'
-t 指定密鑰類型,預設即 rsa
-C 設定注釋文字,比如你的郵箱      

可以設定 私鑰密碼,我這裡設定的密碼為 12345

Mac使用秘鑰登入Linux伺服器

生成的密鑰預設在 家目錄 下的 .ssh 目錄下

Mac使用秘鑰登入Linux伺服器

2.上傳 公鑰到遠端 Linux 伺服器

使用 scp 複制公鑰到遠端伺服器

scp -P <端口号> ~/.ssh/id_rsa.pub <使用者名>@<ip位址>:/home/id_rsa.pub

我這裡使用的 root 使用者上傳,需要輸入登入密碼

Mac使用秘鑰登入Linux伺服器

配置遠端 Linux 的私鑰

3.登入 遠端 Linux 伺服器,把公鑰追加到伺服器 ssh 認證檔案中:

cat /home/id_rsa.pub >> ~/.ssh/authorized_keys      

如果在 家目錄 沒有 .ssh 目錄或 authorized_keys 檔案,可以建立一下,并授予 authorized_keys 檔案 600 權限

Mac使用秘鑰登入Linux伺服器

然後在執行 

cat /home/id_rsa.pub >> ~/.ssh/authorized_keys      

4.本地 ssh 連接配接

ssh -p <端口号> <使用者名>@<ip位址>

5.如果沒有修改預設端口,可以忽略端口号

ssh [email protected]      

建立配置檔案快速登入

每次登入都需要輸入使用者和 ip 位址,這樣也是太麻煩,可以添加配置檔案,使用 别名 來登入

vi ~/.ssh/config      
Host            alias            #自定義别名
HostName        114.11.11.110         #替換為你的ssh伺服器ip或domain
Port            22             #ssh伺服器端口,預設為22
User            root             #ssh伺服器使用者名
IdentityFile    ~/.ssh/id_rsa    #第一個步驟生成的公鑰檔案對應的私鑰檔案      
Mac使用秘鑰登入Linux伺服器

此時就可以使用​

​ ssh jd ​

​進行登入

禁止 Linux 使用賬号密碼登入

1.

cd /etc/ssh/      

2.修改 SSH 的配置檔案​

​ vi sshd_config​

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
#AuthorizedKeysCommand none
#AuthorizedKeysCommandRunAs nobody
#預設PasswordAuthentication 為yes,即允許密碼登入,改為no後,禁止密碼登入
PasswordAuthentication no      

3.重新開機 ssh 服務

systemctl restart sshd.service      

非學,無以緻疑;非問,無以廣識

繼續閱讀