默认proxy_ip: 127.0.0.1
默认proxy_port:7897

设置临时环境变量代理

在终端中直接设置环境变量,仅对当前会话有效。关闭终端后失效:
proxy_ipproxy_port替换为实际代理服务器地址和端口。若需认证可添加用户名密码:

export http_proxy="http://username:password@proxy_ip:proxy_port"

例如:

export http_proxy="http://127.0.0.1:7897"
export https_proxy="http://127.0.0.1:7897"
export all_proxy="socks://127.0.0.1:7897/"

修改配置文件永久生效

编辑~/.bashrc~/.zshrc文件(取决于使用的shell):

nano ~/.bashrc

在文件末尾添加以下内容:

export http_proxy="http://proxy_ip:proxy_port"
export https_proxy="http://proxy_ip:proxy_port"
export ftp_proxy="http://proxy_ip:proxy_port"
export no_proxy="localhost,127.0.0.1"

保存后执行使配置立即生效:

source ~/.bashrc

为APT包管理器配置代理

单独为APT设置代理需编辑配置文件:

sudo nano /etc/apt/apt.conf.d/proxy.conf

添加以下内容(根据代理类型选择HTTP或HTTPS):

Acquire::http::Proxy "http://proxy_ip:proxy_port";
Acquire::https::Proxy "http://proxy_ip:proxy_port";

测试代理连接

使用curl测试HTTP代理:

curl -I http://www.google.com

测试HTTPS代理:

curl -I https://www.google.com

若返回200状态码表示代理生效。

取消代理设置

临时取消:

unset http_proxy https_proxy

永久取消需删除之前添加的环境变量或配置文件内容。

使用wget和curl的临时代理设置

对于使用wget或curl的情况,你可以在命令行中直接指定代理:

wget --proxy=on --http-proxy=http://proxy_ip:proxy_port http://example.com
curl -x http://proxy_ip:proxy_port http://example.com

docker 代理设置

sudo vim /etc/docker/daemon.json
{
“proxies”: {
“http-proxy”: “http://127.0.0.1:7897”,
“https-proxy”: “http://127.0.0.1:7897”,
“no-proxy”: “localhost,127.0.0.0/8”
}
}

git 使用临时代理设置

git clone 命令里直接用 -c 指定代理
这样只对本次 clone 生效,不会影响其他 git 操作。

# use http/https proxy
git -c http.proxy=http://127.0.0.1:7897 -c https.proxy=http://127.0.0.1:7897 clone https://github.com/xxx/yyy.git
# use sock5 proxy
git -c http.proxy=socks5://127.0.0.1:7897 -c https.proxy=socks5://127.0.0.1:7897 clone https://github.com/xxx/yyy.git

注意事项

  • 使用SOCKS5代理时需要额外工具如tsocksproxychains
  • 局域网代理需确认防火墙设置
  • 部分命令如ping不受代理影响,需单独配置
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