不用每次都 -i 指定密鑰,且避免連接自動斷開
ControlMaster auto
ControlPath ~/.ssh/connection-%r@%h:%p
ControlPersist 4h
ServerAliveInterval 6
ServerAliveCountMax 20
Compression yes
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github
Host hangj
Hostname mydomain.name
User hangj
IdentityFile ~/.ssh/hk
然后 ssh hangj 就可以連上我的服務器
拷貝文件
scp file.txt hangj:~/
scp hangj:~/file.txt ./
簡單,高效,省心,關鍵還防禿~~~
管理多個 github 的 deploy key
在 GitHub 倉庫設置好 webhooks,當有新的 push 時,自動發一個 POST 到我的 web 服務器
然后 web 服務器調用一個 shell 腳本自動 pull 最新代碼,並重啟服務
服務器上只需要 pull 代碼,所以沒必要專門給它配置一個 robot 用戶,這個時候可以用 deploy key,給這個 key 設置好 pull 的權限
把 key 文件保存好,然后在 ~/.ssh/config 文件加上
Host myproject1
HostName github.com
User git
IdentityFile ~/.ssh/github_deploy_myproject1
然后到你的倉庫目錄下(hangj 是我的用戶名)
$ git remote set-url origin git@myproject1:hangjr/myrepo.git
$ git remote -v
命令行里的 myproject1 就是 ~/.ssh/config 里的 Host, hangj 是我的用戶名
指定 Host,是為了方便管理多個倉庫,每個倉庫用自己的 deploy key,不會亂
