Centos 安装配置 ShadowSocks

ByWhat'sUs

Centos 安装配置 ShadowSocks

Centos7开始,默认使用Systemd作为开启脚本管理工具,Shadowsocks是比较流行的上网工具,下面内容介绍了如何在Centos下安装和配置Shadowsocks服务。

安装pip

pip是 python 的包管理工具。在本文中将使用 python 版本的 shadowsocks,此版本的 shadowsocks 已发布到 pip 上,因此我们需要通过 pip 命令来安装。

在控制台执行以下命令安装 pip:

# curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
# python get-pip.py

安装配置Shadowsocks
在终端执行以下命令安装 shadowsocks:

# pip install --upgrade pip
# pip install shadowsocks

或者

#安装 python setup tools
yum install python-setuptools
#安装pip
easy_install pip
#升级 pip
pip install --upgrade pip
#安装 shadowsocks
pip install shadowsocks

安装完成后,创建配置文件: /etc/shadowsocks.json,内容如下:

{
  "server": "10.0.0.15",
  "server_port": 8088,
  "password": "9mhct2zr2caqd6",
  "method": "aes-256-cfb"
}

说明:
method为加密方法,可选aes-128-cfb, aes-192-cfb, aes-256-cfb, bf-cfb, cast5-cfb, des-cfb, rc4-md5, chacha20, salsa20, rc4, table
server_port为服务监听端口
password为密码,可使用密码生成工具生成一个随机密码
以上说明部分在客户端设置时需相同。

完整配置内容:

{
    "server": "10.0.0.15",
    "server_port": 8088,
    "local_port": 1080,
    "password": "9mhct2zr2caqd6",
    "timeout": 300,
    "method": "aes-256-cfb",
    "local_address": "0.0.0.0",
    "fast_open": false
}

详细说明可查看shadowsocks帮助文档

自启动脚本
新建启动脚本文件/etc/systemd/system/shadowsocks.service,内容如下:

[Unit]
Description=Shadowsocks
[Service]
TimeoutStartSec=0
ExecStart=/usr/bin/ssserver -c /etc/shadowsocks.json
[Install]
WantedBy=multi-user.target

启动 shadowsocks 服务:

# systemctl enable shadowsocks
# systemctl start shadowsocks

或者

# /usr/bin/ssserver -c /etc/shadowsocks.json -d start

查看服务的状态:

# systemctl status shadowsocks -l

一键脚本
新建文件install-shadowsocks.sh

#!/bin/bash
# Install Shadowsocks on CentOS 7
echo "Installing Shadowsocks..."
random-string()
{
    cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1
}
CONFIG_FILE=/etc/shadowsocks.json
SERVICE_FILE=/etc/systemd/system/shadowsocks.service
SS_PASSWORD=$(random-string 32)
SS_PORT=8388
SS_METHOD=aes-256-cfb
SS_IP=`ip route get 1 | awk '{print $NF;exit}'`
GET_PIP_FILE=/tmp/get-pip.py
# install pip
curl "https://bootstrap.pypa.io/get-pip.py" -o "${GET_PIP_FILE}"
python ${GET_PIP_FILE}
# install shadowsocks
pip install --upgrade pip
pip install shadowsocks
# create shadowsocls config
cat <<EOF | sudo tee ${CONFIG_FILE}
{
  "server": "0.0.0.0",
  "server_port": ${SS_PORT},
  "password": "${SS_PASSWORD}",
  "method": "${SS_METHOD}"
}
EOF
# create service
cat <<EOF | sudo tee ${SERVICE_FILE}
[Unit]
Description=Shadowsocks
[Service]
TimeoutStartSec=0
ExecStart=/usr/bin/ssserver -c ${CONFIG_FILE}
[Install]
WantedBy=multi-user.target
EOF
# start service
systemctl enable shadowsocks
systemctl start shadowsocks
# view service status
sleep 5
systemctl status shadowsocks -l
echo "================================"
echo ""
echo "Congratulations! Shadowsocks has been installed on your system."
echo "You shadowsocks connection info:"
echo "--------------------------------"
echo "server:      ${SS_IP}"
echo "server_port: ${SS_PORT}"
echo "password:    ${SS_PASSWORD}"
echo "method:      ${SS_METHOD}"
echo "--------------------------------"

执行安装:

# chmod +x install-shadowsocks.sh
# ./install-shadowsocks.sh

也可以直接执行以下命令从 GitHub 下载安装脚本并执行:

# bash <(curl -s http://www.poloo.org/uploads/install-shadowsocks.sh)

安装完成后会自动打印出 Shadowsocks 的连接配置信息。例如:

Congratulations! Shadowsocks has been installed on your system.
You shadowsocks connection info:
--------------------------------
server:      10.0.0.15
server_port: 8088
password:    9mhct2zr2caqd6
method: aes-256-cfb 


About the author

What'sUs administrator

Leave a Reply

PHP Code Snippets Powered By : XYZScripts.com