.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

--------- 2024.5.9 更新 -----------
ssh-keygen -t ed25519 生成密钥,并将公钥 .pub 添加到项目的 Deploy keys
然后就可以 git clone -c "core.sshCommand=ssh -i path/to/your/private/key" git@github.com:user-id/project-name.git
你会发现在 .git/config 里会把这个 sshcommand 记录下来

这样就可以做到每个项目单独配置一个 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:hangj/myrepo.git
$ git remote -v

命令行里的 myproject1 就是 ~/.ssh/config 里的 Host, hangj 是我的用户名

指定 Host,是为了方便管理多个仓库,每个仓库用自己的 deploy key,不会乱

posted on 2019-09-11 15:46  明天有风吹  阅读(14889)  评论(0编辑  收藏  举报

导航

+V atob('d2h5X251bGw=')

请备注:from博客园