发布于 2026-01-05 4 阅读
0

SSH Shortcuts DEV 的全球展示挑战赛由 Mux 呈现:展示你的项目!

SSH快捷方式

由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!

原文发布于hoelz.ro

假设你经常通过 SSH 连接到一台服务器。假设sshd这台服务器运行在一个非标准端口上,以避免被恶意扫描程序攻击(本例中我们采用 53718 端口)。要通过 SSH 连接到这台服务器,你需要运行以下命令:

  $ ssh -p 53718 rob@example-server.com

真够长的!肯定有办法避免输入这么多!我经常看到人们使用的一种技巧是为此创建一个 shell 别名:

  $ alias example='ssh -p 53718 rob@example-server.com' # in bash

所以现在我只需要跑就行了example。小菜一碟,对吧?

但是如果你用的是scp?或者sftp?或者rsync?或者git remote add?或者 Vim 的 netrw 插件呢?突然间,你简单的 shell 别名似乎也没那么酷了!

不过,这个问题有解决办法!请输入您的 SSH 配置文件。

您的 SSH 配置文件位于 `/ etc~/.ssh/config / ssh/config ...

Host example
  HostName example-server.com
  User rob
  Port 53718

现在我只需要这样做ssh example,瞧!它奏效了!虽然比别名长了四个字符,但这个快捷方式也适用于以下命令:

  scp example:that-important-file.txt .
  sftp example
  rsync -ar example:my-project/ .
  git remote add example ssh://example/~/my-project
  vim scp://example/that-important-file.txt

如果您有兴趣进一步缩短 Git 示例,请参阅我的相关博客文章。

您的 SSH 配置文件功能非常强大;我建议您查看man ssh_config更多选项并尝试使用!

文章来源:https://dev.to/hoelzro/ssh-shortcuts-13l9