
Ubuntu这个系统没有全局代理这种说法,每个应用都不同,比如命令行需要设置命令行代理,桌面应用需要在图形界面设置代理,docker则需要其他代理,很复杂,这个文章主要讨论命令行代理。
命令行代理生效的应用主要生效的应用有后台运行的应用,比如python,php,等后台服务
设置代理需要有一个代理的服务器,这里选择了windows的clash的局域网代理作为代理服务

选择增强代理,要安装ServiceMode 开启局域网代理,关闭随机切换端口,记住现在的端口号
查看运行clash服务主机的ip
登录Ubuntu
有两种代理方式,一种是临时代理
使用http代理
export http_proxy=”http://127.0.0.1:20171/”
export https_proxy=”http://127.0.0.1:20171/”
上面的ip填写上面主机的ip,端口是对应的局域网端口。
一种是socks代理
export http_proxy=”socks5://127.0.0.1:20170/”
export https_proxy=”socks5://127.0.0.1:20170/”
本教程使用http代理
永久代理设置
登录终端root账户
vim ~/.bashrc
在后面追加如下内容
export http_proxy='http://192.168.1.101:50121/'
export https_proxy='http://192.168.1.101:50121/'
上面填写内容说明在前面
注意格式要严谨,别擅自添加空格改变标点,否则会失败
保存后生效
source ~/.bashrc
终端代理生效后,不一定能ping通,因为:ping 使用的是ICMP协议,ICMP 处于网络层(第三层),而SOCKS5是传输层代理协议(第四层),HTTP和HTTPS是应用层协议(第五层或者第七层),协议层不同是无法代理的。
测试生效方法
curl -vv https://www.github.com
输出内容如下
root@ikaroswebserver:~# curl -vv https://www.github.com
* Uses proxy env variable https_proxy == 'http://192.168.1.101:50121/'
* Trying 192.168.1.101:50121...
* Connected to 192.168.1.101 (192.168.1.101) port 50121
* CONNECT tunnel: HTTP/1.1 negotiated
* allocate connect buffer
* Establish HTTP proxy tunnel to www.github.com:443
> CONNECT www.github.com:443 HTTP/1.1
> Host: www.github.com:443
> User-Agent: curl/8.5.0
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 Connection established
<
* CONNECT phase completed
* CONNECT tunnel established, response 200
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* CAfile: /etc/ssl/certs/ca-certificates.crt
* CApath: /etc/ssl/certs
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256 / X25519 / id-ecPublicKey
* ALPN: server accepted h2
* Server certificate:
* subject: CN=github.com
* start date: Mar 7 00:00:00 2024 GMT
* expire date: Mar 7 23:59:59 2025 GMT
* subjectAltName: host "www.github.com" matched cert's "www.github.com"
* issuer: C=GB; ST=Greater Manchester; L=Salford; O=Sectigo Limited; CN=Sectigo ECC Domain Validation Secure Server CA
* SSL certificate verify ok.
* Certificate level 0: Public key type EC/prime256v1 (256/128 Bits/secBits), signed using ecdsa-with-SHA256
* Certificate level 1: Public key type EC/prime256v1 (256/128 Bits/secBits), signed using ecdsa-with-SHA384
* Certificate level 2: Public key type EC/secp384r1 (384/192 Bits/secBits), signed using ecdsa-with-SHA384
* using HTTP/2
* [HTTP/2] [1] OPENED stream for https://www.github.com/
* [HTTP/2] [1] [:method: GET]
* [HTTP/2] [1] [:scheme: https]
* [HTTP/2] [1] [:authority: www.github.com]
* [HTTP/2] [1] [:path: /]
* [HTTP/2] [1] [user-agent: curl/8.5.0]
* [HTTP/2] [1] [accept: */*]
> GET / HTTP/2
> Host: www.github.com
> User-Agent: curl/8.5.0
> Accept: */*
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
< HTTP/2 301
< content-length: 0
< location: https://github.com/
<
* Connection #0 to host 192.168.1.101 left intact
详细解释如下
这段内容是从使用 curl 命令在 Linux 终端(特别是以 root 用户身份在名为 ikaroswebserver 的机器上)尝试访问 https://www.github.com 的详细输出。curl 是一个命令行工具,用于从或向服务器传输数据,支持多种协议,包括 HTTP、HTTPS 和 FTP。下面是对这段输出的详细解释:
使用代理:
- Uses proxy env variable https_proxy == ‘http://192.168.1.101:50121/’
这表明 curl 正在使用一个环境变量 https_proxy 指定的 HTTP 代理服务器。代理服务器的地址是 192.168.1.101,端口是 50121。
连接到代理服务器: - Trying 192.168.1.101:50121…
- Connected to 192.168.1.101 (192.168.1.101) port 50121
这两行表明 curl 正在尝试连接到指定的代理服务器,并且已经成功连接。
建立 HTTP 代理隧道: - CONNECT tunnel: HTTP/1.1 negotiated
- Establish HTTP proxy tunnel to www.github.com:443
CONNECT www.github.com:443 HTTP/1.1
Host: www.github.com:443
User-Agent: curl/8.5.0
Proxy-Connection: Keep-Alive
< HTTP/1.1 200 Connection established
这几行显示 curl 正在通过代理服务器建立一个到 www.github.com 在 443 端口上的 HTTPS 连接的隧道。代理服务器响应 HTTP 200,表示连接已成功建立。
TLS/SSL 握手:
接下来的几行描述了 TLS/SSL 握手的过程,包括协商的协议版本(TLSv1.3)、加密算法(TLS_AES_128_GCM_SHA256)、服务器证书的验证等。
服务器证书信息:
显示了服务器证书的详细信息,包括证书的颁发者、有效期、主题和主题备用名称(SAN),以及证书链的验证。
使用 HTTP/2 协议:
- using HTTP/2
表明 curl 和 github.com 服务器之间的通信使用的是 HTTP/2 协议。
发送 GET 请求:
[HTTP/2] [1] OPENED stream for https://www.github.com/
[HTTP/2] [1] [:method: GET]
[HTTP/2] [1] [:scheme: https]
[HTTP/2] [1] [:authority: www.github.com]
[HTTP/2] [1] [:path: /]
[HTTP/2] [1] [user-agent: curl/8.5.0]
[HTTP/2] [1] [accept: /]
GET / HTTP/2
Host: www.github.com
User-Agent: curl/8.5.0
Accept: /
这些行显示了 curl 通过 HTTP/2 发送的 GET 请求的详细信息。
重定向:
< HTTP/2 301
< content-length: 0
< location: https://github.com/
服务器返回了一个 301 状态码,表示请求的资源已被永久移动到新的 URL(https://github.com/)。没有返回内容(content-length: 0),但提供了新的位置。
连接保持:- Connection #0 to host 192.168.1.101 left intact
表示与代理服务器的连接在请求完成后仍然保持打开状态。
总结:这段输出展示了 curl 命令通过 HTTP 代理服务器建立到 github.com 的 HTTPS 连接、进行 TLS/SSL 握手、发送 HTTP/2 GET 请求并处理重定向响应的整个过程。