.ssh/config 常用配置

不用每次都 -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,不会乱

原文地址:https://www.cnblogs.com/hangj/p/11506686.html