shadowsocks-libev 是一个 shadowsocks 协议的轻量级实现,是 shadowsocks-android, shadowsocks-ios 以及 shadowsocks-openwrt 的上游项目。其具有以下特点:
体积小巧,静态编译并打包后只有 100 KB。
高并发,基于 libev 实现的异步 I/O,以及基于线程池的异步 DNS,同时连接数可上万。
低资源占用,几乎不占用 CPU 资源,服务器端内存占用一般在 3MB 左右。
跨平台,适用于所有常见硬件平台,已测试通过的包括 x86,ARM 和 MIPS。也适用于大部分 POSIX 的操作系统或平台,包括 Linux,OS X 和 gwin 等。
协议及配置兼容,完全兼容 shadowsocks 协议,且兼容标准实现中的 JSON 风格配置文件,可与任意实现的 shadowsocks 端或服务端搭配使用。
shadowsocks-libev 包括服务端和客户端两部分,一共三个模块。
ss-server:服务器端,部署在远程服务器,提供 shadowsocks 服务。
ss-local:客户端,提供本地 socks5 协议代理。
ss-redir:客户端,提供本地透明代理,需要与 NAT 配合使用,具体使用方法参见文档。

查看cpu信息: cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
当前cpu运行模式: getconf LONG_BIT
查看当前系统版本: cat /etc/redhat-release

1、安装各种库或支持文件:
yum groupinstall "Development tools" -y

yum install autoconf libssl-dev openssl openssl-devel make -y

yum install python-setuptools -y

yum install python-devel -y

easy_install greenlet

easy_install gevent

2.安装git
执行 yum install git -y
装完了执行 git --version ,如果出版本号则安装成功。

3、配置、编译、安装,逐一执行以下命令:
git clone git://github.com/madeye/shadowsocks-libev.git

cd shadowsocks-libev

./configure && make

make install

到这一步如果不发生错误,shadowsocks-libev 就安装成功了,输入 ss-server -h查看是否出现版本信息。

4.运行shadowsocks
方式一:
输入以下命令:

nohup /usr/local/bin/ss-server -s 服务器地址 -p 服务器端口 -k 密码 -m 加密方式 -d 8.8.8.8 -u > /dev/null 2>&1 &

[注:/dev/null :代表空设备文件
> :代表重定向到哪里,例如:echo "123" > /home/123.txt
1 :表示stdout标准输出 (the handle for standard output or STDOUT),系统默认值是1,所以">/dev/null"等同于"1>/dev/null"
2 :表示stderr标准错误 (the handle for standard error or STDERR)
& :表示等同于的意思,2>&1,表示2的输出重定向等同于1

1 > /dev/null 2>&1 语句含义:
1 > /dev/null : 首先表示标准输出重定向到空设备文件,也就是不输出任何信息到终端,说白了就是不显示任何信息。
2>&1 :接着,标准错误输出重定向(等同于)标准输出,因为之前标准输出已经重定向到了空设备文件,所以标准错误输出也重定向到空设备文件。在结尾加上"&"来将命令同时放入后台运行。]

#关于ss命令的用法:
ss-[local|redir|server|tunnel]

-s host name or ip address of your remote server

-p port number of your remote server

-l port number of your local server

-k password of your remote server

[-m ] encrypt method: table, rc4, rc4-md5,
aes-128-cfb, aes-192-cfb, aes-256-cfb,
bf-cfb, camellia-128-cfb, camellia-192-cfb,
camellia-256-cfb, cast5-cfb, des-cfb, idea-cfb,
rc2-cfb, seed-cfb, salsa20 and chacha20

[-f ] the file path to store pid

[-t ] socket timeout in seconds

[-c ] the path to config file

[-i ] network interface to bind,
not available in redir mode

[-b ] local address to bind,
not available in server mode

[-u] enable udprelay mode,
TPROXY is required in redir mode

[-L :] specify destination server address and port
for local port forwarding,
only available in tunnel mode

[-d ] setup name servers for internal DNS resolver,
only available in server mode

[--fast-open] enable TCP fast open,
only available in local and server mode,
with Linux kernel > 3.7.0

[--acl ] config file of ACL (Access Control List)
only available in local and server mode

[-v] verbose mode

notes:

