在 ~/.ssh/config 里配置 ssh 主机别名

把很长的 ssh deploy@203.0.113.10 -p 2222 变成一句 ssh web。

LinuxSSH 远程连接配置低风险
在 ~/.ssh/config 里添加 Host 配置块(Host web / HostName … / User … / Port …),之后直接 ssh web 连接。

01命令

命令
$ nano ~/.ssh/config && ssh web  # after adding: Host web / HostName 203.0.113.10 / User deploy / Port 2222

02作用说明

ssh 客户端每次连接都会读取 ~/.ssh/config。每个配置块以 Host 别名开头,可设置 HostName、User、Port、IdentityFile 等选项;这些配置对 scp、rsync、git(git@web:仓库)同样生效。用通配符如 Host * 还能为所有连接设置默认值。记得 chmod 600 ~/.ssh/config,否则 ssh 会提示权限过宽。

03风险

风险低风险
!注意: 这个文件写错可能导致连不上服务器:选项按“第一个匹配的 Host 块”生效,所以具体主机要写在通配符块之前。改写前先备份一份,可用 ssh -G web 查看最终解析出的配置来验证。

04撤销

撤销
$ Remove the Host block from ~/.ssh/config to delete the alias

05来源

  • ssh_config(5) — OpenSSH client configuration file
    man7.org · community · 2026-09-17
    依据: The per-user ~/.ssh/config file defines Host entries whose options (HostName, User, Port, IdentityFile) apply when the host alias is used on the ssh command line.

最后验证: 2026-09-18 · 分类: 开发者

06相关方案