SSH客户端~/.ssh/config配置文件格式介绍(Host块、配置项、HostName、Host别名、IdentityFile、ForwardAgent、SSH配置文件、SSH config)
·
SSH客户端~/.ssh/config配置文件格式
~/.ssh/config 文件是SSH客户端配置文件,使用特定的文本格式。以下是其主要格式特点:
基本结构
- Host块:每个配置块以
Host开头,后面跟一个或多个主机名/别名 - 配置项:在Host块内,每行一个配置指令
- 缩进:通常使用空格或制表符进行缩进(非强制,但推荐)
- 注释:以
#开头的行是注释
基本格式示例
# 全局配置(Host * 块)
Host *
ServerAliveInterval 60
TCPKeepAlive yes
# 特定主机配置
Host myserver
HostName server.example.com
User username
Port 22
IdentityFile ~/.ssh/id_rsa
# 另一个主机配置
Host github
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
常用配置指令
HostName- 实际的主机名或IP地址User- 登录用户名Port- SSH端口号IdentityFile- 私钥文件路径ProxyCommand- 代理命令ServerAliveInterval- 保持连接活跃的时间间隔ForwardAgent- 是否转发SSH代理
通配符和模式匹配
# 匹配所有以dev-开头的主机
Host dev-*
User developer
IdentityFile ~/.ssh/dev_key
# 匹配特定域
Host *.example.com
User admin
Port 2222
重要规则
- 匹配顺序:SSH按文件中出现的顺序匹配Host块,第一个匹配的块生效
- Host别名:Host后面的名称是别名,不是实际主机名
- 大小写不敏感:配置指令不区分大小写
- 空格分隔:指令和值之间用空格分隔
- 多值支持:某些指令支持多个值(如IdentityFile)
这个文件权限应该设置为600(chmod 600 ~/.ssh/config),以确保安全性。
更多推荐



所有评论(0)