Linux下常用网络工具的代理设置方法
2020-01-10 12:45:33
宗兆伟
我们 经常会碰到网络隔离或网速慢的情况,这时候就需要设置代理访问网络。
代理的分类及使用场景
网络代理分为两 类 , (正向) 代理和反向代理。
简单来说,
代理是指代理客户端向服务器端发出请求,服务器端看到的是代理服务器的IP地址。
反向代理是指代理服务器端,客户端访问的是代理服务器的地址,服务器一般隐藏于代理服务器的后面不可见。
代理软件
常用的代理服务器最著名的是squid,开源且强大,除了做网络代理还经常用作数据缓存 , SSL Offloading 。 它可以实现socks5 和http 两种协议的代理服务器。
反向代理服务器最常见的非Nginx莫属,可以用于生产环境下大规模集群环境的代理。
下边的内容仅讲述Squid HTTP 代理服务器的搭建及常用Linux 命令的代理访问方法。
搭建可移植代理
将Squid 搭建在Docker中可以实现良好的移植性 。搭建方法参考:
https://github.com/zongzw/squid-webproxy/
其中实现了 Squid 代理服务器的一键拉起,可以通过配置文件的方式设置代理的访问密码。
常见Linux 命令的代理设置方法
下文中所提到的 10.250.64.101:3128 是通过以上搭建方法搭建出来的示例代理服务器。
wget
wget 命令可以通过"-e" 添加代理环境变量 。例如
wget -e http_proxy=10.250.64.101:3128 -e https_proxy=10.250.64.101:3128 \ https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname-s)-$(uname -m)
curl
与wget类似 ,使用 “-x” 的方式设置代理 。如果需要用户名密码,格式为 curl -x " http://user:pwd@host:port" www.baidu.com
举例:
curl -x http://10.250.64.101:3128 \ -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o docker-compose
git
使用"git config --global" 的方式设置代理, 例如 :
git config --global http.proxy http://10.250.64.101:3128 git config --global https.proxy http://10.250.64.101:3128
docker
docker 使用代理的方式相对特殊 。
docker的代理访问分docker container内的代理访问 (如 docker内运行 wget curl 等网络命令 ) 和docker 命令的代理访问 (如docker pull )。
docker 内代理访问的配置可以参见以下链接 : https://docs.docker.com/network/proxy/
即 编辑 ~/.docker/config.json 填入以下内容 ,不需要重启docker engine 。
{ "proxies": { "default": { "httpProxy": "http://10.250.64.101:3128", "httpsProxy": "http://10.250.64.101:3128", "noProxy": "*.test.example.com,.example2.com" } } }
docker 的 命令(push pull ..)使用代理的配置方法可以参考以下链接 : https://docs.docker.com/config/daemon/systemd/
即:
创建目录 /et c/systemd/system/docker.service.d
增加代理配置文件 :
cat /etc/systemd/system/docker.service.d/http_proxy.conf [Service] Environment="HTTP_PROXY=http://10.250.64.101:3128/" cat /etc/systemd/system/docker.service.d/http_proxy.conf [Service] Environment="HTTPS_PROXY=http://10.250.64.101:3128/"
重启docker engine
sudo systemctl daemon-reload sudo systemctl restart docker
ssh
Windows 下使用带有gui的ssh工具会提供proxy的配置 ,而在linux下使用openssh + 代理需要借助辅助工具connect-proxy 或nc 。
connect-proxy 需要单独安装 ,使用yum 的 安装方法为: (ubuntu下apt方法类似。)
yum install epel-release yum install connect-proxy
ssh 命令为 :
ssh root@10.250.18.183 -o "ProxyCommand=connect-proxy -H 10.250.64.101:3128 %h %p"
其中,-o 指明ssh 选项 ,如同 ssh常用选项配置“-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null”类似 。
yum
yum 使用代理服务器的方法比较简单 ,但是注意 http_proxy https_proxy 为小写 。
例如:
export http_proxy=http://10.250.64.101:3128 https_proxy=http://10.250.64.101:3128 yumupdate -y …
apt
配置方法 为 :
-o Acquire::http::proxy="http://10.250.64.101:3128/" -o Acquire::https::proxy="http://10.250.64.101:3128/"
例如 :
sudo apt-get -o Acquire::http::proxy="http://10.250.64.101:3128/" -o Acquire::https::proxy="http://10.250.64.101:3128/" update
验证代理配置生效的方法
可以在代理服务器端抓包检测:
tcpdump-i utun2 tcp port 3128 -n
在下载命令执行时使用以上命令会看到大量经过3128的数据包 。
发布评论 加入社群
相关文章

国内环境下ubuntu22.04+kubeadm搭建v1.27.2多节点k8s集群
宗兆伟
2023-06-16 07:12:11 277

更改 kibana 中图表的 index-pattern
李煜峰
2020-05-18 09:41:35 2046

Nginx内存池现实机制
皮皮鲁
2020-05-17 19:32:13 720

回复评论
发布评论