ss-redir provides a transparent proxy function and only works on the
Linux platform with iptables.

方式二:
创建 config.json文件放到/usr/local/bin,编辑内容如下:

vim /usr/local/bin/config.json

{
"server":"服务器地址",
"server_port":端口,
"password":"密码",
"local_address":"127.0.0.1",
"timeout":300,
"method":"aes-256-cfb",
"fast_open":false,
"workers":1
}

如用vi编辑,:wq保存退出。

#各个字段的意思:
server 服务端的地址
server_port 服务端的端口
password 用于加密的密码
timeout 超时时间,单位秒
method 加密方法,推荐 “aes-256-cfb”
fast_open 是否使用 TCP_FASTOPEN, 需客户端配合
workers worker 数量,Unix/Linux 可用,如果不理解含义请不要改

输入
nohup /usr/local/bin/ss-server -c /usr/local/bin/config.json -f /tmp/ss-server.pid -u > /dev/null 2>&1 &

[参数 -c 指定配置文件, -f 创建pid文件。
关于pid文件:
(1) pid文件的内容:pid文件为文本文件,内容只有一行, 记录了该进程的ID。 用cat命令可以看到。
(2) pid文件的作用:防止进程启动多个副本。只有获得pid文件(固定路径固定文件名)写入权限(F_WRLCK)的进程才能正常启动并把自身的PID写入该文件中。其它同一个程序的多余进程则自动退出。]

完成后可输入ps -ef ,netstat -lnp 查看进程运行情况,是否正确启动并监听相应端口。

5.把 shadowsocks-libev 加入开机启动队列
编辑 rc.local,使用以下命令:
vim /etc/rc.d/rc.local

在文件最后新增一行,添加以下内容:
(1)若不使用配置文件config.json,输入

#ss-server
/usr/local/bin/ss-server -s 服务器地址 -p 服务器端口 -k 密码 -m 加密方式 -d 8.8.8.8 -u > /dev/null 2>&1 &

(2)若使用配置文件config.json,输入

#ss-server
/usr/local/bin/ss-server -c /usr/local/bin/config.json -f /tmp/ss-server.pid -u > /dev/null 2>&1 &

完成后:wq退出编辑器。

6.加入防火墙规则及保存。
iptables -I INPUT -p tcp --dport 服务器端口号 -j ACCEPT

service iptables save
完成后可输入iptables -L -n查看规则

7.增加守护进程(可选)
如果 ShadowSocks 经常掉线就在 /root/shadowsocks-libev/ 下新建一个 check.sh 文件,加入以下脚本

#!/bin/bash
if
ps -ef|grep “ss-server”|grep -v “grep”
then
echo “Running!”
else
echo “Stopped!”
nohup /usr/local/bin/ss-server -s 服务器地址 -p 服务器端口 -k 密码 -m 加密方式 -u > /dev/null 2>&1 &
fi

若有配置文件config.json则nohup一句改为:
nohup /usr/local/bin/ss-server -c /usr/local/bin/config.json -f /tmp/ss-server.pid -u > /dev/null 2>&1 &

保存后执行
chmod +x /root/shadowsocks-libev/check.sh
crontab -e
*/5 * * * * /root/shadowsocks-libev/chech.sh
即每 5 分钟执行一次监控脚本,如果 ShadowSocks 的服务不在运行就自动启动。

8.为了安全起见,我们使用非root用户运行ss(可选)

# 如果没有非root用户
# 我们现在就新建一个普通用户
useradd ssusr
# 为ssusr创建密码
passwd ssusr
> 输入两次密码
# 切换用户至ssusr
su - ssusr
# 在主目录创建第一个配置文件,配置格式请参考`使用配置文件运行`一节
vim ~/config1.json

# 保存完毕即可立即运行
/usr/local/bin/ss-server -c ~/config1.json -f /tmp/ss1.pid

# 设置开启自启动
# 我们先切换至root用户
su - root

# 接着在启动脚本里加入启动命令
# 指定ss以用户ssusr的权限运行
# 执行下面的命令时一定注意文件路径是否正确
echo "su - ssusr -c \"/usr/local/bin/ss-server -c /home/ssusr/config1.json -f /tmp/ss1.pid\"" >> /etc/rc.local


标签: none

添加新评论