通过Remote Forward让无联网权限服务器联网

首先回顾一下remote forward的作用:将本地服务转发出去,以SSH服务器为媒介,外部访问SSH服务器的特定端口,流量会转发到本地端口上

参考资料:https://unix.stackexchange.com/questions/116191/give-server-access-to-internet-via-client-connecting-by-ssh

1. 安装Squid (以Ubuntu为例)

1
2
3
4
5
6
7
8
# squid的默认监听端口是`3128`
sudo apt install squid

# Make sure the service is started
sudo service squid start

# 编辑配置文件
sudo vim /etc/squid/squid.conf

在配置文件中注释掉: http_access deny all, 并且添加: http_access allow all

2. 如果Host使用了代理(譬如需要访问Google服务等内容):

有一点需要注意,在squid中加入代理设置会和clash的代理冲突,导致clash软件出现一些奇怪BUG, 或者导致squid的转发出现中断; 这时需要重新启动clash,然后重新建立转发隧道

假设Host使用的代理为:127.0.0.1:7890
那么需要在配置文件/etc/squid/squid.conf中添加以下内容:

1
2
3
4
refresh_pattern (Release|Packages(.gz)*)$ 0 20% 2880
cache_peer 127.0.0.1 parent 7890 0 no-query default
ever_direct deny local-servers # 本地的域名直连
never_direct allow all

后面还需要sudo service squid restart重启一下服务,同时记得暂时断开所有使用了squid服务的SSH连接。

3. 在Server上配置:

下面这个可以写进配置文件里,在登陆用户时使得该配置自动生效;也可以就在终端临时配置;

1
2
export http_proxy="http://127.0.0.1:3129"
export https_proxy="http://127.0.0.1:3129"

4. Remote forward

1
ssh -R 3129:localhost:3128 user@server_ip

也可以使用autossh来建立一个持久化的SSH隧道,具体可见:https://wiki.gentoo.org/wiki/Autossh

1
2
# 需要配好密钥,使得其可以免密登陆
autossh -M 20000 -f -NT -R 3129:localhost:3128 user@server_id

通过Remote Forward让无联网权限服务器联网
https://www.torch-fan.site/2023/02/09/通过Remote-Forward让无联网权限服务器联网/
作者
Torch-Fan
发布于
2023年2月9日
更新于
2023年2月19日
许可协议