SSH 配置生成器

可视化编辑 ~/.ssh/config,给服务器起短别名、配跳板机与端口转发;实时生成配置并检查「首值优先」这类隐蔽陷阱

从场景模板开始

配置段

快速添加 重复选择会继续补充该组下还没有的指令

引入其它文件(Include 支持通配,会写在文件最前面)

实时预览

~/.ssh/config
7 行
# 用法:ssh web1
Host web1
    HostName 203.0.113.10
    User deploy
    Port 22
    IdentityFile ~/.ssh/id_ed25519

配置检查

2 条建议
  • Host web1 的 Port 22 是默认值,可以省略

  • 没有设置 ServerAliveInterval,长时间空闲的连接可能被防火墙掐断

    建议 Host * 里写 ServerAliveInterval 60 与 ServerAliveCountMax 3

生效值推演

相当于本地版的 ssh -G 别名,按「第一个取到的值优先」算出最终结果

输入别名后会列出每个参数最终取到的值和它来自哪一段

配套命令

写入配置文件

mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat > ~/.ssh/config <<'EOF'
# 用法:ssh web1
Host web1
    HostName 203.0.113.10
    User deploy
    Port 22
    IdentityFile ~/.ssh/id_ed25519
EOF
chmod 600 ~/.ssh/config

覆盖式写入;已存在配置请先备份

查看最终生效配置

ssh -G web1

打印 ssh 实际会用到的每一行配置,排障神器

测试连通性

ssh -o ConnectTimeout=5 -o BatchMode=yes web1 'echo ok'

不等待密码输入,5 秒超时

详细排障

ssh -vvv web1

看密钥协商与认证全过程

检查私钥权限

chmod 600 ~/.ssh/id_ed25519 && chmod 644 ~/.ssh/id_ed25519.pub

私钥权限太开放会被 ssh 直接拒绝

常用指令速查

HostName

真实主机

连过去的目标域名或 IP,可用 %h 占位

HostName 192.168.1.10

User

登录用户

远程登录用户名,省略则用当前本地用户

User root

Port

端口

远程 sshd 端口,默认 22

Port 2222

IdentityFile

私钥文件

指定私钥路径,可写多行按顺序尝试

IdentityFile ~/.ssh/id_ed25519

IdentitiesOnly

只用指定密钥

只尝试 IdentityFile 列出的密钥,避免 agent 里密钥太多被拒

IdentitiesOnly yes

AddKeysToAgent

自动加密钥

首次使用后把密钥交给 ssh-agent,省去反复输密码

AddKeysToAgent yes

UseKeychain

钥匙串(macOS)

macOS 上把密钥口令存进系统钥匙串

UseKeychain yes

ProxyJump

跳板机

经由一台(或多台逗号分隔)机器中转,7.3+ 可用

ProxyJump jump@10.0.0.1:22

ProxyCommand

代理命令

自定义中转命令,老版本或需要 nc 时用

ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p

ForwardAgent

Agent 转发

把本地 ssh-agent 带到远程,慎用

ForwardAgent yes

LocalForward

本地端口转发

把远程端口映射到本地(-L)

LocalForward 3306 127.0.0.1:3306

RemoteForward

远程端口转发

把本地端口暴露到远程(-R)

RemoteForward 8080 127.0.0.1:3000

DynamicForward

动态代理

起一个 SOCKS5 代理(-D)

DynamicForward 1080

ServerAliveInterval

保活间隔

每隔多少秒发一次保活包,防断线

ServerAliveInterval 60

ServerAliveCountMax

保活重试

连续多少次没回应就断开

ServerAliveCountMax 3

ConnectTimeout

连接超时

建立 TCP 连接的超时秒数

ConnectTimeout 10

TCPKeepAlive

TCP 保活

TCP 层保活,比应用层保活弱

TCPKeepAlive yes

StrictHostKeyChecking

主机指纹校验

是否确认服务器指纹,防中间人

StrictHostKeyChecking accept-new

UserKnownHostsFile

已知主机文件

指纹记录位置

UserKnownHostsFile ~/.ssh/known_hosts

ControlMaster

连接复用

同一主机复用一条连接,二次连接秒开

ControlMaster auto

ControlPath

复用套接字

复用连接的 socket 文件路径

ControlPath ~/.ssh/cm-%r@%h:%p

ControlPersist

复用保持

主连接在无会话后保持多久

ControlPersist 1h

Compression

压缩

慢网下有收益,快网反而变慢

Compression yes

PreferredAuthentications

认证顺序

优先尝试的认证方式

PreferredAuthentications publickey,password

PubkeyAuthentication

公钥认证

是否允许公钥登录

PubkeyAuthentication yes

PasswordAuthentication

密码认证

是否允许密码登录

PasswordAuthentication no

HostKeyAlgorithms

主机算法

兼容老设备时追加算法

HostKeyAlgorithms +ssh-rsa

PubkeyAcceptedAlgorithms

公钥算法

兼容老设备时追加算法,8.5+ 取代 PubkeyAcceptedKeyTypes

PubkeyAcceptedAlgorithms +ssh-rsa

RequestTTY

强制 TTY

执行远程命令时是否分配终端

RequestTTY force

LogLevel

日志级别

排障时开 DEBUG 看协商过程

LogLevel DEBUG3

Include

包含文件

引入其他配置文件,支持通配

Include ~/.ssh/config.d/*.conf

配置文件怎么用

  1. 把右侧生成的配置保存为 ~/.ssh/config
  2. 执行 chmod 600 ~/.ssh/config,目录保持 700
  3. 私钥同理需要 600,否则 ssh 会拒绝使用
  4. 直接用别名连接:ssh web1,不再记 IP、用户名和端口
  5. 不确定有没有生效,跑 ssh -G web1 看实际结果

越具体越靠前

ssh 对每个参数只取第一个值,具体主机段要放在 Host * 之前

别名随便起

命令行输入什么,Host 就匹配什么,跟真实域名无关

端口默认 22

没改过 sshd 端口就不用写 Port,写了反而冗余

密钥用绝对路径或 ~

IdentityFile 支持 ~,Windows 下请用正斜杠

多台中继用逗号

ProxyJump jump1,jump2 可以串起两级跳板

Include 便于拆分

公司机器多时按项目拆到 config.d/*.conf

使用说明与常见问题

ssh 对每个参数只取「第一个取到的值」。如果文件开头就有 Host * 并且设置了某个参数,后面具体主机段里的同名设置会被忽略。解决办法:把具体的 Host 段放在前面,把 Host * 通用默认放在文件最后。用本站的检查面板可以直接看到被遮挡的参数。