自定义 SSH 端口上的 Git

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5767850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:44:23  来源:igfitidea点击:

Git On Custom SSH Port

gitversion-controlsshcygwin

提问by ryanzec

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 the while I know I can give the port number when create a remote config, it seems like I can't do the same when doing a git clone. I am using gitolite so I clone commands look like:

我的 VPS 提供商建议我将 SSH 端口保留为他们默认分配的自定义端口号(不是 22)。问题是虽然我知道我可以在创建远程配置时提供端口号,但在执行 git clone 时似乎我不能做同样的事情。我正在使用 gitolite,所以我克隆的命令如下所示:

git clone [email protected]:gitolite-admin

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

有没有办法将其转换为使用自定义 ssh 端口号?

I should also mention I am running cygwin on windows. I have seen multiple places saying to add the custom port to the ~/.ssh/configfile like

我还应该提到我在 Windows 上运行 cygwin。我看到多个地方说要将自定义端口添加到~/.ssh/config文件中,例如

Host mydomain.com
    Port 12345

however in cygwin, that file does not seem to exist.

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

回答by Christo

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

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

请注意,端口号应该没有方括号:[]

回答by Jim Green

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

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

1 what's the problem with the ssh config file way?

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

When the config file does not exists, you can create one. Besides portthe config file can include other ssh config option:userIdentityFileand so on, the config file looks like

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

Host mydomain.com
    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

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

2 what about the ssh url way?

2 ssh url方式怎么样?

It's cool, the only thing we should know is that there two syntaxes for ssh url in git

很酷,我们唯一应该知道的是 git 中的 ssh url 有两种语法

  • standard syntax ssh://[user@]host.xz[:port]/path/to/repo.git/
  • scp like syntax [user@]host.xz:path/to/repo.git/
  • 标准语法 ssh://[user@]host.xz[:port]/path/to/repo.git/
  • 类似于 scp 的语法 [user@]host.xz:path/to/repo.git/

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

默认情况下,Gitlab 和 Github 会显示类似 scp 的语法url,我们无法提供自定义 ssh 端口。所以为了改变ssh端口,我们需要使用标准语法

回答by Daniel Santos

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

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

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

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

For Example, if the repo is in /home/Hyman/projects/jillwebon the server jill.comand you are logging in as Hymanwith sshdlistening on port 4242:

例如,如果 repo 位于/home/Hyman/projects/jillweb服务器上jill.com并且您正在Hymansshd监听端口 4242 的方式登录:

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

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

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

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

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

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

回答by earizon

(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 端口”时,Google 和 Qwant“航空公司”仍然将我发送到这里)在较新的 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

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 合适选项(端口、priv.key、IPv6、PKCS#11 设备等)的额外优势。