Ubuntu environment
1. Install nfs-server
$ apt-get install nfs-kernel-server
2. Create a dedicated folder for nfs
$ mkdir /data/share
3. Configure nfs
$ vi /etc/exports
/data/share/ *(async,insecure,no_root_squash,no_subtree_check,rw)
/data/share: The directory shared with the nfs service client, this path must be the same as the path of the file you set earlier
*: All users who can ping the same host
192.168.1.*: Specify the network segment, users in this network segment can mount
192.168.1.12: Only this user can mount
rw: Clients that mount this directory have read and write permissions to this shared directory
async: data is written to memory and hard disk synchronously
no_root_squash: The root user has full administrative access to the root directory.
no_subtree_check: Do not check parent directory permissions.
4. Restart the rpcbind service
nfs is an RPC program. Before using it, you need to map the port and set it through rpcbind.
$ /etc/init.d/rpcbind restart
5. Restart the nfs service
$ /etc/init.d/nfs-kernel-server restart
6. Mount command
$ mount -t nfs 192.168.0.128:/data/share /nfsdir
via docker centos
#Start centos Docker
$ docker run -itd --name docker-centos centos:latest
#Execute the following command after entering docker, and report an error
$ systemctl status firewalld.service
Failed to get D-Bus connection: Operation not permitted
#Solution:
$ docker run --privileged -itd --name docker-centos centos:latest /usr/sbin/init
$ showmount -e 172.17.0.2
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
解决方法:
被访问的NFS服务器上的防火墙没有添加规则,向iptables里面添加查看的所有端口即可(也可以关闭防火墙,不过这样是比较不安全的)
查看端口命令:
$ rpcinfo -p localhost
$ mount -t nfs 172.17.0.2:/data /data -o proto=tcp -o nolock
mount.nfs: Operation not permitted
解决方法:
docker容器启动的时候需要加上--privileged参数,否则启动nfs服务时候会提示权限不足报错。主要是这个过程中涉及到了mount操作, 使用该参数后使得container内的root拥有真正的root权限。
$ showmount -e localhost
clnt_create: RPC: Remote system error - Address family not supported by protocol
解决办法:
启动rpcbind和nfs
$ service rpcbind restart
$ service nfs restart
取消挂载
$ umount -l /data/share