使用archlinux搭建家庭数据服务中心


更新时间:2023年11月21日

平台

树莓派

型号3B,功耗约0.5w,2016年价格219元,型号4B299元。稳定,24小时开机,已经正常运行5年左右。系统archlinuxarm

nuc

型号5CPYH,功耗约8W,速度比树莓派快很多,2016年价格940。系统原版archlinux
本文的内容几乎适用于两种平台的archlinux配置

操作需要的知识

linux命令行基本操作

archlinuxarm在树莓派3B上的安装方法

在这个网页的Installation部分,是通过把tf卡加读卡器插到linux系统的机器上完成的,虚拟机也可以,https://archlinuxarm.org/platforms/armv8/broadcom/raspberry-pi-4,注意+200M那里可以适当大一点。
做好的tf卡插到树莓派上,连接网线,路由器查询ip,可能叫alarm,再通过ssh客户端(MobaXterm)连接到树莓派,账号密码alarm。
输入su切换到root用户,再使用命令wifi-menu连接wifi。

archlinux在nuc上的安装方法

下载iso写到u盘上https://wiki.archlinux.org/title/USB_flash_installation_medium#Using_Rufus
u盘启动后https://wiki.archlinux.org/title/Installation_guide

基本软件

1
pacman -S wget git vim python python-pip unzip

基本设置

时区

编辑/etc/locale.gen,下面两行去掉前面的#

1
2
en_US.UTF-8 UTF-8
zh_CN.UTF-8 UTF-8
1
2
locale-gen
timedatectl set-timezone Asia/Shanghai

修改ls时间显示格式

编辑~/.bashrc添加

1
export TIME_STYLE='+%Y-%m-%d %H:%M:%S'

vim中文乱码和ssh鼠标右键复制

编辑~/.vimrc添加

1
2
set encoding=utf-8
set mouse=r

vim的主题设置vimrc

https://github.com/amix/vimrc

pip下载慢

运行的时候添加参数 -i https://pypi.tuna.tsinghua.edu.cn/simple
永久修改的话,linux下新建或修改~/.pip/pip.conf,添加

1
2
3
4
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

更换默认终端

zsh

1
pacman -S zsh
1
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

安装主题powerlevel10k
git clone –depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
编辑~/.zshrc

1
ZSH_THEME="powerlevel10k/powerlevel10k"
1
source ~/.zshrc

zsh快捷键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CTRL + A: 移动到行(line)的开始位置
CTRL + E: 移动到行的最后面
CTRL + [left arrow]: 向后移动一个单词(one word)
CTRL + [right arrow]: 向前移动一个单词
CTRL + T: 打开新的Tab标签
CTRL + U (BASH): 清除当前行光标位置后面的字符
CTRL + U (zsh): 如果是zsh,则将整行清除
ESC + [backspack]: 删除光标前面的单词
CTRL + W: 同上
ALT + D: 删除光标后面的单词
CTRL + R: 搜索历史
CTRL + G: 退出搜索模式
CTRL + _: 撤销最后一次的改变
CTRL + L: 清空屏幕
CTRL + S: 停止向屏幕继续输出
CTRL + Q: 重新开启屏幕输出
CTRL + C: 终止或者杀死当前前景进程(foreground process)
CTRL + Z: 暂停或者停止当前前景进程
!!: 执行历史记录中的上一个命令
!abc: 执行历史记录中的以 abc开头的命令
!abc:p: 打印历史记录中以abc开头的命令

cron定时运行

1
2
3
pacman -S cronie
systemctl enable cronie
systemctl start cronie

http,php,mysql服务器

1
pacman -S nginx php-fpm mysql

编辑/etc/nginx/nginx.conf,server下面添加

1
root /srv/http;

location /{…}里修改

1
2
3
4
location / {
root /srv/http;
index index.html index.htm index.php;
}

下面添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
location ~ \.php$ {
# 404
try_files $fastcgi_script_name =404;

# default fastcgi_params
include fastcgi_params;

# fastcgi settings
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;

# fastcgi params
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
#fastcgi_param PHP_ADMIN_VALUE "open_basedir=$base/:/usr/lib/php/:/tmp/";
}

nginx中文显示乱码,在server{}里添加

1
2
charset 'utf-8';

1
mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

修改mysql密码mysql -u root -p

1
2
3
set password for root@localhost = password('12332144');
FLUSH PRIVILEGES;
quit;

