有时,防火墙完全拒绝允许 SSH 连接。如果无法将 HTTPS 克隆与凭据缓存结合使用,则可以尝试使用通过 HTTPS 端口建立的 SSH 连接进行克隆。大多数防火墙规则都应该允许这样做,但代理服务器可能会干扰。
有时候会遇到,通过代理用 ssh 推送或拉取 GitHub 的代码会报错,错误提示如图1,这个时候可以试试给 ssh 连接换个端口:修改 ~/.ssh/config 文件就可以了。GitHub也有说明,这个问题应该是一个很普遍的问题:https://docs.github.com/en/authentication/troubleshooting-ssh/using-ssh-over-the-https-port
在这篇文章中
GitHub Enterprise Server 用户:目前不支持通过 HTTPS 端口通过 SSH 访问 GitHub Enterprise Server。
要测试是否可以通过 HTTPS 端口进行 SSH,请运行以下 SSH 命令:
1 2 3 |
$ ssh -T -p 443 git@ssh.github.com > Hi USERNAME! You've successfully authenticated, but GitHub does not > provide shell access. |
注意:端口 443 的主机名是 ,而不是 。ssh.github.com
github.com
如果行得通,那就太好了!如果没有,您可能需要按照我们的故障排除指南进行操作。
现在,要克隆存储库,您可以运行以下命令:
1 |
git clone ssh://git@ssh.github.com:443/YOUR-USERNAME/YOUR-REPOSITORY.git |
启用通过 HTTPS 的 SSH 连接
如果能够通过 SSH 连接到端口 443,则可以覆盖 SSH 设置,以强制与 GitHub.com 的任何连接都通过该服务器和端口运行。git@ssh.github.com
要在 SSH 配置文件中设置此项,请在 编辑该文件,并添加以下部分:~/.ssh/config
1 2 3 4 |
Host github.com Hostname ssh.github.com Port 443 User git |
您可以通过再次连接到 GitHub.com 来测试这是否有效:
1 2 3 |
$ ssh -T git@github.com > Hi USERNAME! You've successfully authenticated, but GitHub does not > provide shell access. |
更新已知主机
切换到端口 443 后首次与 GitHub 交互时,可能会收到警告消息 未在 中找到主机,或者通过其他名称找到主机。known_hosts
1 2 3 4 5 |
> The authenticity of host '[ssh.github.com]:443 ([140.82.112.36]:443)' can't be established. > ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. > This host key is known by the following other names/addresses: > ~/.ssh/known_hosts:32: github.com > Are you sure you want to continue connecting (yes/no/[fingerprint])? |
假设 SSH 指纹匹配,那么对这个问题的回答“是”是安全的 GitHub 发布的指纹之一。有关指纹列表,请参阅“GitHub 的 SSH 密钥指纹”。
使用 publickey
使用时候可能会出现如下信息,需要使用 publickey 登录自己的github.com账号
1 2 3 4 5 6 7 8 |
$ ssh -T git@github.com The authenticity of host '[ssh.github.com]:443 ([20.205.243.160]:443)' can't be established. ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. This host key is known by the following other names/addresses: ~/.ssh/known_hosts:2: [hashed name] Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '[ssh.github.com]:443' (ED25519) to the list of known hosts. git@ssh.github.com: Permission denied (publickey). |
1. 生成新的 SSH 密钥对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" Generating public/private rsa key pair. Enter file in which to save the key (/home/tony/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/tony/.ssh/id_rsa Your public key has been saved in /home/tony/.ssh/id_rsa.pub The key fingerprint is: SHA256:IW8MHwEG18U/ud123456H8KhdNvPYh6s4lHuhmMK0OAa6ZE your_email@example.com The key's randomart image is: +---[RSA 4096]----+ | ..+o.o. | | o ... | | . o o . . | | + o * o + | | E o . S ..= | | . + . . .o=.+ | | o . .o.=o+ | | . =o..=++| | .+.+oo.oB| +----[SHA256]-----+ |
此命令将使用您的电子邮件作为标签创建新的 SSH 密钥。按照提示将密钥保存在默认位置 ( ~/.ssh/id_rsa
),并根据需要设置密码。
2. 启动 SSH 代理
如果尚未运行,请启动 SSH 代理
1 2 |
$ eval "$(ssh-agent -s)" Agent pid 13043 |
3. 将 SSH 密钥添加到代理
1 2 |
$ ssh-add ~/.ssh/id_rsa Identity added: /home/tony/.ssh/id_rsa (your_email@example.com) |
4. 将 SSH 密钥添加到 GitHub
1 2 |
cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2E1AADAQABAAACAQCk12ALq45+pGP6PTm3Az1ycdibOyTLhKZpGNj8iVbKgEApwWJ2MtUy7MhAXsxJh5ago2wk1C7FyQo6adQJtLdxIWA1H7BQD86QYmydIDxVnWZqGPB/rE1w94SUD9mEiyttKm0vlRY8x4h0P0A9pAtQU/w0IDTPYVVAf+46Ae/JgBUFttKfgGcdrvpcVBihvWAJuwiyC43CFBXRVjQcwbdhPqsZQJkiehWSS2y5hVtrrkMuSXVyG/Y2jrMJ48RmgHXBBaZYt/M6zS5NmPAo5mSTowQhznd962Ru+eyd5JgUXvNCOSgztXtIOWYrcVfVreZfPk6EYGfZp8QXjZe/XU0Zy4dDbDJU6zm5MAQ6lpGqU6Np1A0ETXJ6edjEAHZX6OrpjkMHsFNbnnHu9F/GiXAWoGtbtGn04SoReEnVB4Vhxnw1tNqKkzoK9QJBMGb16xDbH1a7NonuZTcT/+v/SW6Frq2b6cfxkixgXXTolvW565/HmwdNMjMWfPvQUOVlLNcobdgBH4eauALsLoR5Zc62pniqiAyVGkEDE+YKa9PFoJSv8DCN2pcOds6x0QKhOEChwFj4y68yZhwxzsANTmO1lyfgAXt3j8Wy+m/fiyckNfBfDITL4WyWJtaZENImX3ULVNS2fc1KaWvJxsYJVZgSpUycnDiY6GdywvhrBm2SilxQ== your_email@example.com |
复制输出,然后导航到 Settings > SSH and GPG keys > New SSH key.将其添加到 GitHub 帐户
5. 修改 ~/.ssh/config
1 2 3 4 5 |
Host github.com HostName ssh.github.com User git Port 443 IdentityFile ~/.ssh/id_rsa |
6. 测试与 GitHub 的 SSH 连接
1 2 |
$ ssh -T -p 443 git@ssh.github.com Hi tony! You've successfully authenticated, but GitHub does not provide shell access. |
7. 克隆项目测试效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$ git clone --recurse-submodules ssh://git@ssh.github.com:443/ollama/ollama Cloning into 'ollama'... remote: Enumerating objects: 22012, done. remote: Counting objects: 100% (8/8), done. remote: Compressing objects: 100% (8/8), done. remote: Total 22012 (delta 2), reused 3 (delta 0), pack-reused 22004 Receiving objects: 100% (22012/22012), 21.95 MiB | 4.72 MiB/s, done. Resolving deltas: 100% (13642/13642), done. Submodule 'llama.cpp' (https://github.com/ggerganov/llama.cpp.git) registered for path 'llm/llama.cpp' Cloning into '/home/tony/ollama/llm/llama.cpp'... remote: Enumerating objects: 19720, done. remote: Counting objects: 100% (19720/19720), done. remote: Compressing objects: 100% (6651/6651), done. remote: Total 19720 (delta 14564), reused 17712 (delta 12819), pack-reused 0 Receiving objects: 100% (19720/19720), 39.37 MiB | 13.83 MiB/s, done. Resolving deltas: 100% (14564/14564), done. Submodule path 'llm/llama.cpp': checked out 'd94c6e0ccbd29ee1ba4f44e9caa8682ad94df9fa' Submodule 'kompute' (https://github.com/nomic-ai/kompute.git) registered for path 'llm/llama.cpp/ggml/src/kompute' Cloning into '/home/tony/ollama/llm/llama.cpp/ggml/src/kompute'... remote: Enumerating objects: 9111, done. remote: Counting objects: 100% (246/246), done. remote: Compressing objects: 100% (143/143), done. remote: Total 9111 (delta 114), reused 192 (delta 93), pack-reused 8865 Receiving objects: 100% (9111/9111), 17.58 MiB | 6.60 MiB/s, done. Resolving deltas: 100% (5721/5721), done. Submodule path 'llm/llama.cpp/ggml/src/kompute': checked out '4565194ed7c32d1d2efa32ceab4d3c6cae006306' |
响应速度比原来的 https 要快。