参考
内网服务器如何设置代理访问外网
描述
公司的服务器通常只能连接公司内网, 不能访问互联网, 这对配置 conda/docker 环境来说比较不方便. 如果有一台可以访问公司外网的服务器, 可以在外网服务器上配置 squid 代理服务, 再让内网服务器通过外网服务器的代理端口连接外网.
配置外网服务器
安装 squid 软件
1 2
| # 安装 squid (这里采用 yum 安装) $ yum install -y squid
|
修改 squid 配置文件
1 2 3 4 5 6
| # CD 到配置文件目录下 $ cd /etc/squid/ # 备份原始配置文件 $ cp squid.conf squid.conf_bak # 修改配置文件 $ vim squid.conf
|
1 2 3 4 5
| # 将 http_access deny all 注释修改为 http_access allow all # http_access deny all http_access allow all # 修改端口为代理的端口 http_port 10022
|
启动 squid 服务
1 2 3 4 5 6 7 8
| # 检查语法 $ squid -k parse # 初始化缓存空间 $ squid -z Squid is already running! Process ID 12948 # 启动 squid 服务 $ service squid start Redirecting to /bin/systemctl start squid.service
|
查看 squid 服务
1 2 3 4 5 6
| $ netstat -lnp | grep 10022 tcp6 0 0 :::10022 :::* LISTEN 12948/(squid-1) $ systemctl status squid ● squid.service - Squid caching proxy Loaded: loaded (/usr/lib/systemd/system/squid.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2024-06-26 18:52:32 CST; 1 months 24 days ago
|
配置内网服务器
如果没有 root 权限, 不能在 /etc/profile.d/ 文件夹下添加内容, 可以使用临时代理. 每次需要在新建终端中执行如下命令
1
| export http_proxy=http://xxx.xx.xx.xxx:port
|
如果有 root 权限, 可以一劳永逸地配置全局的代理
1 2
| # 在 /etc/profile.d/ 目录下新建 proxy.sh (命名无所谓) $ vim /etc/profile.d/proxy.sh
|
在 /etc/profile.d/proxy.sh 中添加如下内容
1 2 3 4 5 6
| IP = xxx.xx.xx.xxx # IP 为要连接的外网服务器 IP 地址, 可以通过 ifconfig 查看 PORT = 10022 # PORT 是 squid.conf 中配置的 http_port export http_proxy=http://${IP}:${PORT} export https_proxy=http://${IP}:${PORT} # 设置不代理的 IP 或者网址 export no_proxy="127.0.0.1, localhost"
|
source 执行 profile 脚本
检查环境变量
测试网络连接
1 2 3 4 5 6 7 8 9 10 11
| $ wget baidu.com --2024-08-20 14:55:33-- http://baidu.com/ Connecting to xxx.xx.xx.xxx:10022... connected. Proxy request sent, awaiting response... 200 OK Length: 81 [text/html] Saving to: ‘index.html’
index.html 100%[===================>] 81 --.-KB/s in 0s
2024-08-20 14:55:33 (9.15 MB/s) - ‘index.html’ saved [81/81] $ rm index.html
|
针对没有root权限的接解决办法
第一种是每次在新建终端中执行一次 export
1
| export http_proxy=http://xxx.xx.xx.xxx:port
|
(推荐)另一种是通过 docker 容器,新建一个 docker 容器作为自己的工作环境,在 docker 容器中用户拥有操作系统层面的全部权限,这样会更方便。
配置防火墙
如果 wget 失败, 提示如下信息
1 2 3
| $ wget baidu.com --2024-08-20 11:16:42-- http://baidu.com/ Connecting to xxx.xx.xx.xxx:10022... failed: No route to host.
|
大概率是外网服务器防火墙的原因. 以 centos7 为例, 它的默认防火墙是 firewalld, 如下配置 firewalld
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| # 查看防火墙状态 $ firewall-cmd --state running # 列出所有开放端口 $ firewall-cmd --zone=public --list-ports xxx99/tcp xxx22/tcp # 添加开放端口 $ firewall-cmd --zone=public --add-port=10022/tcp --permanent success # 重启防火墙 $ firewall-cmd --reload success # 检查端口是否开放 $ firewall-cmd --zone=public --query-port=10022/tcp yes
|
重新在内网服务器测试
重启代理
重启 soup-gpu13 上的 squid 代理服务
1 2 3 4 5 6 7 8
| # 检查语法 [root@soup-gpu13]/etc/squid# squid -k parse # 初始化缓存空间 [root@soup-gpu13]/etc/squid# squid -z 2024/08/20 11:01:56| Squid is already running! Process ID 12948 # 启动 squid 服务 [root@soup-gpu13]/etc/squid# service squid start Redirecting to /bin/systemctl start squid.service
|
查看 squid 服务
1 2 3 4 5 6
| [root@soup-gpu13]~# netstat -lnp | grep 10022 tcp6 0 0 :::10022 :::* LISTEN 12948/(squid-1) [root@soup-gpu13]~# systemctl status squid ● squid.service - Squid caching proxy Loaded: loaded (/usr/lib/systemd/system/squid.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2024-06-26 18:52:32 CST; 1 months 24 days ago
|
此时 xxx.xx.x.xx 可以 ping 通
1 2 3
| :~$ ping xxx.xx.x.xx PING xxx.xx.x.xx (xxx.xx.x.xx) 56(84) bytes of data. 64 bytes from xxx.xx.x.xx: icmp_seq=1 ttl=61 time=42.0 ms
|
但是 telnet 10022 端口失败
1 2 3
| :~$ telnet xxx.xx.x.xx 10022 Trying xxx.xx.x.xx... telnet: Unable to connect to remote host: No route to host
|
设置 soup-gpu13 (centos7) 防火墙 (默认 firewall)
1 2 3 4 5 6 7 8 9 10
| # 查看防火墙状态 [root@soup-gpu13]~# firewall-cmd --state # 列出所有开放端口 [root@soup-gpu13]~# firewall-cmd --zone=public --list-ports # 添加开放端口 [root@soup-gpu13]~# firewall-cmd --zone=public --add-port=10022/tcp --permanent # 重启防火墙 [root@soup-gpu13]~# firewall-cmd --reload # 检查端口是否开放 [root@soup-gpu13]~# firewall-cmd --zone=public --query-port=10022/tcp
|
重新进行测试, telenet 10022 端口成功
1 2 3 4
| :~$ telnet xxx.xx.x.xx 10022 Trying xxx.xx.x.xx... Connected to xxx.xx.x.xx. Escape character is '^]'.
|