使用curl或wget连接网站的怎样忽略SSL证书错误

Crq
Crq
管理员
1932
文章
0
粉丝
Linux教程评论7字数 769阅读2分33秒阅读模式
摘要在默认情况下,cURL 使用 SSL 证书进行连接,如果指定的网站配置错误或证书过期,则会引发错误。下面我们看一下如何忽略其中的 SSL 证书错误。

当我们使用 curl 命令访问网站的时候,有时候可能会得到一个 SSL 证书错误:
这是因为在默认情况下,cURL 使用 SSL 证书进行连接,如果指定的网站配置错误或证书过期,则会引发错误。
下面我们看一下如何忽略其中的 SSL 证书错误。

使用 cURL 忽略 SSL 证书错误

一般来说,直接忽略错误然后继续连接故障网站是不推荐的。但是如果你信任该网站,那就可以。
使用 curl 的时候,附带 --insecure 选项可以忽略 ssl 证书错误,如下代码:

$ curl https://expired.badssl.com
curl: (60) SSL certificate problem: certificate has expired
More details here: https://curl.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

另外,也可以使用 -k 选项,其与 --insecure 的效果是相同的:

使用curl或wget连接网站的怎样忽略SSL证书错误-图片1

对所有 SSL 连接都使用 --insecure 选项

注意:除非在隔离或测试环境下可以执行此操作,否则不建议这样做。
你可以使用如下命令将 insecure 添加到 curl 配置文件中:

echo "insecure" >> ~/.curlrc

然后,在不使用 --insecure 选项的情况下,再次直接使用 curl 连接 html地址,也是可以成功的:

使用curl或wget连接网站的怎样忽略SSL证书错误-图片2

忽略 wget 的 SSL 证书错误

如果某个指定的网站配置错误或证书过期,而使用 wget 命令下载该网站中的文件时,也会出现 SSL 证书错误:

$ wget https://expired.badssl.com
--2022-11-17 14:35:55--  https://expired.badssl.com/
Resolving expired.badssl.com (expired.badssl.com)... 104.154.89.105
Connecting to expired.badssl.com (expired.badssl.com)|104.154.89.105|:443... connected.
ERROR: cannot verify expired.badssl.com's certificate, issued by ‘CN=COMODO RSA Domain Validation Secure Server CA,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB’:
  Issued certificate has expired.
To connect to expired.badssl.com insecurely, use `--no-check-certificate'.

默认情况下,wget会检查有效的SSL证书,以便您可以建立可靠的连接,如果没有,则会抛出一个错误,表示颁发的证书已过期。
要忽略 ssl 错误,可以使用 --no-check-certificate 选项,让它不检查 ssl 证书:

$ wget --no-check-certificate https://expired.badssl.com
--2022-11-17 15:18:07--  https://expired.badssl.com/
Resolving expired.badssl.com (expired.badssl.com)... 104.154.89.105
Connecting to expired.badssl.com (expired.badssl.com)|104.154.89.105|:443... connected.
WARNING: cannot verify expired.badssl.com's certificate, issued by ‘CN=COMODO RSA Domain Validation Secure Server CA,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB’:
  Issued certificate has expired.
HTTP request sent, awaiting response... 200 OK
Length: 494 [text/html]
Saving to: ‘index.html.1’
index.html.1        100%[===================>]     494  --.-KB/s    in 0s      
2022-11-17 15:18:08 (209 MB/s) - ‘index.html.1’ saved [494/494]
跳过认证检查

要在每次访问损坏的 SSL 站点的时候跳过证书检查,只需要在 wget 配置文件中添加 check-certificate = off 即可,这样在访问的时候就可以不添加 --no-check-certificate 选项了:

$ wget https://expired.badssl.com
--2022-11-17 15:41:50--  https://expired.badssl.com/
Resolving expired.badssl.com (expired.badssl.com)... 104.154.89.105
Connecting to expired.badssl.com (expired.badssl.com)|104.154.89.105|:443... connected.
WARNING: cannot verify expired.badssl.com's certificate, issued by ‘CN=COMODO RSA Domain Validation Secure Server CA,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB’:
  Issued certificate has expired.
HTTP request sent, awaiting response... 200 OK
Length: 494 [text/html]
Saving to: ‘index.html.2’
index.html.2        100%[===================>]     494  --.-KB/s    in 0s      
2022-11-17 15:41:51 (191 MB/s) - ‘index.html.2’ saved [494/494]

weinxin
我的微信
微信号已复制
我的微信
这是我的微信扫一扫
 
Crq
  • 本文由 Crq 发表于2025年3月21日 03:41:12
  • 转载请注明:https://www.cncrq.com/13532.html
Linux 利器- Python 脚本编程入门(一) Linux教程

Linux 利器- Python 脚本编程入门(一)

众所周知,系统管理员需要精通一门脚本语言,而且招聘机构列出的职位需求上也会这么写。大多数人会认为 Bash (或者其他的 shell 语言)用起来很方便,但一些强大的语言(比如 P...
手把手教你crontab排障 Linux教程

手把手教你crontab排障

crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro...
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证