当前位置:   article > 正文

如何使git接受自签名证书?_the certificate is self-signed

the certificate is self-signed

本文翻译自:How can I make git accept a self signed certificate?

Using Git, is there a way to tell it to accept a self signed certificate? 使用Git,有没有办法告诉它接受自签名证书?

I am using an https server to host a git server but for now the certificate is self signed. 我正在使用https服务器托管git服务器,但目前证书是自签名的。

When I try to create the repo there for the first time: 当我第一次尝试在此处创建存储库时:

git push origin master -f

I get the error: 我收到错误:

  1. error: Cannot access URL
  2. https://the server/git.aspx/PocketReferences/, return code 22
  3. fatal: git-http-push failed

#1楼

参考:https://stackoom.com/question/mlLs/如何使git接受自签名证书


#2楼

To permanently accept a specific certificate 永久接受特定证书

Try http.sslCAPath or http.sslCAInfo . 尝试使用http.sslCAPathhttp.sslCAInfo Adam Spiers's answer gives some great examples. 亚当·斯皮尔斯(Adam Spiers)的答案提供了一些很好的例子。 This is the most secure solution to the question. 这是最安全的解决方案。

To disable TLS/SSL verification for a single git command 为单个git命令禁用TLS / SSL验证

try passing -c to git with the proper config variable, or use Flow's answer : 尝试使用适当的config变量将-c传递给git ,或使用Flow的答案

git -c http.sslVerify=false clone https://example.com/path/to/git

To disable SSL verification for a specific repository 禁用特定存储库的SSL验证

If the repository is completely under your control, you can try: 如果存储库完全在您的控制之下,则可以尝试:

git config http.sslVerify false

Disabling TLS(/SSL) certificate verification globally is a terribly insecure practice. 全局禁用TLS(/ SSL)证书验证是一种非常不安全的做法。 Don't do it. 不要这样 Do not issue the above command with a --global modifier. 不要在上面的命令中使用--global修饰符。


There are quite a few SSL configuration options in git . git有很多SSL配置选项。 From the man page of git config : git config的手册页中:

  1. http.sslVerify
  2. Whether to verify the SSL certificate when fetching or pushing over HTTPS.
  3. Can be overridden by the GIT_SSL_NO_VERIFY environment variable.
  4. http.sslCAInfo
  5. File containing the certificates to verify the peer with when fetching or pushing
  6. over HTTPS. Can be overridden by the GIT_SSL_CAINFO environment variable.
  7. http.sslCAPath
  8. Path containing files with the CA certificates to verify the peer with when
  9. fetching or pushing over HTTPS.
  10. Can be overridden by the GIT_SSL_CAPATH environment variable.

A few other useful SSL configuration options: 其他一些有用的SSL配置选项:

  1. http.sslCert
  2. File containing the SSL certificate when fetching or pushing over HTTPS.
  3. Can be overridden by the GIT_SSL_CERT environment variable.
  4. http.sslKey
  5. File containing the SSL private key when fetching or pushing over HTTPS.
  6. Can be overridden by the GIT_SSL_KEY environment variable.
  7. http.sslCertPasswordProtected
  8. Enable git's password prompt for the SSL certificate. Otherwise OpenSSL will
  9. prompt the user, possibly many times, if the certificate or private key is encrypted.
  10. Can be overridden by the GIT_SSL_CERT_PASSWORD_PROTECTED environment variable.

#3楼

You can set GIT_SSL_NO_VERIFY to true : 您可以将GIT_SSL_NO_VERIFY设置为true

GIT_SSL_NO_VERIFY=true git clone https://example.com/path/to/git

or alternatively configure Git not to verify the connection on the command line: 或另选配置Git不在命令行上验证连接:

git -c http.sslVerify=false clone https://example.com/path/to/git

Note that if you don't verify SSL/TLS certificates, then you are susceptible to MitM attacks . 请注意,如果您不验证SSL / TLS证书,则容易受到MitM攻击


#4楼

