ubuntu terminal 设置代理
默认proxy_ip: 127.0.0.1
默认proxy_port:7897
设置临时环境变量代理
在终端中直接设置环境变量,仅对当前会话有效。关闭终端后失效:
export http_proxy="http://127.0.0.1:7897"
export https_proxy="http://127.0.0.1:7897"
将proxy_ip
和proxy_port
替换为实际代理服务器地址和端口。若需认证可添加用户名密码:
export http_proxy="http://username:password@proxy_ip:proxy_port"
修改配置文件永久生效
编辑~/.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”
}
}
注意事项
- 使用SOCKS5代理时需要额外工具如
tsocks
或proxychains
- 局域网代理需确认防火墙设置
- 部分命令如
ping
不受代理影响,需单独配置