Raspberry Pi从零开始搭建低成本NAS(6)-文件共享

为了方便访问树莓派的存储设备,有必要为其添加文件共享,下面为给出三种文件访问方法:samba, ftp, web file server

Samba

安装samba所需软件
sudo apt-get install samba samba-common-bin –y

先备份,然后编辑/etc/samba/smb.conf文件

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.orig
sudo vim /etc/samba/smb.conf

创建shares文件夹

mkdir /home/pi/nas-data/DLNA/shares

需要修改添加的内容如下,

[global]
security = user
encrypt passwords = true
guest account = nobody
map to guest = bad user

#======================= Share Definitions =======================
[share]
comment = Guest access shares
path = /home/pi/nas-data/shares
browseable = yes
writable = yes
#read only = yes
guest ok = yes
public = yes

[NAS-Data]
comment = Nas data folder
path = /home/pi/nas-data/
browseable = yes
writable = yes
valid users = root pi

[USBHDD]
comment = Media folder
path = /home/pi/usbhdd
browseable = yes
writable = yes
valid users = root pi

share共享目录访问不需要密码,而NAS-Data, USBHDD访问需要验证,用户为root和pi。

最后重启samba服务。然后同一局域网的其他设备就可以访问RPi的共享目录

sudo service samba restart

Android可以使用ES File Explorer,IOS可以使用FileExplorer, FileBrowser, Documents和Remote File Free,或其他支持访问共享的文件管理器。

FTP

安装vsftp,开源的轻量级的常用ftp服务器

sudo apt-get install vsftpd

先备份,然后编辑配置文件,

sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.orig
sudo vim /etc/vsftpd.conf

vsftp的配置文件,它允许你设置所有类型的限制和策略,目前没有深入研究,修改如下

# 不允许匿名访问
anonymous_enable=NO
# 设定可以进行写操作
write_enable=YES
# 设定本地用户可以访问
local_enable=YES

重启vsftpd服务

sudo service vsftpd restart

通过ftp连接树莓派系统,以用户名pi登录,密码是pi用户的密码。ftp的根目录是/home/pi,即pi用户的HOME目录,可上传或下载文件了。

Web File Server

通过http访问文件,采用python SimpleHTTPServer实现,性能上肯定比不上专业的web服务器软件,但是简单够用。

首先创建脚本httpFileServer.py,

sudo vim /home/pi/httpFileServer.py
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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os
import posixpath
import urllib
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
# modify this to add additional routes
ROUTES = (
# [url_prefix , directory_path]
['/dlna', '/home/pi/nas-data/DLNA'],
['', '/home/pi/usbhdd] # empty string for the 'default' match
)
class RequestHandler(SimpleHTTPRequestHandler):
def translate_path(self, path):
"""translate path given routes"""
# set default root to cwd
root = os.getcwd()
# look up routes and set root directory accordingly
for pattern, rootdir in ROUTES:
if path.startswith(pattern):
# found match!
path = path[len(pattern):] # consume path up to pattern len
root = rootdir
break
# normalize path and prepend root directory
path = path.split('?',1)[0]
path = path.split('#',1)[0]
path = posixpath.normpath(urllib.unquote(path))
words = path.split('/')
words = filter(None, words)
path = root
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir):
continue
path = os.path.join(path, word)
return path
if __name__ == '__main__':
BaseHTTPServer.test(RequestHandler, BaseHTTPServer.HTTPServer)

创建服务,

sudo vim /etc/init.d/hfs
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
#!/bin/sh
# /etc/init.d/hfs
### BEGIN INIT INFO
# Provides: hfs
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Http file server init script.
# Description: Starts and stops http file services.
### END INIT INFO
WWW_PORT='80'
case $1 in
start)
nohup python /home/pi/httpFileServer.py $WWW_PORT >> /tmp/nohup.log 2>&1 &
sleep 2
stat=`netstat -tlpn | grep $WWW_PORT | grep "python" | cut -d":" -f2 | cut -d" " -f1`
if [ $WWW_PORT -eq $stat ]; then
sock=`netstat -tlpn | grep $WWW_PORT | grep "python"`
echo "Server is running:\n$sock"
else
echo "Server is stopped"
fi
;;
stop)
pid=`ps ax | grep "/home/pi/httpFileServer.py $WWW_PORT"| awk '{print $1}'`
kill -9 $pid 2>/dev/null
rm -f /tmp/nohup.log
stat=`netstat -tlpn | grep $WWW_PORT | grep "python"| cut -d":" -f2 | cut -d" " -f1`
if [ $WWW_PORT -eq $stat ]; then
sock=`netstat -tlpn | grep $WWW_PORT | grep "python"`
echo "Server is still running:\n$sock"
else
echo "Server has stopped"
fi
;;
status)
stat=`netstat -tlpn |grep $WWW_PORT| grep "python" | cut -d":" -f2 | cut -d" " -f1`
if [ $WWW_PORT -eq $stat ]; then
sock=`netstat -tlpn | grep $WWW_PORT | grep "python"`
echo "Server is running:\n$sock"
else
echo "Server is stopped"
fi
;;
*)
echo "Use $0 start|stop|status"
exit 1
;;
esac
exit 0

添加可执行权限

sudo chmod +x /etc/init.d/hfs

然后更新rc,添加自启动服务

sudo update-rc.d hfs defaults

启动服务,

sudo service hfs start

假设树莓派ip地址为192.168.1.118,访问就可以访问/home/pi/usbhdd,访问就可以访问/home/pi/nas-data/DLNA