备份数据库请复制/var/lib/mysql文件夹,粘贴后注意用户组
修改/etc/php/php.ini,下面这行前的分号去掉

1
2
extension=mysqli
extension=pdo_mysql

启动服务

1
2
3
4
5
6
systemctl enable nginx
systemctl enable php-fpm
systemctl enable mariadb
systemctl start nginx
systemctl start php-fpm
systemctl start mariadb

aria下载服务器

linux下

用途:离线下载

1
2
3
4
5
pacman -S aria2
cd
mkdir .aria2
cd .aria2
touch aria2.conf aria2.log aria2.session

编辑aria2.conf,注意修改路径和密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 文件的保存路径(可使用绝对路径或相对路径), 默认: 当前启动位置
dir=/srv/ftp/upload

# 启用磁盘缓存, 0为禁用缓存, 需1.16以上版本, 默认:16M
disk-cache=30M

# 文件预分配方式, 能有效降低磁盘碎片, 默认:prealloc
# 预分配所需时间: none < falloc ? trunc < prealloc
# falloc和trunc则需要文件系统和内核支持, NTFS建议使用falloc, EXT3/4建议trunc
file-allocation=trunc

# 断点续传
continue=true

log=/root/.aria2/aria2.log

## 下载连接相关 ##

# 最大同时下载任务数, 运行时可修改, 默认:5
max-concurrent-downloads=10

# 同一服务器连接数, 添加时可指定, 默认:1
max-connection-per-server=15

# 最小文件分片大小, 添加时可指定, 取值范围1M -1024M, 默认:20M
# 假定size=10M, 文件为20MiB 则使用两个来源下载; 文件为15MiB 则使用一个来源下载
min-split-size=5M

# 单个任务最大线程数, 添加时可指定, 默认:5
split=10

# 整体下载速度限制, 运行时可修改, 默认:0
max-overall-download-limit=0

# 单个任务下载速度限制, 默认:0
max-download-limit=0

# 整体上传速度限制, 运行时可修改, 默认:0
max-overall-upload-limit=20kb

# 单个任务上传速度限制, 默认:0
max-upload-limit=5kb

# 禁用IPv6, 默认:false
disable-ipv6=true

# 禁用https证书检查
check-certificate=false

#运行覆盖已存在文件
allow-overwrite=true

#自动重命名
auto-file-renaming=true

## 进度保存相关 ##

# 从会话文件中读取下载任务
input-file=${HOME}/.aria2/aria2.session

# 在Aria2退出时保存`错误/未完成`的下载任务到会话文件
save-session=${HOME}/.aria2/aria2.session

# 定时保存会话, 0为退出时才保存, 需1.16.1以上版本, 默认:0
save-session-interval=0

## RPC相关设置 ##

# 启用RPC, 默认:false
enable-rpc=true

# 允许所有来源, 默认:false
rpc-allow-origin-all=true

# 允许非外部访问, 默认:false
rpc-listen-all=true

# 事件轮询方式, 取值:[epoll, kqueue, port, poll, select], 不同系统默认值不同
#event-poll=select

# RPC监听端口, 端口被占用时可以修改, 默认:6800
rpc-listen-port=6800

#RPC 密码
#rpc-secret=

# 保存上传的种子文件## BT/PT下载相关 ##

# 当下载的是一个种子(以.torrent结尾)时, 自动开始BT任务, 默认:true
follow-torrent=true

# BT监听端口, 当端口被屏蔽时使用, 默认:6881-6999
listen-port=51413

# 单个种子最大连接数, 默认:55
#bt-max-peers=55

# 打开DHT功能, PT需要禁用, 默认:true
enable-dht=true

# 打开IPv6 DHT功能, PT需要禁用
enable-dht6=false

# DHT网络监听端口, 默认:6881-6999
#dht-listen-port=6881-6999

# 本地节点查找, PT需要禁用, 默认:false
bt-enable-lpd=true

# 种子交换, PT需要禁用, 默认:true
enable-peer-exchange=true

# 每个种子限速, 对少种的PT很有用, 默认:50K
#bt-request-peer-speed-limit=50K

# 客户端伪装, PT需要
peer-id-prefix=-UT341-

# user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763

# 当种子的分享率达到这个数时, 自动停止做种, 0为一直做种, 默认:1.0
seed-ratio=1.0

