Skip to content
返回云服务

SSH 客户端配置与跳板机

~/.ssh/config 最佳实践:主机别名、身份选择、跳板机(ProxyJump)、保活和连接多路复用。

VPS 与自托管入门

为什么

结构良好的 ~/.ssh/config 将 'ssh -i ~/.ssh/foo -p 2222 [email protected]' 变为 'ssh db'。ProxyJump 让你通过堡垒机访问私有实例而无需暴露。

ControlMaster 在多个 ssh 会话间多路复用一个 TCP 连接,连接延迟从 1 秒降至 50 毫秒。

何时使用

管理多于一台服务器时使用 SSH 配置。私有子网(数据库、内部 API)仅通过堡垒机可达时用 ProxyJump。

频繁连接(rsync 循环、Ansible)用 ControlMaster 摊销握手成本。

如何做

1. 为每台服务器在 ~/.ssh/config 创建 Host 块。 2. 每个主机设置 HostName、User、Port、IdentityFile。 3. 用 Host 别名(Host web)缩短命令。 4. 为堡垒机后的私有主机添加 ProxyJump。 5. 启用 ControlMaster/ControlPath 实现多路复用。 6. 添加 ServerAliveInterval 检测死连接。 ```bash mkdir -p ~/.ssh && chmod 700 ~/.ssh touch ~/.ssh/config && chmod 600 ~/.ssh/config # 编辑 ~/.ssh/config(见示例) ```

示例

// vps-ssh-config — 配置示例 const config = { provider: "vps" }; 完整的 ~/.ssh/config,含堡垒机、通过 ProxyJump 的私有数据库和多路复用: ```ssh-config # 堡垒机 / 跳板机 Host bastion HostName 203.0.113.10 User ubuntu IdentityFile ~/.ssh/prod_ed25519 # 仅通过堡垒机可达的私有数据库 Host db HostName 10.0.1.50 User postgres ProxyJump bastion IdentityFile ~/.ssh/prod_ed25519 # 所有主机启用多路复用 Host * ControlMaster auto ControlPath ~/.ssh/cm-%r@%h:%p ControlPersist 10m ServerAliveInterval 60 ServerAliveCountMax 3 ``` 现在 'ssh db' 自动通过堡垒机隧道。 ```bash ssh db # 通过 bastion 连接到 10.0.1.50 scp file db:/tmp/ # 同样有效 ```

常见问题

  • ⚠配置权限错误 —— 若 ~/.ssh/config 可被组/世界写入,SSH 拒绝读取;chmod 600 ~/.ssh/config 和 chmod 700 ~/.ssh。
  • ⚠ProxyJump 反复要求堡垒机密码 —— 将堡垒机的 IdentityFile 加入其 Host 块,使跳转使用密钥认证。
  • ⚠ControlMaster 过期套接字阻止新连接 —— rm ~/.ssh/cm-* 或设更短 ControlPersist 超时;套接字在 ControlPersist 后自动清理。

最佳实践

  • ✓使用 ed25519 密钥(ssh-keygen -t ed25519)—— 比 RSA 更短、更快、更安全。
  • ✓有多个密钥时设 IdentitiesOnly yes,使 SSH 仅尝试指定 IdentityFile 而非提供所有密钥(可能触发 max-auth-tries)。
  • ✓用 ProxyJump(非旧版 ProxyCommand)—— 自动处理跳板机自身配置(端口、密钥)且更简单。

常见问题解答

什么是SSH 客户端配置与跳板机?它解决什么问题?

~/.ssh/config 最佳实践:主机别名、身份选择、跳板机(ProxyJump)、保活和连接多路复用。 它可帮助开发者在不自建基础设施的前提下完成部署、存储与扩展,是云服务选型的核心参考。

如何在 5 分钟内上手SSH 客户端配置与跳板机?

两步即可跑通:① 为每台服务器在 ~/.ssh/config 创建 Host 块。;② 每个主机设置 HostName、User、Port、IdentityFile。。建议先用免费套餐验证流程,再接入生产环境。

SSH 客户端配置与跳板机 最容易踩的坑是什么?

最高频的问题是:配置权限错误 —— 若 ~/.ssh/config 可被组/世界写入,SSH 拒绝读取;chmod 600 ~/.ssh/config 和 chmod 700 ~/.ssh。。建议在上线前用 checklist 逐项核对,并在 CI 中加入策略与配置扫描。

SSH 客户端配置与跳板机 的最佳实践有哪些?

SSH 客户端配置与跳板机 的最佳实践包括:使用 ed25519 密钥(ssh-keygen -t ed25519)—— 比 RSA 更短、更快、更安全。;有多个密钥时设 IdentitiesOnly yes,使 SSH 仅尝试指定 IdentityFile 而非提供所有密钥(可能触发 max-auth-tries)。;用 ProxyJump(非旧版 ProxyCommand)—— 自动处理跳板机自身配置(端口、密钥)且更简单。。其中首条「使用 ed25519 密钥(ssh-keygen -t ed25519)—— 比 RSA 更短、更快、更安全。」应作为团队默认规范写入 IaC 模板。

SSH 客户端配置与跳板机 如何控制成本与避免账单意外?

三招防超支:① 为所有资源打 env/team 标签并启用账单告警;② 用预算阈值触发 Slack/邮件通知;③ 定期用 Cost Explorer 审查闲置资源并清理。SSH 客户端配置与跳板机 多数提供免费套餐,先在免费额度内验证再放量。