前言

由於工作的需要
我經常要在 Windows 和 macOS 這兩個系統之間遊走
這使我不得不去研究有什麼方法去同步這兩個系統的 SSH connection

結果一通研究下來
還得是 SSH config 最方便靠譜啊

在認識它之前
我會覺得它非常複雜可怕
但略為有一點了解之後
就會覺得非常簡單易用的好吧

那麼我們就一起來看一看 ssh config 是怎麼用的吧!


什麼是 SSH config

SSH config 就是 SSH client side 的純文字配置文件
你可以在這裏設置自定義主機別名、指定用戶名、端口、密鑰(key)文件路徑等
從而簡化 SSH 命令

只要你把這個配置文件命名為 config
然後把它放到 ~/.ssh/
它就會自動生效

~ 的意思就是你的用戶資料夾
Linux 和 Mac 直接用 ~ 就可以了
它代表的路徑是 /home/<your_user_name>
Windows 的話就是要把 ~ 換為 C:\Users\<your_user_name>

也就是說
設置檔的路徑會變成這樣
Mac / Linux:
~/.ssh/config
或是
/home/<your_user_name>/.ssh/config

 
Windows:
C:\Users\<your_user_name>\.ssh\config

 

SSH config 的基本語法

設置文檔由多個主機配置塊組成
每個配置塊長得像是這樣

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Host example
HostName example.com
User alice
IdentityFile ~/.ssh/id_rsa_example
Port 22
Host example HostName example.com User alice IdentityFile ~/.ssh/id_rsa_example Port 22
Host example
    HostName example.com
    User alice
    IdentityFile ~/.ssh/id_rsa_example
    Port 22

以這個為例子
我們來說一下 ssh config 的常見配置項吧

Host 是每個配置的開首
後面跟着的是主機的別名
之後可以用這個別名直接連線到主機

除了 Host 以外的配置都要縮排一行
HostName 是主機名或 IP 地址
User 是登錄時的用戶名
IdentityFile 是 private key 檔案的路徑
Port 是 SSH 服務器的端口 (非必須,預設為 22)

在配置完成後
你就可以用這句 command 以 SSH 連線到 example.com

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh example
ssh example
ssh example

效果跟下面這句 command 相等

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh -i ~/.ssh/id_rsa_example -p 22 alice@example.com
ssh -i ~/.ssh/id_rsa_example -p 22 alice@example.com
ssh -i ~/.ssh/id_rsa_example -p 22 alice@example.com

看吧
是不是很方便呢~~

要注意的是
在 Windows 的 ssh config 檔案也可以使用 ~/
也就是說
Windows 所使用的 ssh config 檔案
和 Linux 或 macOS 所使用的 ssh config 檔案是可以完全一樣

 

設置 Jumping host

在某些場合還是免不了要使用 Jumping host
不過請大家放心
SSH config 要設置 jumping host 也都是很簡單的
只要先設置好 jumping host 的連接
然後再設置使用 ProxyJump 把它設置為 jumping host 即可

下面就是經由 jump.host 連接到 internal.server 的例子

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 配置 internal.server,通過 jump.host 連接
Host internal
HostName internal.server
User bob
ProxyJump jump.host
IdentityFile ~/.ssh/id_rsa_internal
Port 22
# 配置 jump.host
Host jump.host
HostName jump.host
User bob
IdentityFile ~/.ssh/id_rsa_jump
Port 22
# 配置 internal.server,通過 jump.host 連接 Host internal HostName internal.server User bob ProxyJump jump.host IdentityFile ~/.ssh/id_rsa_internal Port 22 # 配置 jump.host Host jump.host HostName jump.host User bob IdentityFile ~/.ssh/id_rsa_jump Port 22
# 配置 internal.server,通過 jump.host 連接
Host internal
    HostName internal.server
    User bob
    ProxyJump jump.host
    IdentityFile ~/.ssh/id_rsa_internal
    Port 22

# 配置 jump.host
Host jump.host
    HostName jump.host
    User bob
    IdentityFile ~/.ssh/id_rsa_jump
    Port 22

然後使用這句 command 連接到 internal.server

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh internal
ssh internal
ssh internal

怎樣
非常簡單對吧 ( •̀ ω •́ )✧

 

不過如果你使用的 SSH 版本比較舊 (<7.3) 的話
就要使用 ProxyCommand 了 (新版本 SSH 也能兼容)
下面這個例子的效果跟上面的一樣

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 跳板機配置
Host jump.host
HostName jump.host
User bob
IdentityFile ~/.ssh/id_rsa_jump
# 目標主機配置,通過跳板機連接
Host internal
HostName internal.server
User alice
IdentityFile ~/.ssh/id_rsa_internal
ProxyCommand ssh -W %h:%p jump.host
# 跳板機配置 Host jump.host HostName jump.host User bob IdentityFile ~/.ssh/id_rsa_jump # 目標主機配置,通過跳板機連接 Host internal HostName internal.server User alice IdentityFile ~/.ssh/id_rsa_internal ProxyCommand ssh -W %h:%p jump.host
# 跳板機配置
Host jump.host
    HostName jump.host
    User bob
    IdentityFile ~/.ssh/id_rsa_jump

