重新捡起你那吃灰的树莓派

栏目: 软件资讯 · 发布时间: 5年前

内容简介:最近在考驾照,没大块时间看书了,就拿起我那吃灰了1年的树莓派出来耍耍。我安装的是官方的raspberry系统,基于debian。烧录到micro sd卡,启动系统以后:.. code:: bash

最近在考驾照,没大块时间看书了,就拿起我那吃灰了1年的树莓派出来耍耍。

操作系统

我安装的是官方的raspberry系统,基于debian。烧录到micro sd卡,启动系统以后:

  • 第一件要做的事情就是执行 raspi-config 选择第一项,把剩余的sd卡空间用起来。

  • 第二件要做的事情就是把软件源换成国内的:

.. code:: bash

# cat /etc/apt/sources.list
deb http://mirrors.aliyun.com/raspbian/raspbian/ jessie main non-free contrib rpi

虽然我买的是 class 10 的 micro sd 卡,但是 sudo apt-get update 以后还是会有点卡。

  • 第三件要做的事情就是把用户切换成root,虽然在生产环境这么做不太好,但是自己的 折腾机器,用root比较方便,例如少打sudo(虽然你也可以在 /etc/sudoers 里设置 不需要密码,但那还不如直接用root呢)。

.. code:: bash

# 首先,切换到root
$ sudo su
# 添加公钥到 ``/root/.ssh/authorized_keys``
cat >> /root/.ssh/authorized_keys
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# 修改sshd_config中的 ``PermitRootLogin`` 选项成 ``without-password``
# cat /etc/ssh/sshd_config  | grep PermitRootLogin
PermitRootLogin without-password

安装aria2

aria2 is a lightweight multi-protocol & multi-source command-line download utility. It supports HTTP/HTTPS, FTP, BitTorrent and Metalink. aria2 can be manipulated via built-in JSON-RPC and XML-RPC interfaces.

.. code:: bash

apt-get install aria2

但实际执行aria2的时候却是用aria2c来执行的。所以之后我们统一所有配置文件等,名字 全用aria2c来命名。aria2c很多可配置选项,学习它的最好方式就是看man。

接下来把下面的配置文件输入到 /etc/aria2c.conf 去::

enable-rpc=true
rpc-allow-origin-all=true
rpc-listen-all=true
rpc-listen-port=6800
max-concurrent-downloads=5
continue=true
max-connection-per-server=5
min-split-size=10M
split=10
max-overall-download-limit=0
max-download-limit=0
max-overall-upload-limit=100
max-upload-limit=100
dir=/data/aria2
bt-enable-lpd=true
bt-require-crypto=true
bt-max-peers=20
disable-ipv6=true
save-session-interval=60
daemon=true

注意其中dir选项是保存下载文件路径。我的树莓派上用了一个有源USB挂了一个1T的硬盘。 下面是我的fstab::

# cat /etc/fstab
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
/dev/disk/by-uuid/56cf232e-f4d9-4b20-a2e4-xxxx /data  ext4  defaults,nofail 0 0

当然,最后一行,仔细看一下,有个nofail选项,man是这么说的::

nofail Do not report errors for this device if it does not exist.

就是说uuid为这个的硬盘,如果开机的时候没有插入,系统也不会等它。而且我实测了一下, 就算是系统已经开启,我再把硬盘插入,系统还是会自动把硬盘挂到 /data 上。

下一步就是使用systemd来让aria2c开机启动了:

.. code:: bash

# cat /etc/systemd/system/aria2c.service
[Unit]
Description=Aria2c download manager
After=network.target

[Service]
User=root
Type=forking
ExecStart=/usr/bin/aria2c --conf-path=/etc/aria2c.conf

[Install]
WantedBy=multi-user.target

接下来::

# systemctl enable aria2c

大功告成~

Web UI

可以用aria2c来下载了,那怎么看下载进度或者其他呢?有人在Github开源了两个web 界面,一个是 yaaw webui-aria2 。还有人把 yaaw 打包成了chrome插件。 接下来我讲一下怎么把 yaaw 部署在树莓派上。