# 强制保存会话, 话即使任务已经完成, 默认:false
# 较新的版本开启后会在任务完成后依然保留.aria2文件
#force-save=false
# BT校验相关, 默认:true
#bt-hash-check-seed=true
# 继续之前的BT任务时, 无需再次校验, 默认:false
bt-seed-unverified=true
# 保存磁力链接元数据为种子文件(.torrent文件), 默认:falsefollow-torrent=mem

daemon=true

bt-tracker=udp://93.158.213.92:1337/announce,udp://151.80.120.112:2810/announce,udp://208.83.20.20:6969/announce,udp://91.216.110.52:451/announce,udp://89.234.156.205:451/announce,udp://65.108.63.133:80/announce,udp://163.172.170.127:6969/announce,udp://45.14.225.8:1337/announce,udp://51.81.46.170:6969/announce,udp://185.181.60.155:80/announce,udp://148.251.53.72:6969/announce,udp://184.105.151.166:6969/announce,udp://207.241.226.111:6969/announce

bt-tracker在这里获取https://github.com/ngosang/trackerslist
编辑/etc/systemd/system/aria2c.service

1
2
3
4
5
6
7
8
9
[Unit]
Description=Aria2 Daemon

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

[Install]
WantedBy=default.target
1
2
systemctl enable aria2c 
systemctl start aria2c

下载https://github.com/mayswind/AriaNg/releases ,AriaNg-1.2.3-AllInOne.zip,到http目录,新建文件夹aria,复制index.html进去,就可以网页运行管理了

挂载u盘

vim /etc/fstab

1
/dev/sda1               /srv/ftp/udisk  vfat            defaults,noatime        0 0
1
mount -a

windows下

下载https://github.com/mayswind/AriaNg/releases ,AriaNg-1.2.3-AllInOne.zip,到任意目录,打开index.html
或者 https://github.com/mayswind/AriaNg-Native/releases 也可以
后端 https://github.com/aria2/aria2/releases
input-file和save-session的路径需要新建一个文件.txt并改成.session
后端解压后,命令行下运行命令E:\aria2-1.36.0-win-64bit-build1\aria2c.exe –conf-path=E:\aria2\aria2.conf –enable-rpc –rpc-listen-all –rpc-allow-origin-all –file-allocation=falloc –rpc-secret=密码 -c –dir D:\Downloads\aria2 -D,在网页前端里,密码填到ariang设置右侧rpc中,可以把这条命令添加到一个bat文件中一键启动

浏览器下下载百度盘和阿里盘

浏览器安装tampermonkey插件
安装这个脚本https://greasyfork.org/zh-CN/scripts/438022-%E6%B4%BE%E6%B4%BE%E5%8A%A9%E6%89%8B-%E7%99%BE%E5%BA%A6%E7%BD%91%E7%9B%98%E5%B7%A5%E5%85%B7%E7%AE%B1%E7%9B%B4%E9%93%BE%E8%A7%A3%E6%9E%90-%E6%8C%81%E7%BB%AD%E6%9B%B4%E6%96%B0
打开上面的bat文件,网页选取文件,百度盘左上,阿里盘右上直链助手,解析后发到aria或rpc下载,可能需要配置
主机http://localhost 端口6800 路径/jsonrpc 密钥你设置的密码

webdav服务器

用途:静读天下同步阅读进度和标注,或者其他一些小文件
参照搭建webdav服务器实现静读天下同步
目前的使用方式:静读天下专业版同步,joplin同步

ftp服务器

1
pacman -S vsftpd

可以匿名上传文件,进行如下设置:ftp目录权限755,用户组root:ftp,里面新建文件夹upload,权限777,用户组root:root
在/etc/vsftpd.conf里添加

1
2
3
4
5
6
7
8
anonymous_enable=YES
write_enable=YES
anon_mkdir_write_enable=YES
anon_root=/srv/ftp
anon_other_write_enable=YES
anon_umask=022
anon_upload_enable=YES
no_anon_password=YES

ftp目录权限755,用户组root:ftp,里面新建文件夹upload,权限777,用户组root:root
启动服务

1
2
systemctl enable vsftpd
systemctl start vsftpd

签到脚本

通过复制cookie实现每天自动签到,crontab -e里添加以下内容

