很久以前树莓派还在3代的时候尝试过用它来做局域网NAS,因为是百兆网卡速度简直是惨不忍睹。如今更新了4代,入了个8G内存的版本,听说是千兆网卡,想着这回拿来做NAS应该不错,试了一直速度确实可以了。记录一下整个搭建过程。

其实NAS有很多种方案,有像开源的nextCloud、seafile,不过都是基于WEB界面管理,本人不是太喜欢。我还是喜欢那种像一般的文件系统一样可以复制粘贴的。正好samba可以满足我的需求。

挂载硬盘

挂载就不用多说了吧。编辑 /etc/fstab 文件,加入自己要挂载的硬盘信息。比如:

UUID=1234-5678 /home/nas xfs noatime 0 1

硬盘的UUID可以用blkid 命令查看。

编辑好了之后使用mount -a挂载。

安装Samba

我安装的是64版的ubuntu,APT包管理就有现成的。

apt install samba

配置

先建立一个专门连接samba服务的用户

useradd -M -s /usr/sbin/nologin nas-user

samba的配置文件默认在/etc/samba/smb.conf,用自己熟悉的文本编辑器打开它。在它后面加上这段:

[nas]
   comment = Family files share
   path = /home/nas
   browseable = yes
   guest ok = no
   read only = no
   create mask = 0777
   directory mask = 0777

保存退出后用这段命令建立一个samba用户,它的名字必须和前一个步骤建立的linux用户一致。

smbpasswd -a nas-user

顺便贴一段man smbpasswd-a选项的说明:

 -a
  This option specifies that the username following should be added to the local smbpasswd file, with the new password typed (type <Enter> for the old password). This option is ignored if the username following already exists in the smbpasswd file and it is treated like a regular change password command. Note that the default passdb backends require the user to already exist in the system password file (usually /etc/passwd), else the request to add the user will fail.
  This option is only available when running smbpasswd as root.

完成之后重启一下samba服务:

systemctl restart smbd

从Mac连接到服务器

至此Samba服务就搭建完成了,要从Mac上访问到它,可以从访达的菜单上点击【前往】→【连接服务器】,输入smb://<ip>/<profile>。这里的<ip>就填写你的服务器(树莓派)的内网地址,<profile>就写在上面的步骤里在smb.conf下面建立的档案名(中括号里面的那个)。

例如你的服务器为192.168.1.10,档案名是nas,那连接的地址就是

smb://192.168.1.10/nas

点击连接之后会提示你输入用户名和密码,用户名就填上面建立的nas-user,密码就填通过smbpasswd命令建立的那个。

速度测试

我使用的是2.5寸的机械硬盘。

先转到挂载的目录下:

cd /Volumes/nas

写入测试

传输大小为1G,用时大约2分25秒,7.05M/s

dd if=/dev/zero of=test bs=64k count=16k
16384+0 records in
16384+0 records out
1073741824 bytes transferred in 145.216661 secs (7394068 bytes/sec)

取读测试

1G大小,花费2分零4秒,8.2M/s

dd if=test of=/dev/null
2097152+0 records in
2097152+0 records out
1073741824 bytes transferred in 124.139418 secs (8649483 bytes/sec)

嗯…对于4K家庭影院速度还是差太多,不过对于保存学♂习资料已经足够了吧。

而且毕竟我的路由器已经是5年前的了,说不定换新的会快一点,或者等有钱了换个SSD硬盘吧(好像有点奢侈)。

最后修改:2020 年 10 月 03 日
如果觉得我的文章对你有用,请随意赞赏