APT 代理配置
最近遇到 APT 连不上 docker 源的问题,需要配置 APT 的代理。
创建 /etc/apt/apt.conf.d/50proxy.conf
文件,填入代理配置:
Acquire::http::Proxy "http://ADDRESS:PORT";
Cargo 代理配置
编辑 ~/.cargo/config
文件:
# 将 crates-io 替换为中科大镜像
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
#registry = "https://mirrors.ustc.edu.cn/crates.io-index"
# 代理配置,git 也会使用这个配置
[http]
proxy = "socks://localhost:7890"
[https]
proxy = "socks://localhost:7890"
Git 代理配置
HTTP/HTTPS
git config --global http.proxy socks://localhost:7890
HTTP 和 HTTPS 都使用
http.proxy
,不存在名为https.proxy
的配置。
配置会写到 ~/.gitconfig
文件中,也可以通过编辑该文件来进行配置。
[user]
email = hubenchang0515@outlook.com
name = planc
[http]
proxy = socks://localhost:7890
也可以单独配置指定域名的代理:
git config --global http.<URL>.proxy socks://localhost:7890
注意,HTTP 和 HTTPS 都使用
http.<URL>.proxy
,设为https.<URL>.proxy
是无效的。
例如:
git config --global http.http://github.com.proxy socks://localhost:7890 # 代理到 http://github.com
git config --global http.https://github.com.proxy socks://localhost:7890 # 代理到 https://github.com
[user]
email = hubenchang0515@outlook.com
name = planc
[http "http://github.com"]
proxy = socks://localhost:7890
[http "https://github.com"]
proxy = socks://localhost:7890
SSH
使用 SSH 认证时,只能通过 SSH 的配置文件 ~/.ssh/config
配置代理:
Host github.com
Hostname github.com
ServerAliveInterval 55
ForwardAgent yes
ProxyCommand nc -x localhost:7890 %h %p
nc
是一个命令行程序,用来进行转发。也可以使用connect
、socat
、corkscrew
等其他程序,参数的格式也要相应的改变。
HTTP(S) 与 SOCKS 代理的区别
首先要区分的是代理类型中 http
和代理地址中 http
含义的区别。
前者表示 HTTP 协议的传输使用的代理,而后者表示代理使用协议。两者之间并没有任何联系。
例如:
http_proxy="http://localhost:7890
表示 HTTP 协议的传输通过 localhost 的 7890 端口进行代理,代理协议为 HTTP。
https_proxy="http://localhost:7890
表示 HTTPS 协议的传输通过 localhost 的 7890 端口进行代理,代理协议为 HTTP。
http_proxy="socks://localhost:7890
表示 HTTP 协议的传输通过 localhost 的 7890 端口进行代理,代理协议为 SOCKS5。
https_proxy="socks://localhost:7890
表示 HTTPS 协议的传输通过 localhost 的 7890 端口进行代理,代理协议为 SOCKS5。
示例
代理类型为 HTTP,代理协议为 HTTP,发起 HTTP 请求:
$ export http_proxy=http://localhost:7890
$ curl http://www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1681626703180139&usg=AOvVaw0LzDFyyegwkb2RnuEO-nn1">here</A>.
</BODY></HTML>
代理类型为 HTTP,代理协议为 SOCKS5,发起 HTTP 请求:
$ export http_proxy=socks://localhost:7890
$ curl http://www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1681626703180139&usg=AOvVaw0LzDFyyegwkb2RnuEO-nn1">here</A>.
</BODY></HTML>
代理类型为 HTTPS,代理协议为 HTTP,发起 HTTPS 请求:
$ export https_proxy=http://localhost:7890
$ curl https://www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1681626703180139&usg=AOvVaw0LzDFyyegwkb2RnuEO-nn1">here</A>.
</BODY></HTML>
代理类型为 HTTPS,代理协议为 SOCKS5,发起 HTTPS 请求:
$ export https_proxy=socks://localhost:7890
$ curl https://www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1681626703180139&usg=AOvVaw0LzDFyyegwkb2RnuEO-nn1">here</A>.
</BODY></HTML>
也可以不写明代理协议,代理软件会自动判断:
$ export http_proxy=localhost:7890
$ curl http://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
$ export https_proxy=localhost:7890
$ curl https://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>
KDE 上代理设置不生效的问题
KDE 的系统设置中已经提示了,一些应用程序可能不会使用这个代理。因为在 Linux 上有很多配置代理的方式,不同的应用程序可能读取了不同的配置。 而 KDE 的系统设置使用的是自家的 KConfig,基本只有 KDE 自己支持,很少有其他应用程序兼容。
$ kreadconfig5 --file kioslaverc --group 'Proxy Settings' --key httpProxy
localhost 7890
$ cat /home/planc/.config/kioslaverc
ProxyUrlDisplayFlags=15
[Proxy Settings]
NoProxyFor=
Proxy Config Script=
ProxyType=1
ReversedException=false
ftpProxy=localhost 7890
httpProxy=localhost 7890
httpsProxy=localhost 7890
socksProxy=localhost 7890
大部分应用程序采用 gsettings 中的代理配置:
$ gsettings list-recursively | grep proxy
org.gnome.evolution.shell.network-config proxy-type 0
org.gnome.evolution.shell.network-config use-http-proxy false
org.gnome.system.proxy autoconfig-url ''
org.gnome.system.proxy ignore-hosts ['localhost', '127.0.0.0/8', '::1']
org.gnome.system.proxy mode 'manual'
org.gnome.system.proxy use-same-proxy true
org.gnome.system.proxy.ftp host 'localhost'
org.gnome.system.proxy.ftp port 7890
org.gnome.system.proxy.http authentication-password ''
org.gnome.system.proxy.http authentication-user ''
org.gnome.system.proxy.http enabled false
org.gnome.system.proxy.http host 'localhost'
org.gnome.system.proxy.http port 7890
org.gnome.system.proxy.http use-authentication false
org.gnome.system.proxy.https host 'localhost'
org.gnome.system.proxy.https port 7890
org.gnome.system.proxy.socks host 'localhost'
org.gnome.system.proxy.socks port 7890
少数应用程序使用环境变量中的代理配置:
$ env | grep proxy
no_proxy=localhost,127.0.0.0/8,::1
ftp_proxy=http://localhost:7890/
https_proxy=http://localhost:7890/
http_proxy=http://localhost:7890/
all_proxy=socks://localhost:7890/
NPM 镜像源配置
# 当前用户
npm config set registry https://registry.npmmirror.com
# ROOT
sudo npm config set registry https://registry.npmmirror.com
发生的问题
错误消息
npm ERR! Cannot read property 'insert' of undefined
这是因为配置了错误的地址,下载失败导致的。
解决办法
# 当前用户
npm cache clear --force
npm config set registry https://registry.npmmirror.com
# ROOT
sudo npm cache clear --force
sudo npm config set registry https://registry.npmmirror.com
Parsec 代理配置
Windows 平台上配置文件路径如下:
- 用户安装:
%appdata%\Parsec\config.txt
- 系统安装:
%ProgramData%\Parsec\config.txt
在配置文件中写入代理配置:
app_proxy = true
app_proxy_scheme = http
app_proxy_address = localhost
app_proxy_port = 7890
Systemd 服务的代理配置
以 Docker 为例,创建或编辑 /etc/systemd/system/docker.service.d/http-proxy.conf
文件,写入以下内容:
[Service]
Environment="ALL_PROXY=http://192.168.1.100:7890"
Environment="HTTP_PROXY=http://192.168.1.100:7890"
Environment="HTTPS_PROXY=http://192.168.1.100:7890"
然后重启:
sudo systemctl daemon-reload
sudo systemctl restart docker
在树莓派上配置 Clash 代理
编译 clash 源码
git clone https://github.com/Dreamacro/clash.git
cd clash
go build
sudo cp clash /usr/bin
如果遇到依赖包无法下载的问题,可以配置 GOPROXY,参考 goproxy.io。
$ export GOPROXY=https://proxy.golang.com.cn,direct
如果遇到
package embed is not in GOROOT
的问题, 这是因为 debian bullseye 的 golang 版本为 1.15,而 embed 是 1.16 版本新增的。可以安装 bullseye-backports 的版本。
$ apt policy golang-go golang-go: Installed: 2:1.15~1 Candidate: 2:1.15~1 Version table: 2:1.19~1~bpo11+1 100 100 https://mirrors.tuna.tsinghua.edu.cn/debian bullseye-backports/main arm64 Packages 100 /var/lib/dpkg/status *** 2:1.15~1 500 500 https://mirrors.tuna.tsinghua.edu.cn/debian bullseye/main arm64 Packages $ sudo apt install golang-go=2:1.19~1~bpo11+1 golang-src=2:1.19~1~bpo11+1
编辑配置文件
配置文件的文件名默认为 config.yaml
,可以放在任何路径。本文放在 /etc/clash
目录中。
启动 clash
$ clash -d /etc/clash
如果遇到
Can't find MMDB, start download
并且下载失败的问题,可以手动下载并放到配置文件目录中。$ wget https://cdn.jsdelivr.net/gh/Dreamacro/maxmind-geoip@release/Country.mmdb -P /etc/clash/
配置 systemd service
创建 /usr/lib/systemd/system/clash.service
文件:
如果要配置为用户级服务,则文件路径为
/usr/lib/systemd/user/clash.service
。后续命令需要附加
--user
选项,且不使用sudo
。
[Unit]
Description=Clash proxy
After=syslog.target systemd-user-sessions.service
[Service]
Type=simple
ExecStart=/usr/bin/clash -d /etc/clash
ExecStop=pkill clash
Restart=on-failure
RestartSec=2
[Install]
WantedBy=multi-user.target
更新配置
$ sudo systemctl daemon-reload
启动 clash
$ sudo systemctl start clash.service
查看启动是否成功
$ sudo systemctl status clash.service
设置自动启动
$ sudo systemctl enable clash.service
配置代理
gsettings
$ gsettings set org.gnome.system.proxy mode manual
$ gsettings set org.gnome.system.proxy.http host localhost
$ gsettings set org.gnome.system.proxy.http port 7890
$ gsettings set org.gnome.system.proxy.http enabled true
$ gsettings set org.gnome.system.proxy use-same-proxy true
环境变量
echo 'export all_proxy=localhost:7890' >> ~/.bashrc