1
2
3
PYTHONIOENCODING=utf-8
5 7 * * * /usr/bin/python /srv/script/m1.py > /srv/http/index.txt
15 7 * * * /usr/bin/python /srv/script/m2.py >> /srv/http/index.txt

双大于号和后面的内容可以去掉,用于保存每次运行的结果。这个脚本每天0点5分运行,路径注意要填绝对路径。

空气质量检测系统

用途:配合mysql,记录家庭温度等信息
详情https://andi.wang/2017/05/15/%E6%A0%91%E8%8E%93%E6%B4%BE3B%E5%92%8C%E6%94%80%E8%97%A4PMS5003ST/
想在树莓派加添加屏幕查看信息,参照https://andi.wang/2020/03/26/%E6%A0%91%E8%8E%93%E6%B4%BE4B%E5%8A%A0%E5%BE%AE%E9%9B%AA2.13%E5%AF%B8%E5%A2%A8%E6%B0%B4%E5%B1%8F%E7%9B%91%E6%B5%8Bpm2.5%E4%BF%A1%E6%81%AF/

samba服务器

用途:给perfect viewer远程看漫画,和电脑共享方式类似,可以和ftp的upload设置在同一个目录

1
2
pacman -S samba
useradd guest -s /bin/nologin

新建/etc/samba/smb.conf填以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
[global]
security = user
map to guest = bad user
guest account = guest

[pi3]
comment = guest
path = /srv/ftp/
public = yes
only guest = yes
writable = yes
printable = no

启动服务

1
2
3
4
systemctl enable nmb
systemctl enable smb
systemctl start nmb
systemctl start smb

文件管理

filebrowser

用途:文件下载上传管理
https://github.com/filebrowser/filebrowser
树莓派下载linux-armv7-filebrowser.tar.gz
nuc下载linux-amd64-filebrowser.tar.gz

1
2
3
4
5
6
7
8
chmod +x filebrowser
mv filebrowser /usr/bin
cd /etc
filebrowser config init
filebrowser -d /etc/filebrowser.db config set --locale zh-cn
filebrowser -d /etc/filebrowser.db config set --address 0.0.0.0
filebrowser -d /etc/filebrowser.db config set --port 8080
filebrowser -d /etc/filebrowser.db users add username userpassword --perm.admin

新建文件/etc/systemd/system/filebrowser.service并填写,注意修改路径和ip

1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=filebrowser
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/bin/filebrowser -d /etc/filebrowser.db
Restart=on-failure

[Install]
WantedBy=multi-user.target
1
2
systemctl enable filebrowser
systemctl start filebrowser

运行192.168.1.你的ip:8080,账号密码admin
若不行请运行filebrowser users add admin admin

Resilio Sync(BitTorrent Sync)文件同步

用途:手机电脑间文件同步、备份,可以在手机网络与家庭网络中同步。同类软件有syncthing,syncthing更适合个人文件同步,resilio适合与其他人分享。
树莓派下载https://download-cdn.resilio.com/stable/linux-armhf/resilio-sync_armhf.tar.gz
解压tar zxvf resilio-sync_arm.tar.gz
运行命令生成默认配置文件

1
2
chmod +x rslsync
./rslsync --dump-sample-config > /etc/rslsync.conf

文件https://aur.archlinux.org/cgit/aur.git/tree/rslsync.service?h=rslsync,复制里面的内容到/etc/systemd/system/rslsync.service,注意修改路径

1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=Resilio Sync service
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/rslsync --nodaemon --config /etc/rslsync.conf
Restart=on-abort

[Install]
WantedBy=multi-user.target

运行

1
2
systemctl enable rslsync
systemctl start rslsync

如果提示error while loading shared libraries: libcrypt.so.1: cannot open shared object file: No such file or directory,运行

1
pacman -S libxcrypt-compat

树莓派下若无法找到安装包,请手动下载https://archlinuxarm.org/packages/armv7h/libxcrypt-compat后运行

1
pacman -U libxcrypt-compat-4.4.28-2-armv7h.pkg.tar.xz

管理地址:ip:8888

获取路由器ip的方法

新建python文件getmyip.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import requests

url = 'https://ifconfig.me'

response = requests.get(url)
se = requests.Session()
if response.status_code == 200:
newip = response.text

with open('/srv/sync/getmyip/ip.txt', mode='r', encoding = 'UTF-8') as file:
read = file.readlines()
file.close()
oldip = read[0]

