自定义 SSH 端口上的 Git
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请注意,端口号应该不带方括号:
[]
Note that the port number should be there without the square brackets:
[]
SSH配置文件方式有什么问题?
当配置文件不存在时,您可以创建一个。除了
port
之外,配置文件还可以包含其他 SSH 配置选项:user
IdentityFile
等等,配置文件看起来像如果您运行的是 Linux,注意配置文件必须有严格的权限:用户读/写,其他人不能访问
SSH URL 方式怎么样?
很酷,我们唯一应该知道的是 Git
ssh://[user@]host.xz[:port]/path/to/repo.git/
[user@]host.xz:path/to/repo.git/
默认情况下,Gitlab 和 GitHub 将显示scp 类似语法 URL,我们无法提供自定义 SSH 端口。因此,为了更改 SSH 端口,我们需要使用标准语法
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 likeIf 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
ssh://[user@]host.xz[:port]/path/to/repo.git/
[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
当您想要主目录(在任何 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 serverjill.example
and you are logging in asjack
withsshd
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
(更新:几年后,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, ...).
如果您使用 SSH 的自定义端口(转发),正确的解决方案是
ssh://
标头就是窍门。In case you are using a custom port (forwarding) for SSH, the correct solution is
The
ssh://
header is the trick.