直接在本地电脑的~/.ssh/config文件中配置Host块即可实现ssh别名直连,无需额外工具;该文件为OpenSSH原生支持,Linux/macOS路径为~/.ssh/config,Windows为$env:USERPROFILE.sshconfig,需设chmod 600权限;每个Host块定义别名、HostName、User、Port及IdentityFile等参数,支持ProxyJump跳板、通配符匹配和全局Host *配置。
直接在本地电脑的 ~/.ssh/config 文件里写一个 Host 块就行,不用装额外工具,SSH 自带支持。
Linux/macOS 下路径是 ~/.ssh/config,如果文件不存在就新建一个。Windows(PowerShell)用 notepad $env:USERPROFILE.sshconfig 打开。
写完后记得设权限,避免 SSH 拒绝读取:
chmod 600 ~/.ssh/config每个服务器对应一个 Host 块,结构清晰、一行一配置:
myserver 或 gpu-train
192.168.1.100)或域名(如 server.example.com)ubuntu、root 或 git
~/.ssh/id_ed25519)示例:
# 生产服务器Host prodHostName 203.0.113.5User adminPort 2222IdentityFile ~/.ssh/prod_keyGitHub 工作账号
Host github-work HostName github.com User git IdentityFile ~/.ssh/id_rsa_work
除了登录简化,还能加些实用功能:
ServerAliveInterval 60 和 ServerAliveCountMax 3,每分钟发一次心跳ProxyJump bastion,先连跳板再进目标机器Host dev-* 统一指定开发机的用户和密钥Host * 块,设置所有连接共用的保活或转发选项保存 config 后,终端直接运行:
ssh prod → 自动连到 203.0.113.5:2222,用 admin 用户和 prod_key 密钥scp file.txt github-work:user/repo/ → 同样走 github-work 别名配置如果报错,常见原因是权限不对、Host 名拼错、或者私钥路径不正确,可以用 ssh -v prod 查看详细连接过程。