if newip != oldip:
newfile = open('/srv/sync/getmyip/ip.txt', mode = 'w', encoding = 'UTF-8')
newfile.writelines(newip)
newfile.close()

可以通过上面的resilio sync同步ip.txt文件

添加这个python到crontab -e里,每天0点,12点运行,注意填写绝对路径

1
0 0,12 * * */usr/bin/python /srv/resiliosync/getmyip/getmyip.py

home assistant智能家居

安装

1
2
pacman -S home-assistant python-wheel python-numpy python-jose python-josepy python-cryptography rust ffmpeg
pip install sqlalchemy PyTurboJPEG python-miio snitun pyrfc3339 requests_toolbelt hass-nabucasa pep517 cryptography pyproject-toml async-upnp-client pynacl pyproject.toml -i https://pypi.tuna.tsinghua.edu.cn/simple
1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=Home Assistant
After=network-online.target

[Service]
Type=simple
User=%i
ExecStart=/usr/bin/hass

[Install]
WantedBy=multi-user.target

第一次启动服务需要很久时间
启动服务

1
2
systemctl enable home-assistant@root.service
systemctl start home-assistant@root.service

关闭服务

1
2
systemctl stop home-assistant@root.service
systemctl disable home-assistant@root.service

查看状态

1
systemctl status home-assistant@root.service

管理界面http://192.168.1.你的ip:8123/
升级home assistant

1
pip3 install --upgrade homeassistant home-assistant-frontend

如果升级失败,出现building wheel

编辑/etc/pip.conf添加

1
2
[global]
extra-index-url=https://www.piwheels.org/simple

或尝试

1
pip3 install --upgrade homeassistant --no-deps

启动时Error

ERROR (MainThread) [homeassistant.setup] Error during setup of component cloud
编辑/usr/lib/python3.10/site-packages/homeassistant/components/cloud/init.py第297行添加#

1
#cloud.register_on_initialized(_on_initialized)

获取小米设备token

1
pip3 install pycryptodome pybase64 requests

下载解压运行,输入账号密码
https://github.com/PiotrMachowski/Xiaomi-cloud-tokens-extractor

1
python3 token_extractor.py

地址192.168.1.你的ip:8123,配置,集成,添加集成,xiaomi miio,手动配置,添加ip和token
root配置文件默认保存在/root/.homeasistant/.storage

添加萤石摄像头

编辑/root/.homeassistant/configuration.yaml
添加

1
2
3
4
 camera:
- platform: ffmpeg
name: 摄像头名
input: -rtsp_transport tcp -i rtsp://admin:六位大写字幕,见摄像头底下贴纸前6位@192.168.1.你的ip:554/h264/ch1/main/av_stream

重启服务

1
systemctl restart home-assistant@root.service

home assistant管理界面,概览,右上角编辑仪表盘,右下添加卡片,图片实体,实体选择添加的摄像头

添加pm2.5信息

pm2.5信息来源

https://andi.wang/2017/05/15/%E6%A0%91%E8%8E%93%E6%B4%BE3B%E5%92%8C%E6%94%80%E8%97%A4PMS5003ST/

编辑/root/.homeassistant/configuration.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
homeassistant:
allowlist_external_dirs:
- "/srv/http/pm25"

sensor:
- platform: file
name: Temperature
file_path: /srv/http/pm25/data.json
unit_of_measurement: "°C"
value_template: '{{ value_json.temperature }}'
- platform: file
name: Humidity
file_path: /srv/http/pm25/data.json
unit_of_measurement: "%"
value_template: '{{ value_json.humidity }}'
- platform: file
name: PM2.5
file_path: /srv/http/pm25/data.json
unit_of_measurement: "μg/m³"
value_template: '{{ value_json.pm25 }}'

重启服务后添加卡片,实体

安装hacs

详细教程https://hacs.xyz/docs/setup/download
下载https://github.com/hacs/get/blob/main/get

1
chmod +x get

59行的地址,地址改成可以下载到的。或者删除命令,直接把文件放到某位置。
树莓派使用root用户

1
wget -O - https://get.hacs.xyz | bash -

重启home assistant,清除浏览器数据后,配置集成里添加hacs,按步骤连接github,连接不稳定可能要重试很多次
成功后左边出现hacs栏,右下角浏览下载自己需要的插件
在configuration.yaml按照对应的github说明添加设备,在重启服务后实体卡片添加就可以用了