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 配合使用,具体使用方法参见文档。
安装 ss-libev 前先查看相关系统信息:
查看cpu信息:cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
当前cpu运行模式: getconf LONG_BIT
查看当前系统版本: cat /etc/redhat-release
1、使用centOS 7,按以下步骤安装:
yum install epel-release -y
注意:
安装了 rpel 源后记得运行 yum repolist 一次,如果没有出现“epel/x86_64 Extra Packages for Enterprise Linux”字样,还需编辑 epel.repo 文件:vi /etc/yum.repos.d/epel.repo 把 [epel] 项里的 enabled=0 改为 enabled=1。
更新系统并安装关联文件:
yum upgrade –y
yum groupinstall ‘Development Tools’ -y
yum install gcc gettext autoconf libtool automake make pcre-devel asciidoc xmlto udns-devel libev-devel mbedtls-devel -y
注意不能用 yum 的方式安装 libsodium 或 libsodium-devel,因为 yum 库收录的版本太低(v1.05),ss-libev 并不支持,需要按以下方式手动编译安装:
2.安装 vim、git 和 wget
yum install vim git wget -y
完成后执行 git –version ,如果出版本号则安装成功。
3.安装 libsodium
git clone https://github.com/jedisct1/libsodium –branch stable
pushd libsodium
git submodule update –init –recursive
./autogen.sh
./configure && make
make install
popd
查看 libsodium的版本号并建立软链接:
ls -al /usr/local/lib/
ln -s /usr/local/lib/libsodium.so.18 /usr/lib/libsodium.so.18 (数字按实际版本号填写)
sudo ldconfig
扩展:
软件更新步骤
make uninstall //卸载
git submodule update –init –recursive
make clean //讀取 Makefile 中关于 clean 的工作,清除編譯過的目標檔案 (*.o)
./autogen.sh
./configure
make
make install
参考:
《git-submodule – Initialize, update or inspect submodules》
4、配置、编译、安装,逐一执行以下命令:
git clone https://github.com/shadowsocks/shadowsocks-libev.git
cd shadowsocks-libev
git submodule update –init –recursive
./autogen.sh
./configure
make
make install
到这一步如果不发生错误,shadowsocks-libev 就安装成功了,输入 ss-server -h 查看是否出现版本信息。
如果编译时显示autoconf版本过低(通常出现在centos6上),则需要手动安装autoconf:
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
tar -zxvf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure
make
make install
检查是否安装成功,输入命令:
autoconf –version
5、运行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)
& :m>&n 表示 文件描述符 m 重定向到文件描述符 n 所指向的文件。&n 也表明 n 是一个描述符(descriptor)而不是文件。
> /dev/null 2>&1 & 语句含义:
> /dev/null : 首先标准输出(stdout) 重定向到空设备文件,也就是不输出任何信息到终端,说白了就是不显示任何信息。
2>&1 :接着,标准错误输出 (stderr) 重定向到标准输出的指向文件,因为之前标准输出已经重定向到了空设备文件,所以标准错误输出也重定向到空设备文件。
第二个&:在结尾加上“&”来将命令同时放入后台运行。
参考:
tutorial" style="border: 0px;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;line-height: inherit;font-family: inherit;font-optical-sizing: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;padding: 0px;vertical-align: baseline;color: rgb(0, 155, 194)">http://wiki.bash-hackers.org/howto/redirection_tutorial (推荐细读,直接把原理讲透了)
http://wiki.bash-hackers.org/syntax/redirection
https://www.gnu.org/software/bash/manual/html_node/Redirections.html
http://sc.tamu.edu/help/general/unix/redirection.html
参考:ss命令参数
ss-[local|redir|server|tunnel|manager]
-s <server_host> host name or ip address of your remote server
-p <server_port> port number of your remote server
-l <local_port> port number of your local server
-k <password> password of your remote server
-m <encrypt_method> Encrypt method: rc4-md5, aes-128-gcm, aes-192-gcm, aes-256-gcm, aes-128-cfb, aes-192-cfb, aes-256-cfb, aes-128-ctr, aes-192-ctr, aes-256-ctr, camellia-128-cfb, camellia-192-cfb, camellia-256-cfb, bf-cfb, chacha20-poly1305, chacha20-ietf-poly1305 salsa20, chacha20 and chacha20-ietf.
[-f <pid_file>] the file path to store pid
[-t <timeout>] socket timeout in seconds
[-c <config_file>] the path to config file
[-i <interface>] network interface to bind, not available in redir mode
[-b <local_address>] local address to bind
[-u] enable udprelay mode, TPROXY is required in redir mode
[-U] enable UDP relay and disable TCP relay, not available in local mode
[-L <addr>:<port>] specify destination server address and port for local port forwarding, only available in tunnel mode
[-d <addr>] 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 <acl_file>] config file of ACL (Access Control List) only available in local and server mode
[–manager-address <addr>] UNIX domain socket address only available in server and manager mode
[–executable <path>] path to the executable of ss-server only available in manager mode
[–plugin <name>] Enable SIP003 plugin. (Experimental)
[–plugin-opts <options>] Set SIP003 plugin options. (Experimental)
[-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”:”0.0.0.0“,
“server_port”:42402,
“password”:”shadowsockspass”,
“local_address”:”127.0.0.1″,
“timeout”:3600,
“method”:”aes-192–gcm“,
“fast_open”:false,
}
编辑后,:wq 保存退出。
注解:在以上配置文件中,
定义了服务器的监听地址为任意地址:”server”: “0.0.0.0”,
定义了服务器端口为42402:”server_port”: 42402,
定义了客户端本地监听地址为127.0.0.1:”local_address”: “127.0.0.1”,
定义了客户端本地的监听端口为1080:”local_port”: 1080,
定义了密码为 shadowsockspass:”password”: “shadowsockspass”,
定义了连接超时的时间为3600秒:”timeout”: 3600,
定义了加密的方式为 aes-192-gcm:”method”: “aes-192-gcm”,
默认关闭了TCP_FASTOPEN属性:”fast_open”: false,
服务器监听地址不建议填本机外网ip,在某些系统中会出现 bind:cannot assign requested address 错误。
TCP fastopen 需客户端配合,且服务器和客户端的 Linux 版本均为 3.7.1或以上才支持。
config.json 配置多用户账号:
{
“server”:”my_server_ip”,
“port_password”:{
“8388”:”mypassword”,
“8389”:”mypassword”,
“8390”:”mypassword”
},
“timeout”:3600,
“method”:”aes-192-gcm“,
“fast_open”: false
}
参考:
《Configuration via Config File》
https://blessing.studio/why-do-shadowsocks-deprecate-ota/
试运行ss:
nohup /usr/local/bin/ss-server -c /usr/local/bin/config.json -f /tmp/ss-server.pid -u > /dev/null 2>&1 &
完成后可输入ps -ef ,netstat -lnp(需安装 net-tools)查看进程运行情况,是否正确启动并监听相应端口。
注解:
参数 -c 指定配置文件, -f 创建pid文件。
关于pid文件:
(1) pid文件的内容:pid文件为文本文件,内容只有一行, 记录了该进程的ID。 用cat命令可以看到。
(2) pid文件的作用:防止进程启动多个副本。只有获得pid文件(固定路径固定文件名)写入权限(F_WRLCK)的进程才能正常启动并把自身的PID写入该文件中。其它同一个程序的多余进程则自动退出。
6、把 shadowsocks-libev 加入开机启动队列
方式1(适用于 CentOS 7 系统):
新建启动脚本文件
vim /etc/systemd/system/shadowsocks.service
内容如下:
[Unit]
Description=Shadowsocks
After=network-online.target
[Service]
TimeoutStartSec=0
ExecStart= /usr/local/bin/ss-server -c /usr/local/bin/config.json -u /dev/null 2>&1
[Install]
WantedBy=multi-user.target
保存脚本后,前先使用 ps –ef 查看ss是否已运行,然后 kill 掉ss的pid号码关闭ss。
执行以下命令启动 shadowsocks 服务:
systemctl daemon-reload
systemctl enable shadowsocks # 使用enable / disable 选项来控制一个服务是否开机启动
systemctl start shadowsocks
为了检查 shadowsocks 服务是否已成功启动,可以执行以下命令查看服务的状态:
systemctl status shadowsocks -l
查看已启动的服务列表:
systemctl list-unit-files | grep enabled 或者 systemctl list-units –type=service
参考:
《centos7 中 systemd systemctl管理服务的命令 》
《Running Services After the Network is up》
《System Administrator’s Guide:Infrastructure Services》
扩展:
如果遇到故障,可以查看最近一次启动后的系统日志:
journalctl -b
或者指定查看 shadowsocks 最近一次启动后的日志:
journalctl -b -u shadowsocks
或者实施跟踪最近一次启动后的实时日志:
journalctl -f -b -u shadowsocks
运行 systemctl status shadowsocks -l 时如果提示“This system doesn’t provide enough entropy to quickly generate high-quality random numbers. Installing the rng-utils/rng-tools or haveged packages may help.“则有可能是系统熵数太小导致。安装 rng-tools 组件可以解决。
安装熵服务
yum install rng-tools
启动熵服务
systemctl start rngd
如果你的 CPU 不支持 DRNG 特性或者像我一样使用虚拟机,可以使用/dev/unrandom来模拟:
cp /usr/lib/systemd/system/rngd.service /etc/systemd/system
vim /etc/systemd/system/rngd.service
#以下是编辑内容,把ExecStart行修改如下:
ExecStart=/sbin/rngd -f -r /dev/urandom
重新载入服务
systemctl daemon-reload
systemctl restart rngd
扩展参考:
http://web.theurbanpenguin.com/centos-7-rngd-will-not-start/
https://zh.wikipedia.org/wiki//dev/random
《/dev/random和/dev/urandom的一点备忘 》
方式2(适用于 centos6 或 以下系统):
编辑 rc.local,使用以下命令:
vim /etc/rc.d/rc.local
在文件最后新增一行,添加以下内容:
(1)若不使用配置文件config.json,输入
#ss-server
/usr/local/bin/ss-server -s 0.0.0.0 -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退出编辑器。
为 rc.local 增加执行权限:chmod +x /etc/rc.d/rc.local
检查权限: ll /etc/rc.d/rc.local
扩展:
有时我们自己在 /etc/rc.d/rc.local 里面增加的随机器启动的脚本和指令总是不能自动加载和启动,机器启动后手动执行脚本又能成功,经常被搞得晕头转向,解决方式如下,/etc/rc.d/rc.local文件的文件头是 #!/bin/sh (注意:#!/bin/sh 不是注释,#! 是特殊的表示符,后面是解释此脚本的 shell 的路径),系统启动后会把 /etc/rc.d/rc.local 里面的指令或脚本的运行日志写入 /var/log/messages ,我们查看messages文件内容就知道具体的问题出在哪里了。
重启前可以通过 null 重定向清空文件内容:
> /var/log/messages
执行 reboot 后查看messages文件:
cat /var/log/messages
或者
cat /var/log/messages | grep -i ‘ss-server’ # 提取含 ss-server 字符的信息
参考:https://zh.wikipedia.org/wiki/Shebang
7、加入防火墙规则及保存。
CentOS 7 默认的防火墙是FirewallD ,它是 iptables 的前端控制器,用于实现持久的网络流量规则。FirewallD 也是 iptables 的一个封装,可以让你更容易地管理 iptables 规则 ,但它并不是 iptables 的替代品。虽然 iptables 命令仍可用于 FirewallD,但建议使用 FirewallD 时仅使用 FirewallD 命令。
方式一:使用 FirewallD(推荐)
安装 firewalld:
yum install firewalld -y
启动firewalld.servers 服务
systemctl start firewalld
开机启动:
systemctl enable firewalld
查看状态:
systemctl status firewalld
查看防火墙默认区域的规则信息
firewall-cmd –list-all
journalclt -b -u firewalld
永久开启 public zone(firewall 的默认区域为 public zone)的相关端口:
firewall-cmd –permanent –zone=public –add-port=22/tcp
firewall-cmd –permanent –zone=public –add-port=80/tcp
firewall-cmd –permanent –zone=public –add-port=42402/tcp #你的ss端口
firewall-cmd –permanent –zone=public –add-port=42402/udp
firewall-cmd –reload
扩展:
删除一个端口(不指定 zone 时默认为 default zone):
firewall-cmd –remove-port=42402/tcp #立即生效,但重载服务会恢复
firewall-cmd –permanent –remove-port=42402/tcp #非立即生效,但重载服务后永久生效
参考:
How To Set Up a Firewall Using FirewallD on CentOS 7
方式二:使用 iptables(不推荐)
切换到iptables首先应该关掉默认的firewalld,然后安装iptables服务。
#停止firewalld 服务
systemctl stop firewalld
#禁止firewalld 服务
systemctl mask firewalld
#禁止firewalld 开机启动
systemctl disable firewalld.service
#先检查是否安装了iptables,若显示 iptables.service could not be found 则需进行安装。
service iptables status
#安装 iptables-services
yum install iptables iptables-services -y
加入规则:
iptables -I INPUT -p tcp –dport 42402 -j ACCEPT #你的ss服务器端口号
iptables -I INPUT -p udp –dport 42402 -j ACCEPT
保存防火墙规则:
service iptables save
注意:若显示 “The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.”说明 iptables-services 未安装。显示 “iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]”则正常。
#开机启动iptables 服务
systemctl enable iptables.service
#开启服务
systemctl start iptables.service
#查看状态
systemctl status iptables.service
完成后可输入 iptables -L -n 查看规则
8、增加守护进程(只适用于CentOS 6 或以下系统)
如果 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 的服务不在运行就自动启动。
9、为了安全起见,我们使用非root用户运行ss(只适用于CentOS 6 或以下系统)
# 如果没有非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
推荐本站淘宝优惠价购买喜欢的宝贝:
本文链接:https://zblog.hqyman.cn/post/10637.html 非本站原创文章欢迎转载,原创文章需保留本站地址!
休息一下~~