ssh config

SSH 客户端配置文件(~/.ssh/config),为不同主机设置连接参数,简化 SSH 命令

语法

Host <alias>
  HostName <hostname>
  User <username>
  Port <port>
  IdentityFile <key_path>

参数

参数说明示例级别
Host 主机别名,支持通配符 * Host myserver 常用
HostName 实际主机名或 IP 地址 HostName 192.168.1.100 常用
User 登录用户名 User deploy 常用
Port SSH 端口号 Port 2222 常用
IdentityFile 私钥文件路径 IdentityFile ~/.ssh/work_key 常用
ProxyJump 跳板机配置 ProxyJump bastion 进阶

示例

基本主机配置

Host prod
  HostName production.example.com
  User deploy
  Port 2222
  IdentityFile ~/.ssh/prod_key
配置后直接 ssh prod 即可连接

GitHub 多账号配置

Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/work_key

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/personal_key
用 git clone git@github-work:org/repo.git 区分账号

通配符配置

Host 10.0.0.*
  User admin
  IdentityFile ~/.ssh/internal_key
  StrictHostKeyChecking no
对内网所有机器使用相同配置

保持连接活跃

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 3
每 60 秒发送心跳,防止连接断开

常见错误

Bad configuration option 检查配置项拼写是否正确,注意大小写敏感
配置不生效 检查文件权限(chmod 600 ~/.ssh/config),确认 Host 别名匹配正确

技巧

相关命令