2021年11月3日星期三

Linux 让终端走代理的几种方法

一、正向代理服务器设置:

安装 TinyProxy

1. 通过软件包安装

TinyProxy 目前已支持大多数发行版通过软件包安装,下面介绍下比较常用的几个平台的安装方式。

Ubuntu / Debian

$ sudo apt-get -y install tinyproxy

配置 TinyProxy

TinyProxy 默认配置文件路径为 /etc/tinyproxy/tinyproxy.conf。如果你要自定义配置文件位置,可以在启动 TinyProxy 时 通过 -c 参数来指定。

3. 代理访问互联网的主要修改配置如下:

如果你想允许所有人使用该代理,注释 Allow 选项即可。

Port 8888#Allow 127.0.0.1
#Allow 192.168.0.0/16
#Allow 172.16.0.0/12
#Allow 10.0.0.0/8
Allow 0.0.0.0/0

运行 TinyProxy

• 运行 TinyProxy 非常简单,使用官方提供的脚本即可。

# 启动 TinyProxy
$ service tinyproxy start

# 停止 TinyProxy
$ service tinyproxy stop

# 允许开机启动 TinyProxy
$ sudo systemctl enable tinyproxy.service

# 重启 TinyProxy
$ service tinyproxy restart

• 如果服务器有启用防火墙,记得开放相应的 TinyProxy 端口

$ iptables -I INPUT -p tcp –dport 8888 -j ACCEPT

• 测试代理是否正常工作,示例:

$ curl --proxy 192.168.1.1:8888 -k https://cip.cc/

如果出现对应网页的源代码,则证明代理工作正常。

二、代理设置:

科学上网方式客户端方式:详:Linux系统 v2rayA 安装

Linux 让终端及apt走代理的几种方法

方法一:(推荐使用)

 命令 curl cip.cc 检查终端是否处于代理状态。

为什么说这个方法推荐使用呢?因为他只作用于当前终端中,不会影响环境,而且命令比较简单

在终端中直接运行:

export http_proxy=http://proxyAddress:port

如果你是SSR,并且走的http的代理端口是1081,想执行wget或者curl来下载国外的东西,可以使用如下命令:

export http_proxy=http://127.0.0.1:1080

如果是https那么就经过如下命令:

export https_proxy=http://127.0.0.1:1080

方法二 :

这个办法的好处是把代理服务器永久保存了,下次就可以直接用了

把代理服务器地址写入shell配置文件.bashrc或者.zshrc 直接在.bashrc或者.zshrc添加下面内容

export http_proxy="http://localhost:port"
export https_proxy="http://localhost:port"

或者走socket5协议(ss,ssr)的话,代理端口是1080

export http_proxy="socks5://127.0.0.1:1080"
export https_proxy="socks5://127.0.0.1:1080"

或者干脆直接设置ALL_PROXY

export ALL_PROXY=socks5://127.0.0.1:1080

最后在执行如下命令应用设置

source ~/.bashrc

或者通过设置alias简写来简化操作,每次要用的时候输入setproxy,不用了就unsetproxy。

 alias setproxy="export ALL_PROXY=socks5://127.0.0.1:1080" alias unsetproxy="unset ALL_PROXY"

方法三:

改相应工具的配置,比如为apt配置代理

sudo vim /etc/apt/apt.conf

在文件末尾加入下面这行

Acquire::http::Proxy "http://proxyAddress:port"
重点来了!!如果说经常使用git对于其他方面都不是经常使用,可以直接配置git的命令。

使用ss/ssr来加快git的速度

直接输入这个命令就好了

git config --global http.proxy 'socks5://127.0.0.1:1080' 
git config --global https.proxy 'socks5://127.0.0.1:1080'

方法四:

安装proxychains4
sudo apt install proxychains4
卸载
sudo apt-get --purge remove proxychains4
sudo apt autoremove proxychains4
修改参数:
sudo nano /etc/proxychains.conf
最后一行改为:
socks5 127.0.0.1 1080 ( 这里的1080就是socks5 代理端口)
测试proxychains4:
proxychains wget http://www.google.com

终端使用proxychains4:
proxychains bash

这就进入能自由联网的子命令行了看自己在不在子bash里
ps -f
显示3个进程:父zsh,本zsh,ps -f子进程.只显示两个则不在子进程里.
退出:
exit
执行exit会回到父命令行中.
源码编译方式安装:
你可以从proxychains-ng下载到源代码,编译后安装即可
git clone https://github.com/rofl0r/proxychains-ng
cd proxychains-ng
./configure
sudo make && make install