git 远程添加其他 SSH 端口

发布于 2024-09-16 14:19:26 字数 125 浏览 8 评论 0原文

在 Git 中,当我的主机使用不同的 SSH 端口时,如何添加远程源服务器?

git remote add origin ssh://user@host/srv/git/example

In Git, how can I add a remote origin server when my host uses a different SSH port?

git remote add origin ssh://user@host/srv/git/example

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

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

发布评论

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

评论(9

留一抹残留的笑 2024-09-23 14:19:26

你可以这样做:

git remote add origin ssh://user@host:1234/srv/git/example

1234是正在使用的ssh端口

You can just do this:

git remote add origin ssh://user@host:1234/srv/git/example

1234 is the ssh port being used

dawn曙光 2024-09-23 14:19:26

您需要编辑 ~/.ssh/config 文件。添加如下内容:

Host example.com
    Port 1234

快速谷歌搜索显示 很少 不同的资源比我更详细地解释了它。

You need to edit your ~/.ssh/config file. Add something like the following:

Host example.com
    Port 1234

A quick google search shows a few different resources that explain it in more detail than me.

好多鱼好多余 2024-09-23 14:19:26

您可以继续使用传统的 URL 形式通过 SSH 访问 git,而不是使用 ssh:// 协议前缀,只需进行一点小小的更改。提醒一下,常规 URL is

git@host:path/to/repo.git

要指定替代端口,请将 user@host 部分放在括号中,包括端口:

[git@host:port]:path/to/repo.git

但如果端口更改只是暂时的,您可以告诉 git 使用不同的 SSH命令而不是更改存储库的远程 URL:

export GIT_SSH_COMMAND='ssh -p port'
git clone git@host:path/to/repo.git # for instance

Rather than using the ssh:// protocol prefix, you can continue using the conventional URL form for accessing git over SSH, with one small change. As a reminder, the conventional URL is:

git@host:path/to/repo.git

To specify an alternative port, put brackets around the user@host part, including the port:

[git@host:port]:path/to/repo.git

But if the port change is merely temporary, you can tell git to use a different SSH command instead of changing your repository’s remote URL:

export GIT_SSH_COMMAND='ssh -p port'
git clone git@host:path/to/repo.git # for instance
或十年 2024-09-23 14:19:26

最佳答案对我不起作用。我从一开始就需要 ssh:// 。

# does not work
git remote set-url origin [email protected]:10000/aaa/bbbb/ccc.git
# work
git remote set-url origin ssh://[email protected]:10000/aaa/bbbb/ccc.git

Best answer doesn't work for me. I needed ssh:// from the beggining.

# does not work
git remote set-url origin [email protected]:10000/aaa/bbbb/ccc.git
# work
git remote set-url origin ssh://[email protected]:10000/aaa/bbbb/ccc.git
蓝梦月影 2024-09-23 14:19:26

对于那些编辑 ./.git/config 的人

[remote "external"]                                                                                                                                                                                                                                                            
  url = ssh://[email protected]:11720/aaa/bbb/ccc                                                                                                                                                                                                               
  fetch = +refs/heads/*:refs/remotes/external/* 

For those of you editing the ./.git/config

[remote "external"]                                                                                                                                                                                                                                                            
  url = ssh://[email protected]:11720/aaa/bbb/ccc                                                                                                                                                                                                               
  fetch = +refs/heads/*:refs/remotes/external/* 
忘羡 2024-09-23 14:19:26

对于 gitlab,示例 ssh 端口是 2224,因此:

git 远程添加 ssh://[电子邮件受保护]:2224 /your_group/your_project.git

for gitlab, example ssh port is 2224, therefore:

git remote add ssh://[email protected]:2224/your_group/your_project.git

半暖夏伤 2024-09-23 14:19:26

尝试连接到我的 git 服务器时遇到类似的问题

(有一个 gitea docker 容器中的服务器,ssh 端口配置为 2022,而不是标准 22,此处作为示例 my-git-server.lan )。

  1. 创建 ssh 密钥对(安静,无密码)
$ ssh-keygen -q -N '' -b 4096 -f ~/.ssh/mykeyfile

(这将创建两个文件:公钥 mykeyfile.pub 和私钥 mykeyfile,不带任何扩展名)

  1. 显示以下内容公钥并将其复制/粘贴到 git 服务器中您的个人资料的 SSH 密钥(类似于您在 Github )
$ cat ~/.ssh/mykeyfile.pub
  1. 将以下行添加到 ssh-config 以指定 git-server主机名、端口和密钥文件
$ nano ~/.ssh/config
Host my-git-server.lan
  HostName my-git-server.lan
  User git
  Port 2022
  IdentityFile ~/.ssh/mykeyfile

(请注意,用户名始终 git,无论您在 git 服务器上的实际用户名是什么)

  1. 使用以下命令测试与 git 服务器的 ssh 连接公钥,.. 并收到成功消息
$ ssh -T [email protected]
Hi there, username! You've successfully authenticated with the key named /Users/username/.ssh/mykeyfile.pub

.. 使用 -v“详细模式”来分析任何错误:(

$ ssh -Tvvv [email protected]

再次注意,用户名是 always git)

  1. 指定您的远程地址 ssh://[email] protected]:2022/alex/myproject.git 用于本地 git 存储库(再次注意用户 git 和端口 2022), ..检查远程配置
$ cd your/local/git/repository/folder
$ git remote add my-git-server ssh://[email protected]:2022/alex/myproject.git
$ git remote -v

(在这里您还可以看到在我的 git 服务器上我的实际用户是alex,存储库是myproject

完成!您现在可以使用 git-server .. fetch/commit/push 等

(这是我在 serverfault.com 上发布的帖子的副本 )


更新:正如评论中正确指出的 - 您不一定需要在远程 URL 中指定端口 2022,因为它已经在 < code>~/.ssh/config 文件为 PORT 2022

Had a similar issue trying to connect to my git server

( have a gitea server in a docker container with ssh-port configured to 2022, instead of standard 22, here as an example my-git-server.lan ).

  1. create ssh key-pair (quiet, without password)
$ ssh-keygen -q -N '' -b 4096 -f ~/.ssh/mykeyfile

(this will create two files: public-key mykeyfile.pub and private-key mykeyfile without any extension)

  1. display contents of the public-key and copy/paste it to your profile's SSH keys in your git-server (similar to how you would do it on Github )
$ cat ~/.ssh/mykeyfile.pub
  1. add following lines to ssh-config to specify git-server's hostname, port and key-file
$ nano ~/.ssh/config
Host my-git-server.lan
  HostName my-git-server.lan
  User git
  Port 2022
  IdentityFile ~/.ssh/mykeyfile

(notice that the username is always git, regardless of your actual username on your git-server)

  1. test ssh connection to your git-server using public-key, .. and receive a success message
$ ssh -T [email protected]
Hi there, username! You've successfully authenticated with the key named /Users/username/.ssh/mykeyfile.pub

.. use -v "verbose mode" to analyse any errors:

$ ssh -Tvvv [email protected]

(again, notice that the username is always git)

  1. specify your remote address ssh://[email protected]:2022/alex/myproject.git for your local git repository (again, notice the user git and the port 2022), .. check remote configuration
$ cd your/local/git/repository/folder
$ git remote add my-git-server ssh://[email protected]:2022/alex/myproject.git
$ git remote -v

( here you also see that on my git-server my actual user is alex and repository is myproject )

Done! You can now work with your git-server .. fetch/commit/push etc.

( this is a copy of my post on serverfault.com )


Update: as rightly noted in the comments - you do not necessarily need to specify port 2022 in the remote-url, since it is already configured in ~/.ssh/config file as PORT 2022.

木格 2024-09-23 14:19:26

只需看看如何正确设置 ~/.ssh/config 文件即可。

您可以轻松地为不同的主机指定不同的设置。

要解决您的问题,您可以

Host github.com 
Port 22 
Host * 
Port 1234

查看 ssh_config 手册页,它在前几页上解释了您需要了解的所有内容。

Just have a look at how to set up your ~/.ssh/config file correctly.

You can specify different settings for different hosts easily.

To solve your problem you would set

Host github.com 
Port 22 
Host * 
Port 1234

Have a look at the ssh_config manual page, it explains everything you need to know on the first few pages.

别低头,皇冠会掉 2024-09-23 14:19:26

1.git remote add ${shortname} ${url}

2.git remote remove Shortname (删除远程)

3.git remote -v(就是查看你当前的远程列表)

4.git push 远程分支

5.git remote rename AB(将 A 重命名为 B)

6.git remote 显示短名称(显示远程信息)

所有这些都适合我。

1.git remote add ${shortname} ${url}

2.git remote remove shortname (is remove a remote)

3.git remote -v (is to see your current remote list)

4.git push remote branch

5.git remote rename A B (rename A to B)

6.git remote show shortname (show remote info)

All this works for me.

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