Ubuntu设置NFS共享文件服务器

多个服务器共享同一个文件服务器,通过NFS网络文件系统来设置。

NFS Network File System 网络文件系统

安装NFS服务

1
2
3
4
5
6
7
8
9
10
11
//安装服务
sudo apt-get install nfs-kernel-server

//配置文件
sudo vim /etc/exports

exports文件内容添加
/home/alwynxu/file *(rw,sync,no_root_squash,no_subtree_check)

//重启服务
service nfs-kernel-server restart

查看服务器共享的目录

1
2
3
alwynxu@alwynxu:~$ showmount -e localhost
Export list for localhost:
/home/alwynxu/file *

其中/home/alwynxu/file当前nfs服务器要共享的文件夹路径,* 代表所有客户端均可访问 (rw,sync,no_root_squash,no_subtree_check)一些权限设置

在客户端执行命令,将nfs共享服务器文件夹挂载到当前客户端的/Users/alwynxu/mountfile路径下, 注意添加-o resvport 选项,否则会出现无法访问的错错误
alwynxudeMacBook-Pro:mountfile alwynxu$ sudo mount -t nfs -o rw -o tcp -o resvport 127.0.0.1:/home/alwynxu/file /Users/alwynxu/mountfile

错误

1
2
alwynxudeMacBook-Pro:mountfile alwynxu$ sudo mount -t nfs -o rw -o tcp 127.0.0.1:/home/alwynxu/file /Users/alwynxu/mountfile
mount_nfs: can't mount /home/alwynxu/file from 127.0.0.1 onto /Users/alwynxu/mountfile: Operation not permitted

java中接受的上传文件可以通过MultipartFile来接收并通过transferTo将文件上传到NFS服务器

1
2
3
4
5
6
7
8
9
public void uploadFile(MultipartFile file){
//注意此文件夹路径是挂载点的路径,也即当前应用所在的服务器中的路径,而非NFS服务器上的路径地址。
String serverPath = "/home/alwynxu/file";
try{
file.transterTo(new File(serverPath+ "/girl.png"));
}catch(Exception e){
e.printStackTrace();
}
}

0%