I'm not a huge fan of the [EDIT: original versions of the] existing answers, because disabling security checks should be a last resort, not the first solution offered. 我不是现有答案的[编辑:原始版本]的忠实拥护者,因为禁用安全检查应该是不得已的方法,而不是第一个提供的解决方案。 Even though you cannot trust self-signed certificates on first receipt without some additional method of verification, using the certificate for subsequent git operations at least makes life a lot harder for attacks which only occur after you have downloaded the certificate. 即使没有某些其他验证方法也无法信任首次签收的自签名证书,但将证书用于后续git操作至少会大大降低仅下载证书后才发生的攻击的难度。 In other words, if the certificate you downloaded is genuine, then you're good from that point onwards. 换句话说,如果您下载的证书真实的,那么从那时起您就可以了。 In contrast, if you simply disable verification then you are wide open to any kind of man-in-the-middle attack at any point . 相反,如果你简单地禁用验证,那么你敞开,以在任何时候任何中间人攻击的。

To give a specific example: the famous repo.or.cz repository provides a self-signed certificate . 举一个具体的例子:著名的repo.or.cz存储库提供了一个自签名证书 I can download that file, place it somewhere like /etc/ssl/certs , and then do: 我可以下载该文件,将其放在/etc/ssl/certs ,然后执行以下操作:

  1. # Initial clone
  2. GIT_SSL_CAINFO=/etc/ssl/certs/rorcz_root_cert.pem \
  3. git clone https://repo.or.cz/org-mode.git
  4. # Ensure all future interactions with origin remote also work
  5. cd org-mode
  6. git config http.sslCAInfo /etc/ssl/certs/rorcz_root_cert.pem

Note that using local git config here (ie without --global ) means that this self-signed certificate is only trusted for this particular repository, which is nice. 请注意,在此处使用本地git config (即不使用--global )意味着仅该特定存储库信任此自签名证书,这很好。 It's also nicer than using GIT_SSL_CAPATH since it eliminates the risk of git doing the verification via a different Certificate Authority which could potentially be compromised. 它比使用GIT_SSL_CAPATH更好,因为它消除了git通过不同的证书颁发机构进行验证的风险,这可能会受到威胁。


#5楼

I keep coming across this problem, so have written a script to download the self signed certificate from the server and install it to ~/.gitcerts, then update git-config to point to these certificates. 我一直遇到这个问题,因此编写了一个脚本来从服务器下载自签名证书并将其安装到〜/ .gitcerts,然后更新git-config指向这些证书。 It is stored in global config, so you only need to run it once per remote. 它存储在全局配置中,因此每个远程只需要运行一次。

https://github.com/iwonbigbro/tools/blob/master/bin/git-remote-install-cert.sh https://github.com/iwonbigbro/tools/blob/master/bin/git-remote-install-cert.sh


#6楼

Git Self-Signed Certificate Configuration Git自签名证书配置

tl;dr tl; dr

NEVER disable all SSL verification! 切勿禁用所有SSL验证!

This creates a bad security culture. 这造成了不良的安全文化。 Don't be that person. 不要做那个人。

The config keys you are after are: 您需要的配置键是:

These are for configuring host certificates you trust 这些用于配置您信任的主机证书

These are for configuring YOUR certificate to respond to SSL challenges. 这些用于配置您的证书以响应SSL挑战。

Selectively apply the above settings to specific hosts. 有选择地将上述设置应用于特定主机。

Global .gitconfig for Self-Signed Certificate Authorities 自签名证书颁发机构的全局.gitconfig

For my own and my colleagues' sake here is how we managed to get self signed certificates to work without disabling sslVerify . 就我自己和我的同事而言,这就是我们如何在不禁用sslVerify情况下使自签名证书起作用的sslVerify Edit your .gitconfig to using git config --global -e add these: 编辑您的.gitconfig以使用git config --global -e添加以下内容:

  1. # Specify the scheme and host as a 'context' that only these settings apply
  2. # Must use Git v1.8.5+ for these contexts to work
  3. [credential "https://your.domain.com"]
  4. username = user.name
  5. # Uncomment the credential helper that applies to your platform
  6. # Windows
  7. # helper = manager
  8. # OSX
  9. # helper = osxkeychain
  10. # Linux (in-memory credential helper)
  11. # helper = cache
  12. # Linux (permanent storage credential helper)
  13. # https://askubuntu.com/a/776335/491772
  14. # Specify the scheme and host as a 'context' that only these settings apply
  15. # Must use Git v1.8.5+ for these contexts to work
  16. [http "https://your.domain.com"]
  17. ##################################
  18. # Self Signed Server Certificate #
  19. ##################################
  20. # MUST be PEM format
  21. # Some situations require both the CAPath AND CAInfo
  22. sslCAInfo = /path/to/selfCA/self-signed-certificate.crt
  23. sslCAPath = /path/to/selfCA/
  24. sslVerify = true
  25. ###########################################
  26. # Private Key and Certificate information #
  27. ###########################################
  28. # Must be PEM format and include BEGIN CERTIFICATE / END CERTIFICATE,
  29. # not just the BEGIN PRIVATE KEY / END PRIVATE KEY for Git to recognise it.
  30. sslCert = /path/to/privatekey/myprivatecert.pem
  31. # Even if your PEM file is password protected, set this to false.
  32. # Setting this to true always asks for a password even if you don't have one.
  33. # When you do have a password, even with this set to false it will prompt anyhow.
  34. sslCertPasswordProtected = 0

