自定义 SSH 端口上的 Git

发布于 2024-11-03 02:16:06 字数 606 浏览 1 评论 0原文

我的 VPS 提供商建议我将 SSH 端口保留为他们默认分配的自定义端口号(不是 22)。问题是,虽然我知道我可以在创建远程配置时提供端口号,但在进行 Git 克隆时似乎无法提供相同的操作。我正在使用 gitolite 所以克隆命令看起来像:

git clone [email protected]:gitolite-admin

有没有办法将其隐藏为使用自定义 SSH 端口号?

我还应该提到我正在 Windows 上运行 Cygwin。我看到多个地方都说将自定义端口添加到 ~/.ssh/config 文件中:

Host mydomain.example
    Port 12345

但是在 Cygwin 中,该文件似乎不存在。

My VPS provider recommends that I leave my SSH port to the custom port number they assign it by default (not 22). The thing is, while I know I can provide the port number when creating a remote config, it seems I can't do the same when doing a Git clone. I am using gitolite so the clone commands look like:

git clone [email protected]:gitolite-admin

Is there a way to covert this to using the custom SSH port number?

I should also mention I am running Cygwin on Windows. I have seen multiple places saying to add the custom port to the ~/.ssh/config file:

Host mydomain.example
    Port 12345

However in Cygwin, that file does not seem to exist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娇妻 2024-11-10 02:16:06
git clone ssh://[email protected]:[port]/gitolite-admin

请注意,端口号应该不带方括号:[]

git clone ssh://[email protected]:[port]/gitolite-admin

Note that the port number should be there without the square brackets: []

黑色毁心梦 2024-11-10 02:16:06

以上答案很好,但对于像我这样的 Git 新用户来说还不清楚。因此,经过一番调查,我提供了这个新答案。

SSH配置文件方式有什么问题?

当配置文件不存在时,您可以创建一个。除了 port 之外,配置文件还可以包含其他 SSH 配置选项:user IdentityFile 等等,配置文件看起来像

Host mydomain.example
    User git
    Port 12345

如果您运行的是 Linux,注意配置文件必须有严格的权限:用户读/写,其他人不能访问

SSH URL 方式怎么样?

很酷,我们唯一应该知道的是 Git

  • 标准语法中 SSH URL 有两种语法 ssh://[user@]host.xz[:port]/path/to/repo.git/
  • scp 类似语法 [user@]host.xz:path/to/repo.git/

默认情况下,Gitlab 和 GitHub 将显示scp 类似语法 URL,我们无法提供自定义 SSH 端口。因此,为了更改 SSH 端口,我们需要使用标准语法

Above answers are nice and great, but not clear for new Git users like me. So after some investigation, I offer this new answer.

What's the problem with the SSH config file way?

When the config file does not exists, you can create one. Besides port the config file can include other SSH config option:user IdentityFile and so on, the config file looks like

Host mydomain.example
    User git
    Port 12345

If you are running Linux, take care the config file must have strict permission: read/write for the user, and not accessible by others

What about the SSH URL way?

It's cool, the only thing we should know is that there two syntaxes for SSH URL in Git

  • standard syntax ssh://[user@]host.xz[:port]/path/to/repo.git/
  • scp like syntax [user@]host.xz:path/to/repo.git/

By default Gitlab and GitHub will show the scp like syntax URL, and we can not give the custom SSH port. So in order to change SSH port, we need use the standard syntax

萌吟 2024-11-10 02:16:06

当您想要主目录(在任何 UNIX 上)的相对路径时,您可以使用以下奇怪的语法:

ssh://[user@]host.example[:port]/~[user]/path/to/repo

例如,如果存储库位于服务器 jill.example 上的 /home/jack/projects/jillweb 中,并且您以 jack 身份登录 使用 sshd 监听端口 4242:

ssh://[email protected]:4242/~/projects/jillweb

当以 jill 身份登录时(假设您具有文件权限):

ssh ://[电子邮件受保护]:4242/~jack/projects/jillweb

When you want a relative path from your home directory (on any UNIX) you use this strange syntax:

ssh://[user@]host.example[:port]/~[user]/path/to/repo

For Example, if the repo is in /home/jack/projects/jillweb on the server jill.example and you are logging in as jack with sshd listening on port 4242:

ssh://[email protected]:4242/~/projects/jillweb

And when logging in as jill (presuming you have file permissions):

ssh://[email protected]:4242/~jack/projects/jillweb

‘画卷フ 2024-11-10 02:16:06

(更新:几年后,Google 和 Qwant“航空公司”在搜索“git 非默认 ssh 端口”时仍然将我发送到这里)
在较新的 git 版本中,可能更好的方法是使用 GIT_SSH_COMMAND ENV.VAR,例如:

GIT_SSH_COMMAND="ssh -oPort=1234 -i ~/.ssh/myPrivate_rsa.key" \
git clone myuser@myGitRemoteServer:/my/remote/git_repo/path

这具有允许任何其他 ssh 合适选项(端口、priv.key、IPv6、PKCS#11 设备等)的额外优点。

(Update: a few years later Google and Qwant "airlines" still send me here when searching for "git non-default ssh port")
A probably better way in newer git versions is to use the GIT_SSH_COMMAND ENV.VAR like:

GIT_SSH_COMMAND="ssh -oPort=1234 -i ~/.ssh/myPrivate_rsa.key" \
git clone myuser@myGitRemoteServer:/my/remote/git_repo/path

This has the added advantage of allowing any other ssh suitable option (port, priv.key, IPv6, PKCS#11 device, ...).

后知后觉 2024-11-10 02:16:06

如果您使用 SSH 的自定义端口(转发),正确的解决方案是

git clone ssh://git@url:2222/user/repo.git

ssh:// 标头就是窍门。

In case you are using a custom port (forwarding) for SSH, the correct solution is

git clone ssh://git@url:2222/user/repo.git

The ssh:// header is the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文