Linux下有很多程序都只有命令行接口,对于这类程序,它们通过代理服务器(proxy)访问网络的方式也不尽相同。在本文中Easwy总结了一些常用Linux程序配置代理服务器的方法。
[ 通用代理服务器配置 ]
对于大多数Linux控制台程序,例如Debian或Ubuntu中的apt-get和aptitude命令、git命令、wget命令,这些程序都使用http_proxy和ftp_proxy环境变量来获取代理服务的配置。
方法是在你的~/.bashrc里加上类似下面的话:
export http_proxy=http://username:password@proxyserver:port/ export ftp_proxy=http://username:password@proxyserver:port/
如果你的代理服务器需要用户名和密码才能访问,需要填写上面的username和passwd部分,否则的话,省略这两部分。
例如,假设你的代理服务器为192.168.1.1,端口是8080,用户名为easwy,密码是123456,那么应该这样设置这两个环境变量:
export http_proxy=http://easwy:[email protected]:8080 export ftp_proxy=http://easwy:[email protected]:8080
这样配置之后,退出再登录一次,或者直接使用下面的命令source一下.bashrc:
source ~/.bashrc
现在,上述程序就可以通过代理服务器访问网络了。
[ subversion的代理服务器配置 ]
要配置subversion的代理服务器,需要修改$HOME/.subversion/servers文件,在此文件的[global]段加上:
http-proxy-host = 192.168.1.1 http-proxy-port = 8080 http-proxy-username = easwy http-proxy-password = 123456
现在svn就可以使用代理服务器访问版本库了。
[ yum的代理服务器配置 ]
如果想让CentOS中的yum可以通过代理服务器更新程序,则需要修改文件/etc/yum.conf,在此文件中加上:
proxy=http://easwy:[email protected]:8080
有些局域网环境上网需要使用代理上网,图形界面的很好解决就设置一下浏览器的代理就好了,但是Linux纯命令行的界面就需要手动配置了。
如果要全局用户使用应用于所有的Shell,就需要修改 /etc/profile 文件
1 # vi /etc/profile
在文件中添加以下配置
1 http_proxy=proxy.abc.com:8080 2 https_proxy=$http_proxy 3 ftp_proxy=user:[email protected]:8080 4 no_proxy=*.abc.com,10.*.*.*,192.168.*.*,*.local,localhost,127.0.0.1 5 export http_proxy https_proxy ftp_proxy no_proxy
其中:
http_proxy:http协议使用代理服务器地址;
https_proxy:https协议使用安全代理地址;
ftp_proxy:ftp协议使用代理服务器地址;
user:代理使用的用户名;
password:代理使用用户名的密码;
proxy.abc.com:代理地址,可以是IP,也可以是域名;
8080:使用的端口;
no_proxy:不使用代理的主机或IP。
保存退出,注销重新登陆系统即可生效。
此方法只适合配置http代理,使用socket代理上网的另有其他配置方法。
server端需要安装、配置shadowsocks,启动ss-server
1,安装shadowsocks
1
2
|
yum
install
python-pip
pip
install
shadowsocks
|
2,配置shadowsocks
创建shadowsocks配置文件
1
2
3
4
5
6
7
8
9
10
|
[root@AliCloudInstance opt]
# cat /etc/shadowsocks.json
{
"server"
:
"47.91.1xx.1xx"
,
"server_port"
:10666,
"local_port"
:1080,
"password"
:
"MyPassWord"
,
"timeout"
:600,
"method"
:
"aes-256-cfb"
}
[root@AliCloudInstance opt]
#
|
3,启动ss-server
可以同步命令行使用ss-server指定配置文件直接启动
1
|
ssserver -c
/etc/shadowsocks
.json
|
不过还是推荐放到supervisor中启动,如果没有supervisor,通过yum或者pip安装下即可
1
2
3
4
5
6
7
8
9
|
[root@AliCloudInstance opt]
# tail -7 /etc/supervisord.conf
[program:shadowsocks]
command
=ssserver -c
/etc/shadowsocks
.json
autostart=
true
autorestart=
true
user=root
log_stderr=
true
logfile=
/var/log/shadowsocks
.log
[root@AliCloudInstance opt]
#
|
shadowsocks服务端到此配置结束,启动后会监听上面配置的端口,此时就可以通过各种ss客户端进行连接使用了。
1
2
3
4
5
|
[root@AliCloudInstance opt]
# ss -lntup|grep ssserver
udp UNCONN 0 0 *:56925 *:*
users
:((
"ssserver"
,5497,6))
udp UNCONN 0 0 47.91.1xx.1xx:10666 *:*
users
:((
"ssserver"
,5497,4))
tcp LISTEN 0 128 47.91.1xx.1xx:10666 *:*
users
:((
"ssserver"
,5497,3))
[root@AliCloudInstance opt]
#
|
Client端
cilent端需要安装、配置shadowsocks,启动sslocal,安装配置polipo或者proxychains
1,安装、配置shadowsocks(同server端)
1
2
|
yum
install
python-pip
pip
install
shadowsocks
|
2,配置shadowsocks
创建shadowsocks配置文件
1
2
3
4
5
6
7
8
9
10
|
[root@thatsit opt]
# cat /etc/shadowsocks.json
{
"server"
:
"47.91.1xx.1xx"
,
"server_port"
:10666,
"local_port"
:1080,
"password"
:
"MyPassWord"
,
"timeout"
:600,
"method"
:
"aes-256-cfb"
}
[root@thatsit opt]
#
|
3,启动ss客户端
1
2
3
4
5
|
[root@thatsit polipo]
# sslocal -c /etc/shadowsocks.json -d start
INFO: loading config from
/etc/shadowsocks
.json
2017-02-28 23:06:48 INFO loading libcrypto from libcrypto.so.10
started
[root@thatsit polipo]
#
|
启动之后,本地会监听一个tcp的1080端口和一个udp的1080端口
1
2
3
4
|
[root@thatsit polipo]
# ss -lntup|grep sslocal
udp UNCONN 0 0 127.0.0.1:1080 *:*
users
:((
"sslocal"
,24079,5))
tcp LISTEN 0 128 127.0.0.1:1080 *:*
users
:((
"sslocal"
,24079,4))
[root@thatsit polipo]
#
|
4,安装polipo将socks5协议转为http协议
Shadowsocks使用socks5协议,而终端很多工具目前只支持http和https等协议,所以我们为终端设置Shadowsocks的思路就是将socks5协议转换成http协议,然后为终端设置即可。
①安装polipo(github地址:https://github.com/jech/polipo)
1
2
3
4
|
git clone https:
//github
.com
/jech/polipo
.git
cd
polipo
make
all
make
install
|
②配置polipo
1
2
3
4
5
6
7
|
[root@thatsit polipo]
# cat /etc/polipo/config
socksParentProxy =
"127.0.0.1:1080"
socksProxyType = socks5
logFile =
/var/log/polipo
logLevel = 99
logSyslog =
true
[root@thatsit polipo]
#
|
polipo -v 可以查看支持的选项
③启动polipo:
1
|
/root/polipo/polipo
-c
/etc/polipo/config
|
polipo监听8123端口
1
2
3
|
[root@thatsit polipo]
# ss -lntup|grep polipo
tcp LISTEN 0 128 127.0.0.1:8123 *:*
users
:((
"polipo"
,23399,5))
[root@thatsit polipo]
#
|
④添加http_proxy环境变量,实现全局代理
当前会话生效
1
|
export
http_proxy=http:
//localhost
:8123
|
取消此环境变量,关闭代理
1
|
unset
http_proxy
|
同时可以将下面环境变量设置的语句添加到/etc/profile、~/.bashrc等文件来实现更大的代理范围
1
|
export
http_proxy=http:
//127
.0.0.1:8123
|
⑤测试
1
2
3
|
[root@thatsit ~]
# curl ip.gs
当前 IP:47.91.1xx.1xx 来自:美国加利福尼亚州圣克拉拉 阿里云
[root@thatsit ~]
#
|