树莓派上不论是raspbian的安装/更新,还是conda/pip的安装/更新,都因为科学原因不太稳定,因此可以先准备科学安装/更新环境。
1 系统安装及配置
1.1 Headless下的WiFi及ssh配置
因为要将树莓派当做网关用,所以用命令行就可以了,下载lite镜像。 按树莓派官方说明在SD卡上写入镜像,然后进行Headless配置:
配置WIFI,需要在SD卡根目录下新建一个名为wpa_supplicant.conf的文件:
touch /Volumes/boot/wpa_supplicant.conf写入WIFI配置:
country=USctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdevupdate_config=1
network={ ssid="<YOUR-NETWORK-NAME>" psk="<YOUR-NETWORK-PASSWORD>"}开启SSH,只需在SD卡根目录下新建一个名为ssh的空文件即可:
touch /Volumes/boot/ssh配置完成后正常启动树莓派即可自动连接WIFI,SSH可以正常登录。
注:如果不知道设备IP,可以使用ping raspberry.local查找。
如果这个IP上曾经还有过其他树莓派前辈,那么要先删除本机上的ssh信任主机,比如我的树莓派一直用固定IP配在108上:
ssh-keygen -R 192.168.1.108第一次用pi登录后默认密码为raspberry,应使用sudo raspi-config通过树莓派管理工具修改密码、主机名等信息,或是直接使用passwd修改密码。
1.2 换国内镜像源
解决升级慢或Cannot initiate the connection to mirrors.opencas.cn问题:
sudo vi /etc/apt/sources.list修改sources.list,注释第一行,在最后添加国内镜像站:
deb http://mirrors.aliyun.com/raspbian/raspbian/ stretch main non-free contribdeb-src http://mirrors.aliyun.com/raspbian/raspbian/ stretch main non-free contrib同样的:
sudo vi /etc/apt/sources.list.d/raspi.list修改raspi.list,注释第一行,在最后添加:
deb http://mirrors.aliyun.com/raspbian/raspbian/ stretch main ui也可以使用其他镜像站,比如:
# 中国科学技术大学Raspbian http://mirrors.ustc.edu.cn/raspbian/raspbian/# 阿里云Raspbian http://mirrors.aliyun.com/raspbian/raspbian/# 清华大学Raspbian http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/# 华中科技大学Raspbian http://mirrors.hustunique.com/raspbian/raspbian/Arch Linux ARM http://mirrors.hustunique.com/archlinuxarm/# 华南农业大学(华南用户)Raspbian http://mirrors.scau.edu.cn/raspbian/# 大连东软信息学院源(北方用户)Raspbian http://mirrors.neusoft.edu.cn/raspbian/raspbian/# 重庆大学源(中西部用户)Raspbian http://mirrors.cqu.edu.cn/Raspbian/raspbian/# 中山大学 已跳转至中国科学技术大学源Raspbian http://mirror.sysu.edu.cn/raspbian/raspbian/# 新加坡国立大学Raspbian http://mirror.nus.edu.sg/raspbian/raspbian固件升级,谨慎操作,可能会踩到奇怪的编译坑:
sudo rpi-update && sudo reboot -h now软件包升级:
sudo apt-get update && time sudo apt-get upgrade && time sudo apt-get dist-upgrade软件包清理,顺便装个vim:
sudo apt-get cleansudo apt-get install -y vim解决树莓派的locale警告:
sudo dpkg-reconfigure locales选择生成en_GB.UTF-8、en_US.UTF-8、zh_CN.UTF-8。
为空项设置值:
sudo update-locale LANGUAGE="en_GB.UTF-8"sudo update-locale LC_ALL="en_GB.UTF-8"2 搭建科学工具
2.1 安装SS
安装shadowsocks命令行客户端:
pip install shadowsockssudo apt-get install -y shadowsocks-libev# 更新CPANsudo cpan install CPAN# 重载CPANsudo cpan reload CPANsudo cpan Net::Shadowsocks2.2 配置防火墙
安装UFW以方便调整防火墙策略,其实是我不会用iptables:
sudo apt-get install ufw更新防火墙策略:
sudo ufw allow proto tcp to 0.0.0.0/0 port 22 comment "sshd listen port"sudo ufw allow proto tcp to 0.0.0.0/0 port 5900 comment "vnc server listen port"sudo ufw allow proto tcp to 0.0.0.0/0 port 1080 comment "Shadowsocks server listen port"sudo ufw default denysudo ufw enable2.3 socks5代理
在/etc/shadowsocks-libev/xxx.json下配置科学上网服务器:
{ "server": "<your-remote-server>", "server_port": <your-remote-server-port>, "timeout": 60, "password": "<your-password>", "method": "aes-256-cfb", "local_address": "0.0.0.0", "local_port": 1080}检查默认端口占用,通常SS安装完成后会自动使用默认配置(/etc/shadowsocks-libev/config.json)启动ss-server进程,因此8388端口通常会被占用。
而我们在树莓派上使用SS仅仅是为了加速,因此不需要启动ss-server:
sudo netstat -nap | grep 8388停止默认ss服务端:
# 检查ss默认系统服务systemctl list-unit-files # 或 systemctl -asystemctl status shadowsocks-libev.servicesudo systemctl stop shadowsocks-libev.servicesudo systemctl disable shadowsocks-libev.servicesystemctl status shadowsocks-libev.service启动ss客户端:
# 后台nohup ss-local -v -c /etc/shadowsocks-libev/xxx.json &# 或前台ss-local -v -s <your-remote-server> -p <your-remote-server-port> -l 1080 -k <your-password> -m aes-256-cfb测试ss客户端的socks5是否正常:
curl -I --socks5 localhost:1080 google.com停止ss客户端:
sudo kill -TERM 5914 && rm nohup.out编写快捷脚本:
启动脚本ss-s:
sudo touch /usr/local/bin/ss-ssudo chmod +x /usr/local/bin/ss-s#!/bin/bash
nohup ss-local -v -c /etc/shadowsocks-libev/xxx.json > ~/nohup.log 2>&1 &echo $! > ~/ss-pid.txt停止ss-t:
sudo touch /usr/local/bin/ss-tsudo chmod +x /usr/local/bin/ss-t#!/bin/bash
kill -s TERM `cat ~/ss-pid.txt`rm ~/ss-pid.txt2.4 proxychains-ng
ProxyChains是一个使用方便的代理工具,它只会代理指定的程序,下载:
git clone https://github.com/rofl0r/proxychains-ng安装:
cd proxychains-ng/./configure --prefix=/usr --sysconfdir=/etcmakesudo make installsudo make install-config配置/etc/proxychains.conf,将最后一行修改为shadowsocks的端口:
socks5 127.0.0.1 1080测试wget,返回网页源码:
proxychains4 wget -qO- https://www.google.com2.5 http代理
有些程序并不像curl那样能够直接支持socks5代理,有时,按照情况需要配置http代理。
安装privoxy,开启全局http代理,其默认代理地址为http://127.0.0.1:8118:
sudo apt-get install privoxysudo -secho 'forward-socks5 / 127.0.0.1:1080 .' >>/etc/privoxy/config^Dsystemctl status privoxy.service#systemctl enable privoxy.service#systemctl start privoxy.service#systemctl restart privoxy.servicesystemctl status privoxy.service测试http代理:
wget -qO- -e use_proxy=yes -e http_proxy=127.0.0.1:8118 http://google.comconda可能存在科学安装问题,因此可以临时设置代理环境变量:
export HTTP_PROXY="http://127.0.0.1:8118" && conda install -y scikit-imageunset HTTP_PROXY或是配置.condarc的代理:
proxy_servers: http: http://127.0.0.1:81183 云科学(要钱)
3.1 在AWS上部署Streisand
在aws中新建用户administrator,放入新建组Administrators,附加超级管理员权限AdministratorAccess。
新建访问秘钥并下载,记住key id与key后删除秘钥。
aws启动Ubuntu 16.04实例,在安装pip时出现E: Unable to locate package python-pip:
sudo apt-get install software-properties-commonsudo apt-add-repository universesudo apt-get updatesudo apt-get install python-pippip install --upgrade pip软件包升级:sudo apt-get update && time sudo apt-get upgrade && time sudo apt-get dist-upgrade
查看系统地区设置locale,添加中文支持sudo locale-gen zh_CN.UTF-8
设置空项的值:
sudo update-locale LANGUAGE="en_US.UTF-8"sudo update-locale LC_ALL="en_US.UTF-8"在树莓派上安装Streisand的准备工作: 生成秘钥对:
ssh-keygen安装git和vim:
sudo apt-get install gitsudo apt-get install vim安装python编译依赖:
sudo apt install python-paramiko python-pip python-pycurl python-dev build-essential安装ansible相关依赖:
sudo pip install ansible markupsafe安装aws相关依赖:
sudo pip install boto boto3下载Streisand源码并安装:
git clone https://github.com/StreisandEffect/streisand.git && cd streisand./streisand按要求填写aws的相关信息,等很久…部署完成…
拷贝generated-docs到一个能看HTML网页的机器上(我的树莓派是raspbian lite版本):
scp -r pi@<raspberrypi-ip>:streisand ~/Downloads按照网页提示、按需求(iOS、Linux、Android等)一步一步操作。
在树莓派上,按streisand生成的generated-docs中的提示信息,填写shadowsocks的配置文件/etc/shadowsocks-libev/aws.json:
{ "server":"<your-server-ip>", "server_port":<your-server-port>, "timeout":60, "password":"<your-password>", "method":"chacha20-ietf-poly1305", "local_address":"0.0.0.0", "local_port":1080}3.2 本机编译科学工具
由于树莓派上直接安装的shadowsocks不支持chacha20-ietf-poly1305加密算法,因此,需要从源码编译shadowsocks:
# Installation of basic build dependenciessudo apt-get install --no-install-recommends gettext build-essential autoconf libtool libpcre3-dev asciidoc xmlto libev-dev libc-ares-dev automake libmbedtls-dev libsodium-dev
# Installation of Libsodiumexport LIBSODIUM_VER=1.0.16wget https://download.libsodium.org/libsodium/releases/libsodium-$LIBSODIUM_VER.tar.gztar xvf libsodium-$LIBSODIUM_VER.tar.gzpushd libsodium-$LIBSODIUM_VER./configure --prefix=/usr && makesudo make installpopdsudo ldconfig
# Installation of MbedTLSexport MBEDTLS_VER=2.12.0wget https://tls.mbed.org/download/mbedtls-$MBEDTLS_VER-gpl.tgztar xvf mbedtls-$MBEDTLS_VER-gpl.tgzpushd mbedtls-$MBEDTLS_VERmake SHARED=1 CFLAGS=-fPICsudo make DESTDIR=/usr installpopdsudo ldconfig
# Start buildinggit submodule update --init --recursive# ref: https://github.com/shadowsocks/shadowsocks-libev/issues/1177#issuecomment-319032195./autogen.sh && ./configure --with-sodium-include=/usr/include --with-sodium-lib=/usr/local/lib --with-mbedtls-include=/usr/include --with-mbedtls-lib=/usr/lib && makesudo make install现在使用ss-local --help会发现加密算法支持chacha20-ietf-poly1305。
使用
# 启动awsnohup ss-local -v -c /etc/shadowsocks-libev/aws.json&# 测试连接curl -I --socks5 localhost:1080 google.com# 返回301,成功关于要钱:最便宜的EC2即可,一个月不到8刀吧……