这里的分享主要指在局域网内部,通过samba或nfs协议直接读取nas中的内容,通过外网进行访问在后面的同步章节介绍。
网络结构
简单介绍一下家里的网络结构,电信200Mb宽带,路由器是网件的R6800,没有交换机,一个树莓派常年开着,Nas基本人在就开,我的两台笔记本T510/X201都运行debian,kitty的笔记本X250运行win7,一台小米电视3s,其他还有手机、ipad,对了还有一个刚到的福利,小度在家,感觉没大用。可惜的是,主力笔记本T510网卡虽然支持千兆,但实际居然只有百兆,跑到intel主页下载最新的驱动,仍然解决不了。还有装修的时候考虑不周全,埋设的网线都是垃圾网线,虽然面上的网线后来都换成了六类的,但先天的问题没法解决。无线的话,没有做AP,R6800支持2.4G和5G两个频段的wifi,实测下来差距明显,一个覆盖范围更广,一个传输速度更快,鱼与熊掌不可兼得呀。
西数的硬盘作为下载和分享盘,samba协议虽然速度一般,但兼容性强,几乎所有设备都支持,所以在西数盘上面配置samba服务。而东芝盘配置nfs,仅作备份用。
samba
西数的硬盘挂载到/mnt/storage下。
- 安装samba:
sudo apt install samba groups # 查看当前组 sudo groupadd homeshare # 新建homeshare组 sudo useradd -g homeshare -s /usr/sbin/nologin -M kitty # 新建kitty用户并加入homeshare组 sudo passwd kitty sudo usermod -a -G homeshare swint # 将swint加入homeshare组,使该组成为swint的附加组 mkdir /mnt/storage/share # 创建分享目录 sudo chown -R nobody. /mnt/storage/share sudo chmod -R 777 /mnt/storage/share # 如设置成755,kitty写入失败 sudo smbpasswd -a swint # 在samba中创建与系统用户同名的用户 sudo smbpasswd -a kitty
- 修改 /etc/samba/smb.conf 配置文件,将Share Definitions下自带各配置注释掉,然后加入:
[wd-storage] comment = WD storage path = /mnt/storage/share browseable = yes #是否显示在客户机中 guest ok = no #不属于homeshare组的用户是否可访问 writeable = yes #是否可写,write list/read list定义例外。 create mask = 0640 #新建文件权限,所有者可读写,所属组可读,其他人无权限。 directory mask = 0750 #新建文件将权限,所有者可读写及执行,所属组可读及执行,其他人无权限。 valid users = @homeshare #能够访问的用户(组),加@代表组,多个用户用逗号隔开。 write list = swint #格式与valid users相同。 read list = kitty vfs objects = recycle #客户端删除操作时,移动到回收站。 recycle:repositary = .recycle recycle:exclude = .tmp|.temp recycle:keeptree = yes recycle:versions = yes
- 重启samba服务,服务端配置完毕。
sudo systemctl restart smbd.service
- 如客户端是window,不需要任何配置,直接在网络邻居中找到并打开就可以,实测kitty连有线网络,传输速度可以达到100MB以上。如客户端是linux,修改 /etc/fstab :
//ip/wd-storage /mnt/wd-storage-share cifs noauto,x-systemd.automount,x-systemd.device-timeout=30s,x-systemd.idle-timeout=10min,_netdev 0 0
nfs
服务端
- 安装:
sudo apt install nfs-kernel-server
sudo chown -R nobody:nogroup /mnt/share-nfs
sudo chmod -R 777 /mnt/share-nfs
- 修改 /etc/exports :
/mnt/share-nfs 192.168.1.*(rw,sync,no_subtree_check,no_root_squash,crossmnt)
-
rw
Read/write filesystem.
-
sync
Force all transfers to operate in synchronous mode, so all clients will wait until their operations are really done. This can avoid data corruption in the event of a server crash
-
no_subtree_check
Disable file location checks on partial volume exports. This option will speed up transfers on full volume exports.
-
no_root_squash
The root account on the client machine will have the same privilege level as the root on the server machine. On the other hand, *root_squash* causes root on the client to have the same access type as nobody on the server
-
crossmnt
可以在nfs分享目录中包括加载(mount)点,否则显示空文件夹。
-
重启服务
exportfs -a # reload all NFS exports
sudo systemctl restart nfs-server.service
这里仍然有问题,若/mnt/share-nfs为挂载点,可使用systemd的automount模式,但不能加~netdev选项~,否则开机ssh连不上。另外若nfs服务开启,似乎需监控硬盘,导致自动卸载失败。
客户端
- 安装:
sudo apt install nfs-common sudo mount -t nfs IP:/mnt/share-nfs /mnt/nfs/
- 修改 /etc/fstab :
IP:/mnt/share-nfs /mnt/nfs/ nfs rw,async,hard,intr,noexec 0 0
-
rw
Read/write filesystem.
-
hard
Applications using files stored on an NFS will always wait if the server goes down.
-
intr
Allows user interruption of processes waiting on a NFS request.
-
noexec
Disable execution of binaries or scripts on an NFS share.