.. yaaw : https://github.com/binux/yaaw .. webui-aria2 : https://github.com/ziahamza/webui-aria2

  • 首先安装nginx和git::

    apt-get install nginx git

  • yaaw _ 拷贝下来,因为我不想让太多数据写入到sd卡上,所以我把web资源都放 /data 上,前提是/data挂载好了,要不然会拿到404哦。

  • 首先拷贝 yaaw _ ::

    # git clone https://github.com/binux/yaaw /data/www/yaaw
  • 然后配置 /etc/nginx/sites-enabled/default ::

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;
        server_name _;
        location / {
            root /data/www/yaaw;
            index index.html index.htm;
        }
    }
  • 重启nginx::

    # systemctl restart nginx # 或者 nginx -s reload
  • 测试访问80端口::

    # curl localhost
  • 接下来再安装一个chrome插件叫 aria2c-integration _ ,把平时的下载拦截然 后导入到aria2c去,安装完成以后记得配置一下 json-rpchttp://{树莓派ip地址}:6800/jsonrpc

.. _ aria2c-integration : https://chrome.google.com/webstore/detail/aria2c-integration/edcakfpjaobkpdfpicldlccdffkhpbfk

配置ftp或Samba

可以下东西,也可以看到下载进度,那怎么用下载好的东西呢?本来是想配置samba的, 但是一看安装那么多依赖,还是改成ftp吧:

  • 首先安装vsftp::

    # apt-get install vsftp
  • 然后配置vsftp::

    http://www.g-loaded.eu/2008/12/02/set-up-an-anonymous-ftp-server-with-vsftpd-in-less-than-a-minute/

定时报告公网ip

试了一下,我家里的ip地址是分配的公网ip,但是是会变的,比如重启路由器之类的。 如果你们家的ip不是公网ip那还是跳过这一段吧,哈哈哈。 写了一个程序让树莓派邮件报告当前公网ip:

.. code:: python

# coding=utf-8

import requests
import smtplib
from email.mime.text import MIMEText

USER = "[email protected]"
PASSWD = "xxx"
TO = "[email protected]"
TITLE = "raspberry_pi_ip_report"

# get ip

API = "http://www.hahayangqi.cn/a/ip_api.php"
r = requests.get(API)

msg = MIMEText(r.text, "plain", "utf-8")
msg["Subject"] = TITLE
msg["From"] = USER
msg["To"] = TO

s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.set_debuglevel(1)
s.login(USER, PASSWD)
s.sendmail(USER, TO, msg.as_string())
s.quit()

其中用来查询公网ip的API自己也可以做,比如tornado直接打印 self.request.remote_ip 就得了。

接下来要做的事情就是在路由器里设置端口映射,把路由器的某个端口转发到树莓派上。

设置完之后就可以ssh登陆上去啦!

当然,更简单的一种方式是,检测到ip有变,就去dnspod更新对应的域名绑定,也可以检测到 ip有变才发送邮件,否则不发送邮件。不过考虑到树莓派的sd卡是脆弱的,还是采用最简单粗暴 的方式吧。

最后,别忘了设置crontab定时执行任务。还有别忘了在gmail里加入过滤规则,要不然会被 邮件淹没的。。。

总结

折腾下来,树莓派常用的软件都有,CPU也还过得去,性能瓶颈在于IO。我用的 class 10 的 sd卡,还是会感觉到卡顿。另外CPU也不是特别够用,比如vim装了很多插件以后就特别特别卡, 一方面是IO,一方面是CPU。所以树莓派上的vim用的都是我特别精简版的vimrc。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Building Social Web Applications

Building Social Web Applications

Gavin Bell / O'Reilly Media / 2009-10-1 / USD 34.99

Building a social web application that attracts and retains regular visitors, and gets them to interact, isn't easy to do. This book walks you through the tough questions you'll face if you're to crea......一起来看看 《Building Social Web Applications》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码