树莓派建NAS服务器安装samba minidlna nfs
sean 编辑于2022-08-01 18:44树莓派及Linux
需求:电视盒和手机播放NAS里的电影视频,音乐播放器(用另一个树莓派外接音箱做的本地云端音乐播放器,主要解决手机控制播放省去插拔音频线的麻烦,)播放NAS服务器里的音乐,pc可以存取NAS里的文件。
方案:电视盒和手机端都安装kodi/原xbmc,音乐播放器安装的是moodeaudio(无需视频输出)具体安装参考这篇文章,手机文件备份通过es文件浏览器app基本上能支持的都支持了,NAS服务器主要是samba和minidlna服务。
一、samba安装:
$ sudo apt-get install samba
samba配置修改文件/etc/samba/smb.conf,
默认的[global]如下:
## Browsing/Identification ###
# Change this to the workgroup/NT-domain name your Samba server will part of
workgroup = WORKGROUP
server string = Samba Server
server min protocol = SMB3_00
#### Networking ####
# The specific set of interfaces / networks to bind to
# This can be either the interface name or an IP address/netmask;
# interface names are normally preferred
; interfaces = 127.0.0.0/8 eth0
#interfaces = 192.168.1.0/24 表示只开启ipv4
#interfaces = eth0 表示ipv6,ipv4都开启了
# Only bind to the named interfaces and/or networks; you must use the
# 'interfaces' option above to use this.
# It is recommended that you enable this feature if your Samba machine is
# not protected by a firewall or is a firewall itself. However, this
# option cannot handle dynamic or non-broadcast interfaces correctly.
; bind interfaces only = yes
#### Debugging/Accounting ####
# This tells Samba to use a separate log file for each machine
# that connects
log file = /var/log/samba/log.%m
# Cap the size of the individual log files (in KiB).
max log size = 1000
# We want Samba to only log to /var/log/samba/log.{smbd,nmbd}.
# Append syslog@1 if you want important messages to be sent to syslog too.
logging = file
# Do something sensible when Samba crashes: mail the admin a backtrace
panic action = /usr/share/samba/panic-action %d
####### Authentication #######
# Server role. Defines in which mode Samba will operate. Possible
# values are "standalone server", "member server", "classic primary
# domain controller", "classic backup domain controller", "active
# directory domain controller".
#
# Most people will want "standalone server" or "member server".
# Running as "active directory domain controller" will require first
# running "samba-tool domain provision" to wipe databases and create a
# new domain.
server role = standalone server
obey pam restrictions = yes
# This boolean parameter controls whether Samba attempts to sync the Unix
# password with the SMB password when the encrypted SMB password in the
# passdb is changed.
unix password sync = yes
# For Unix password sync to work on a Debian GNU/Linux system, the following
# parameters must be set (thanks to Ian Kahan <<kahan@informatik.tu-muenchen.de> for
# sending the correct chat script for the passwd program in Debian Sarge).
passwd program = /usr/bin/passwd %u
passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
# This boolean controls whether PAM will be used for password changes
# when requested by an SMB client instead of the program listed in
# 'passwd program'. The default is 'no'.
pam password change = yes
# This option controls how unsuccessful authentication attempts are mapped
# to anonymous connections
map to guest = bad user
在配置文件的“[global]”节的最后,加上下面三条设置解决ln -s命令软连接上去的目录或文件:
follow symlinks = yes wide links = yes unix extensions = no
注释掉容器里的内容用不上
[print$],[printers],
如果想匿名用户只读访问可以在最后增加这个(验证可行):
[pi_share]
comment = pi share
path = /home/pi/share
read only = yes
#create mask = 0644 # 创建文件的权限
directory mask = 0755
guest ok = yes
browseable = yes
增加带认证的写权限的phone目录如下:
[phone]
comment = phone
path = /home/pi/backup/phone
read only = no
writeable = yes
valid users = samba
create mask = 0775
#browseable no 默认打开服务器目录是不显示文件夹的,也就是空的,相对安全些。这里yes方便扫描。
browseable = yes
#guest ok = no
public = yes
directory mask = 0775
这个share和刚才guest都可以访问的目录设置是一样的:
[share]
comment = share
path = /home/pi/share
read only = yes
writeable = no
#create mask = 0700
browseable = yes
#guest ok = yes
public = yes
directory mask = 0700
为了保证目录可以访问给share目录755权限,并且保证share的上级目录也是755否则依然权限不够无法正常访问目录。
如果share在/home/xx目录下,那么xx目录也要有755权限,通常出于安全角度考虑,xx目录是700权限,所以,我建立了一个/home/share目录用来专门分享
共享文件,同时给他chown -R XX:XX /home/share , chmod -R 755 /home/share 这样就不会受/home/XX目录的权限影响了。
重启samba,使配置生效,这里我没有配置额外用户而是简单设置了一个Guest用户,空密码可以登录,但没有给share目录写权限。
而backup/phone目录我给777否则samba是没有写权限的。
创建一个samba用户用来授权登录用于写,因为是samba外部登录我没有加-m ,所以home下没有samba目录
$ sudo useradd samba $ sudo smbpasswd -a samba New SMB password: Retype new SMB password: Added user samba.
如果本机用户想访问samba所有的目录,需要把本机用户加到该组
$ sudo gpasswd -M localuser samba
重启测试
$ sudo /etc/init.d/samba restart
或
$ systemctl restart smbd.service
$ systemctl restart nmbd.service
测试没问题后可以写入开机启动,
sudo systemctl enable smbd.service
sudo systemctl enable nmbd.service
二、安装minidlna:
sudo apt-get install minidlna
修改/etc/minidlna.conf
media_dir=A,/home/pi/share/music media_dir=V,/home/pi/share/movie media_dir=P,/home/pi/share/picture db_dir=/home/pi/share/db log_dir=/home/pi/share/log
然后新建文件夹
$ cd /home/pi/share/ $ mkdir music movie db log $ chmod 755 -R /home/pi/share
然后添些文件进music movie这些目录,并重新索引目录
$ sudo /etc/init.d/minidlna force-reload
没问题后写入重启
sudo systemctl enable minidlna.service
三、安装nfs(默认没有安装,使用samba和minidlna即可,终端支持这两个协议较多)
nfs
网络文件系统,英文Network File System(NFS)。是由SUN公司研制的UNIX表示层协议(pressentation layer protocol),能使使用者访问网络上别处的文件就像在使用自己的计算机一样。
1.在树莓派中安装NFS 服务器:
pi@raspberrypi:~ $ sudo apt-get install nfs-common nfs-kernel-server pi@raspberrypi:~ $ sudo apt-get install portmap //网上有很多人安装,但是无需安装因为新版本已经代替了这个
2.建立要分享及挂载的目录
pi@raspberrypi:~ $ mkdir /home/pi/NFS
3.在系统配置文件中添加需要挂载的目录
pi@raspberrypi:~ $ sudo nano /etc/exports
修改 /etc/exports
在最后添加下面内容
/home/pi/NFS *(rw,sync,no_root_squash,no_subtree_check)
/home/pi/share *(ro,sync,no_subtree_check)
- /home/ning/rootfs :nfs客户端加载目录
- ‘*’ :允许所有的网段访问,也可以使用具体的IP;
- rw:挂接此目录的客户端对该共享目录具有读写权限;
- sync:资料同步写入内存和硬盘;
- no_root_squash:root用户具有对根目录的完全管理访问权限;
- no_subtree_check:不检查父目录的权限。
注意:*和后面的括号不能出现空格。
4. 重启rpcbind 服务
pi@raspberrypi:~ $ sudo /etc/init.d/rpcbind restart [ ok ] Restarting rpcbind (via systemctl): rpcbind.service.
5、重启nfs服务
pi@raspberrypi:~ $ sudo /etc/init.d/nfs-kernel-server restart [ ok ] Restarting nfs-kernel-server (via systemctl): nfs-kernel-server.service.
6.测试nfs服务是否启动成功
pi@raspberrypi:~ $ showmount -e Export list for raspberrypi: /home/pi/NFS *
挂载NFS
在 Windows 上挂载 NFS
1、首先开启Windows的NFS客户端服务,按照百度的教程即可打开。
2、在CMD中输入:
mount \\树莓派的IP地址\mnt\nfs z:
PS:上面的z:是将nfs云存储挂载到了Z这个盘符上(不要和现有的盘符冲突就行,比如说你要挂载到C:上肯定就是不行的)。
如果看到下面的提示,那么就证明挂载成功了。
z: is now successfully connected to \\xxx.xxx.xxx.xxx\mnt\nfs The command completed successfully.
之后打开计算机,就能看到你刚才挂载的nfs了。
如果遇到下面的报错,那就是你在Windows中挂载的有问题。
Network Error - 53 Type 'NET HELPMSG 53' for more information.
一般这种都是在挂载时IP地址没有填写正确,或者挂载的目录不正确,请反复检查来排查错误。
在 Linux 中挂载 NFS
1、安装nfs-utils和portma包
sudo apt-get install nfs-common portmap
2、创建一个提供挂载的目录
sudo mkdir /mnt/mount_nfs
3、挂载
sudo mount -t nfs 192.168.xxx.xxx:/mnt/nfs /mnt/mount_nfs
请将xxx这部分替换为树莓派的IP地址
肥龙软件分享的软件是本站作者开发的免费,无广告,安全可靠,绝不附带任何无关软件,绝不困绑任何插件的实用软件。如果您感觉好用,可以捐赠我们,这样我们可以有更积极的动力去改进升级软件,维持服务器运转,感谢您的捐助,谢谢!
更新时间:2024.8.31