使用Git,是否有一种方法告诉它接受自签名证书?
我使用https服务器托管git服务器,但目前证书是自签名的。
当我第一次尝试在那里创建回购时:
git push origin master -f
我得到了错误:
error: Cannot access URL
https://the server/git.aspx/PocketReferences/, return code 22
fatal: git-http-push failed
使用Git,是否有一种方法告诉它接受自签名证书?
我使用https服务器托管git服务器,但目前证书是自签名的。
当我第一次尝试在那里创建回购时:
git push origin master -f
我得到了错误:
error: Cannot access URL
https://the server/git.aspx/PocketReferences/, return code 22
fatal: git-http-push failed
当前回答
在使用sslKey或sslCert使用一行程序时要小心,如Josh Peak的回答所示:
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
只有Git 2.14.x/2.15(2015年Q3)能够正确地解释~username/mykey这样的路径(同时它仍然可以解释/path/to/privatekey这样的绝对路径)。
参见Junio C Hamano (gitster)提交的8d15496(2017年7月20日)。 资助人:查尔斯·贝利(hashpling)。 (由Junio C Hamano合并- gitster -在commit 17b1e1d, 2017年8月11日)
http.c: http.sslcert and http.sslkey are both pathnames Back when the modern http_options() codepath was created to parse various http.* options at 29508e1 ("Isolate shared HTTP request functionality", 2005-11-18, Git 0.99.9k), and then later was corrected for interation between the multiple configuration files in 7059cd9 ("http_init(): Fix config file parsing", 2009-03-09, Git 1.6.3-rc0), we parsed configuration variables like http.sslkey, http.sslcert as plain vanilla strings, because git_config_pathname() that understands "~[username]/" prefix did not exist. Later, we converted some of them (namely, http.sslCAPath and http.sslCAInfo) to use the function, and added variables like http.cookeyFile http.pinnedpubkey to use the function from the beginning. Because of that, these variables all understand "~[username]/" prefix. Make the remaining two variables, http.sslcert and http.sslkey, also aware of the convention, as they are both clearly pathnames to files.
其他回答
我是这样做的:
git init
git config --global http.sslVerify false
git clone https://myurl/myrepo.git
它为我工作,只需运行以下命令
git config --global http.sslVerify false
它将打开一个git凭据窗口,提供您的凭据。第一次只有它问
我经常遇到这个问题,所以写了一个脚本从服务器下载自签名证书并将其安装到~/。然后更新git-config以指向这些证书。它存储在全局配置中,因此每个远程只需要运行一次。
https://github.com/iwonbigbro/tools/blob/master/bin/git-remote-install-cert.sh
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. 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.
举一个具体的例子:著名的repo.or.cz存储库提供了一个自签名证书。我可以下载这个文件,把它放在/etc/ssl/certs这样的地方,然后做:
# Initial clone
GIT_SSL_CAINFO=/etc/ssl/certs/rorcz_root_cert.pem \
git clone https://repo.or.cz/org-mode.git
# Ensure all future interactions with origin remote also work
cd org-mode
git config http.sslCAInfo /etc/ssl/certs/rorcz_root_cert.pem
注意,在这里使用本地git配置(即不使用——global)意味着这个自签名证书只对这个特定的存储库受信任,这很好。它也比使用GIT_SSL_CAPATH更好,因为它消除了git通过不同的证书颁发机构进行验证的风险,这种风险可能会受到损害。
关于http。sslCAPath选项:如果在包含证书文件的目录上执行了OpenSSL c_rehash命令,git将只检测指定目录路径下的证书文件。c_rehash命令将为每个证书创建符号链接,其中链接的名称为哈希值。例如:
$ cd /path/to/ssl/cert/directory
$ ls -al
total 16
drwxr-xr-x 3 user staff 96 Oct 20 13:47 .
drwxr-xr-x 4 user staff 128 Oct 20 13:46 ..
-rw-r--r-- 1 user staff 4832 Oct 20 13:47 google.pem
$ /usr/local/opt/openssl@1.1/bin/c_rehash ./
Doing ./
$ ls -al
total 16
drwxr-xr-x 4 user staff 128 Oct 20 13:58 .
drwxr-xr-x 4 user staff 128 Oct 20 13:46 ..
lrwxr-xr-x 1 user staff 10 Oct 20 13:58 f6dbf7a7.0 -> google.pem
-rw-r--r-- 1 user staff 4832 Oct 20 13:47 google.pem
注意,c_rehash命令创建了以下符号链接:f6dbf7a7.0 -> google.pem。
您还可以用下面的命令代替c_rehash实用程序,不过请注意,下面的命令只处理*。Pem文件,而c_rehash实用程序将处理.pem, .crt, .cer或.crl文件:
for file in *.pem; do ln -s $file `openssl x509 -hash -noout -in $file`.0; done
如果您现在配置http。将sslCAPath导入到包含上述符号链接的目录,git将获取证书文件:
# contents of /etc/gitconfig
[http]
sslCAPath = /path/to/ssl/cert/directory/
也可以配置http。sslCAPath使用一个环境变量:
export GIT_SSL_CAPATH=/path/to/ssl/cert/directory/