# 目標主機配置,通過跳板機連接
Host internal
    HostName internal.server
    User alice
    IdentityFile ~/.ssh/id_rsa_internal
    ProxyCommand ssh -W %h:%p jump.host

其等價的 SSH command 長成這樣

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh -i ~/.ssh/id_rsa_internal -p 22 -o "ProxyCommand ssh -i ~/.ssh/id_rsa_jump -W %h:%p -p 22 bob@jump.host" bob@internal.server
ssh -i ~/.ssh/id_rsa_internal -p 22 -o "ProxyCommand ssh -i ~/.ssh/id_rsa_jump -W %h:%p -p 22 bob@jump.host" bob@internal.server
ssh -i ~/.ssh/id_rsa_internal -p 22 -o "ProxyCommand ssh -i ~/.ssh/id_rsa_jump -W %h:%p -p 22 bob@jump.host" bob@internal.server

如果你想使用 ProxyJump 而不是 ProxyCommand 的話
那就需要先在 SSH config 中設置 jumping host 的身份認證(如有)了
其 SSH command 長得像這樣

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh -v -i ~/.ssh/id_rsa_internal -o "ProxyJump=bob@jump.host" alice@internal.server
ssh -v -i ~/.ssh/id_rsa_internal -o "ProxyJump=bob@jump.host" alice@internal.server
ssh -v -i ~/.ssh/id_rsa_internal -o "ProxyJump=bob@jump.host" alice@internal.server

反正都是要設置 SSH config
那就不如直接在 SSH config 把所有需要的設置都一次弄好吧


共用用戶設置 (使用 Match)

如果你有很多 SSH connection 都使用相同的 user
Match 可就能幫上大忙了

我們先像這樣設置好用戶

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Match User alice
IdentityFile ~/.ssh/id_rsa_alice
Match User alice IdentityFile ~/.ssh/id_rsa_alice
Match User alice
    IdentityFile ~/.ssh/id_rsa_alice

然後在以 alice 為用戶時
就會自動使用 ~/.ssh/id_rsa_alice 這個 key file 了
下面就是連接到 example1example2 的例子

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Match User alice
IdentityFile ~/.ssh/id_rsa_alice
Host example1
HostName 1.example.com
User alice
Host example2
HostName 2.example.com
User alice
Match User alice IdentityFile ~/.ssh/id_rsa_alice Host example1 HostName 1.example.com User alice Host example2 HostName 2.example.com User alice
Match User alice
    IdentityFile ~/.ssh/id_rsa_alice
Host example1
    HostName 1.example.com
    User alice
Host example2
    HostName 2.example.com
    User alice

此外
你還可以讓使用同一個 root domain 的
不同伺服器的連接都使用相同的身份

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Match Host *.example.com
User alice
IdentityFile ~/.ssh/id_rsa_example
Host example1
HostName 1.example.com
Host example2
HostName 2.example.com
Match Host *.example.com User alice IdentityFile ~/.ssh/id_rsa_example Host example1 HostName 1.example.com Host example2 HostName 2.example.com
Match Host *.example.com
    User alice
    IdentityFile ~/.ssh/id_rsa_example
Host example1
    HostName 1.example.com
Host example2
    HostName 2.example.com

Match 還有很多細節和進階用法
我們改天再在另一篇文章再展開吧~


連接到 SFTP

既然都能夠連接到 SSH 了
那基於 SSH 的 SFTP 自然不在話下了

SSH Config 也會同時應用到 sftp 指令
不過我還是喜歡通過 GUI 來使用 SFTP
Cyberduck 就是其中一個可以使用 SSH config 的 SFTP GUI 軟件
我之後還會再開一篇文章說說的
敬請期待~


跨平台同步

正如前文所說
Windows 和 Linux 或 macOS 的 SSH config 檔案是可以完全一樣的
而且它是普通的文字檔

這就非常適合使用 git 來進行同步處理了

~/.ssh 作為 git repository 的 root directory
把 SSH config 和 key files 都放到 ~/.ssh
然後一起 push 到 git repository
(( 記得要先把 git repository 設置為 private
(( 以及用強密碼作為你的 key file 的 passphrase
再配合使用 password manager 來記住 passphrase

一個簡單好用的跨平台 SSH 同步系統就完成了
(~ ̄▽ ̄)~
用過的都說好啊😘


小結

其實我們只觸及到 SSH config 的一小部份
它還有很多進階用法是這篇文章沒有提及的
不過這也能夠滿足 99% 的一般使用需求了吧

像我這樣需要跨平台的用戶也被好好的滿足了
((如果你是要用電話或平板來進行 SSH 連接的當我沒說

設置 SSH 對新手來說一直都是麻煩事
SSH config 看起來很可怕
不過用着用着反倒是覺得比 GUI 更方便好用
希望這篇簡單的入門文章能幫助各位看客享受 SSH 帶來的樂趣🤭


發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


Trending