linux服务器使用tsocks设置代理

上一篇 / 下一篇  2016-08-17 10:38:17 / 个人分类:linux


一、tosocks介绍:
官网:
http://tsocks.sourceforge.net

tsocks概述:
SOCKS servers are a form. of proxy that are commonly used in firewalled LAN environments to allow access between networks, and often to the Internet. The problem is that most applications don't know how to gain access through SOCKS servers. This means that network based applications that don't understand SOCKS are very limited in networks they can reach. An example of this is simple 'telnet'. If you're on a network firewalled from the internet with a SOCKS server for outside access, telnet can't use this server and thus can't telnet out to the Internet.
tsocks' role is to allow these non SOCKS aware applications (e.g telnet, ssh, ftp etc) to use SOCKS without any modification. It does this by intercepting the calls that applications make to establish network connections and negotating them through a SOCKS server as necessary.

linux上,如果只是做http或ftp的代理,可以简单的设置环境变量:http_proxy、https_proxy、ftp_proxy;
这里使用tsocks,是为了解决ssh通过代理连接远程服务器

二、下载安装
从官网下载最新版本,本文使用的是:tsocks-1.8beta5.tar.gz

tar xzvf tsocks-1.8beta5.tar.gz
cd tsocks-1.8
./configure
make && make install

确认是否存在:/usr/lib/libtsocks.so文件,如果没有,cp -af libtsocks.so.1.8 /usr/lib/libtsocks.so
注:tsocks-1.8包里的libtsocks.so,只是libtsocks.so.1.8的软链接,复制实际文件过去以免安装目录删除,导致不可用。

三、配置tsocks
tar.gz包中有两个配置文件模板:
tsocks.conf.simple.example
tsocks.conf.complex.example
复制一个到/etc/tsocks.conf

样例:
# cat /etc/tsocks.conf             
# This is the configuration for libtsocks (transparent socks)
# Lines beginning with # and blank lines are ignored
#
# The basic idea is to specify:
#       - Local subnets - Networks that can be accessed directly without
#                         assistance from a socks server
#       - Paths - Paths are basically lists of networks and a socks server
#                 which can be used to reach these networks
#       - Default server - A socks server which should be used to access
#                          networks for which no path is available
# Much more documentation than provided in these comments can be found in
# the man pages, tsocks(8) and tsocks.conf(8)

# Local networks
# For this example this machine can directly access 192.168.0.0/255.255.255.0
# (192.168.0.*) and 10.0.0.0/255.0.0.0 (10.*)

local = 192.168.0.0/255.255.0.0
local = 10.0.0.0/255.0.0.0
local = 211.211.211.0/255.255.255.224

# Default server
# For connections that aren't to the local subnets or to 150.0.0.0/255.255.0.0
# the server at 192.168.0.1 should be used (again, hostnames could be used
# too, see note above)

server = 211.211.211.211 #代理服务器IP
# Server type defaults to 4 so we need to specify it as 5 for this one
server_type = 5 #代理服务器使用socks5版本
# The port defaults to 1080 but I've stated it here for clarity
server_port = 1080 #代理服务器端口

default_user = 代理帐号用户名
default_pass = 代理帐号密码



这里配置所有的都使用默认的代理服务器
也可以使用path来指定部分走其他代理服务器,具体参见:tsocks.conf.complex.example模板中的配置及说明;
模板中:local = 192.168.0.0/255.255.255.0 子网掩码配置错误,应修改为255.255.0.0

四、使用tsocks
源码安装的tsocks,默认路径为:/usr/bin/tsocks
tsocks [application [applications arguments]]
tsocks [on|off]

举个实例吧:
tsocks firefox  #之后在firefox上浏览网页就会默认使用tsocks配置的代理


source tsocks on #设置当前shell的tsocks
tsocks show    #查看当前Shell的tsocks设置,LD_PRELOAD="/usr/lib/libtsocks.so";如果LD_PRELOAD为空,表示没设置成功;
do_something   #这里就能默认使用tsocks的代理去do something了,前面已经设置了
source tsocks off #关闭当前shell的tsocks代理设置
tsocks show    #再次查看,LD_PRELOAD="".

查看/usr/bin/tsocks文件,能发现tsocks on 只是设置LD_PRELOAD环境变量,tsocks off只是去除LD_PRELOAD环境变量的设置;
所以如果想让所有用户都可以使用tsocks配置,直接在/etc/profile中设置:export LD_PRELOAD="/usr/lib/libtsocks.so"
再source /etc/profile,让所有用户都生效;


五、遇到的问题及解决方案
10:26:43 libtsocks(31015): Need a password in tsocks.conf or $TSOCKS_PASSWORD to authenticate withssh: connect to host 151.151.151.151 port 22: Connection refused
使用的代理服务器,需要帐号密码进行身份校验,增加default_user及default_pass配置解决;

10:30:45 libtsocks(31032): SOCKS server 211.211.211.211 (211.211.211.211) is not on a local subnet!
设置的代理没有与/etc/tsocks.conf文件中的local设置的网络在同一个子网中。配置:local = 211.211.211.0/255.255.255.224解决;

ERROR: ld.so: object '/usr/lib/libtsocks.so' from LD_PRELOAD cannot be preloaded: ignored.
.so库文件不存在,从安装包中复制过去:cp -af libtsocks.so.1.8 /usr/lib/libtsocks.so




TAG: Linux 代理 代骂 服务器 tsocks

 

评分:0

我来说两句

Open Toolbar