.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   明天有风吹  阅读(14984)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通

导航

+V atob('d2h5X251bGw=')

请备注:from博客园

点击右上角即可分享
微信分享提示