Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

用 ~/.ssh/config 為不同站台指定各自的 SSH 金鑰

手上同時管理好幾台伺服器或幾個 Git 代碼託管平台,如果都用同一組金鑰,一旦外洩就是全面淪陷。做法是為每個站台各產生一組金鑰,再透過 config 檔自動配對。

一、建立不同的金鑰

ssh-keygen -t dsa -C "email.xxx" -f ~/.ssh/xxx

-t 指定加密方法(RSA 或 DSA),-C 是註解,-f 指定檔案名稱。這條指令會在 ~/.ssh/ 下產生 xxx 與 xxx.pub 一組金鑰對。

二、編輯 ~/.ssh/config

設定格式如下:

Host github.com www.github.com
  IdentityFile ~/.ssh/code_github

Host bitbucket.org www.bitbucket.org
  IdentityFile ~/.ssh/code_bitbucket

執行 man ssh_config 有更多參數說明。

三、把公鑰交給對方

將 xxx.pub 的內容加入目標伺服器該使用者下的 ~/.ssh/authorized_keys。

四、驗證

之後用 ssh 登入時,SSH 會依 Host 自動判斷該用哪一組金鑰認證,不需手動指定 -i。

幾個實務補充:config 檔的權限建議設為 600,私鑰也是 600、公鑰 644;Host 支援萬用字元,例如 Host *.internal.example.com 可一次套用到整批主機;若想為同一主機準備多組金鑰並依序嘗試,可以列多個 IdentityFile,再加上 IdentitiesOnly yes 避免 ssh 把 agent 裡其他金鑰全部試一遍而觸發伺服器端的嘗試次數上限。

Copyright © Linux教程網 All Rights Reserved