References: 参考文献:

Specify config when git clone -ing git clone -ing时指定配置

If you need to apply it on a per repo basis, the documentation tells you to just run git config --local in your repo directory. 如果您需要在每个回购基础上应用它,文档会告诉您只需在回购目录中运行git config --local Well that's not useful when you haven't got the repo cloned locally yet now is it? 那么,当您还没有在本地克隆存储库时,这没有用吗?

You can do the global -> local hokey-pokey by setting your global config as above and then copy those settings to your local repo config once it clones... 您可以通过如上所述设置全局配置来执行global- global -> local hokey-pokey,然后在克隆后将这些设置复制到本地repo配置中。

OR what you can do is specify config commands at git clone that get applied to the target repo once it is cloned. 或者,您可以做的是git clone指定配置命令,命令将在git clone到目标存储库后应用。

  1. # Declare variables to make clone command less verbose
  2. OUR_CA_PATH=/path/to/selfCA/
  3. OUR_CA_FILE=$OUR_CA_PATH/self-signed-certificate.crt
  4. MY_PEM_FILE=/path/to/privatekey/myprivatecert.pem
  5. SELF_SIGN_CONFIG="-c http.sslCAPath=$OUR_CA_PATH -c http.sslCAInfo=$OUR_CA_FILE -c http.sslVerify=1 -c http.sslCert=$MY_PEM_FILE -c http.sslCertPasswordProtected=0"
  6. # With this environment variable defined it makes subsequent clones easier if you need to pull down multiple repos.
  7. git clone $SELF_SIGN_CONFIG https://mygit.server.com/projects/myproject.git myproject/

One Liner 一线

EDIT: See VonC 's answer that points out a caveat about absolute and relative paths for specific git versions from 2.14.x/2.15 to this one liner 编辑:请参阅VonC答案 ,该警告指出了有关从2.14.x / 2.15到此衬垫的特定git版本的绝对和相对路径的警告

git clone -c http.sslCAPath="/path/to/selfCA" -c http.sslCAInfo="/path/to/selfCA/self-signed-certificate.crt" -c http.sslVerify=1 -c http.sslCert="/path/to/privatekey/myprivatecert.pem" -c http.sslCertPasswordProtected=0 https://mygit.server.com/projects/myproject.git myproject/

CentOS unable to load client key CentOS unable to load client key

If you are trying this on CentOS and your .pem file is giving you 如果您在CentOS上尝试此操作,并且.pem文件给您

unable to load client key: "-8178 (SEC_ERROR_BAD_KEY)"

Then you will want this StackOverflow answer about how curl uses NSS instead of Open SSL. 然后,您将需要此StackOverflow答案 ,以了解curl如何使用NSS而不是Open SSL。

And you'll like want to rebuild curl from source : 而且您想要从源代码重建curl

  1. git clone http://github.com/curl/curl.git curl/
  2. cd curl/
  3. # Need these for ./buildconf
  4. yum install autoconf automake libtool m4 nroff perl -y
  5. #Need these for ./configure
  6. yum install openssl-devel openldap-devel libssh2-devel -y
  7. ./buildconf
  8. su # Switch to super user to install into /usr/bin/curl
  9. ./configure --with-openssl --with-ldap --with-libssh2 --prefix=/usr/
  10. make
  11. make install

restart computer since libcurl is still in memory as a shared library 重新启动计算机,因为libcurl仍作为共享库在内存中

Python, pip and conda Python,pip和conda

Related : How to add a custom CA Root certificate to the CA Store used by pip in Windows? 相关如何将自定义CA Root证书添加到Windows中pip使用的CA Store?

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/208207
推荐阅读
相关标签
  

闽ICP备14008679号