Linux Tutorials - Herong's Tutorial Examples - v5.41, by Herong Yang
NFS (Network File System)
This section provides a tutorial example on how to setup NFS server and client to share sub-directories as 'nfs' file systems.
If you want to export a local file system to remote computer, you need to install and config NFS (Network File System) server.
Here is what I did on my CentOS 8 computer (IP: 172.16.1.10).
1. Install "nfs-utils" and the required package, "rpcbind":
herong$ sudo dnf install nfs-utils Upgraded: nfs-utils-1:2.3.3-41.el8_4.2.x86_64 herong$ sudo dnf install rpcbind Upgraded: rpcbind-1.2.5-8.el8.x86_64
2. Start "nfs-server" and the required service, "rpcbind", using "systemctl" commands. You can also run the server programs directly.
herong$ sudo systemctl start rpcbind herong$ sudo systemctl start nfs-server herong$ sudo /etc/init.d/rpcbind start Starting rpcbind: [ OK ] herong$ sudo /etc/init.d/nfs start Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ]
3. Check service ports:
herong$ rpcinfo -p program vers proto port service 100000 4 tcp 111 portmapper 100000 4 udp 111 portmapper 100005 1 udp 20048 mountd 100005 2 tcp 20048 mountd 100003 3 tcp 2049 nfs 100021 1 udp 52058 nlockmgr 100021 1 tcp 46069 nlockmgr ...
4. Define and control directories to be exported to given IP addresses or IP address ranges.
herong$ sudo vi /etc/exports /home/herong/data 172.16.1.11(rw,sync) /home/herong/pub 172.16.1.0/24(rw,sync)
5. Refresh NSF server cache.
herong$ sudo systemctl stop nfs-server herong$ sudo systemctl start nfs-server herong$ sudo /etc/init.d/nfs stop herong$ sudo /etc/init.d/nfs start
6. Test on the server locally. It looks good.
herong$ showmount --export 172.16.1.10 Export list for 172.16.1.10: /home/herong/data 172.16.1.11 /home/herong/pub 172.16.1.0/24
7. Open firewall to allow NSF and required services. Since the port numbers are dynamically mapped, I just added the remote computer to the "trusted" zone to accept all ports.
herong$ sudo firewall-cmd --zone=trusted --add-source=172.16.1.0/24 herong$ sudo firewall-cmd --runtime-to-permanent
8. Go to my other Linux computer (IP: 172.16.1.11). Mount the exported sub-directory.
herong$ sudo mkdir /mnt/data herong$ sudo mount -t nfs 172.16.1.10:/home/herong/data /mnt/data
10. Go to the NFS server computer (IP: 172.16.1.10) and verify log messages.
herong$ sudo cat /var/log/messages | grep mount rpc.mountd: authenticated mount request from 192.168.1.191:960 for /home/herong/data (/home/herong/data)
If you see the "No such file or directory" error on the client computer, you are trying to mount a NFS file system that is not exported by the server.
herong$ sudo mount -t nfs 172.16.1.10:/junk /mnt/junk mount.nfs: mounting 172.16.1.10:/junk failed, reason given by server: No such file or directory
If you see "Access denied by server while mounting" error on the client computer, your Host name or IP address is not listed in the server's /etc/exports file.
herong$ sudo mount -t nfs 172.16.1.10:/data /mnt/data mount.nfs: access denied by server while mounting 172.16.1.10:/data
If a file on an exported NSF directory is lack of "read" permission for "other", you will get the "Permission denied" error on the remote system even as a "root" user.
You can also use "exportfs" to manage exported NFS file systems.
# Refresh exported NFS file systems herong$ sudo exportfs -r # Show exported NFS file systems herong$ sudo exportfs -s /home/herong/pub 172.16.1.0/24(sync,wdelay,hide,no_subtree_check, sec=sys,rw,insecure,root_squash,no_all_squash)
Table of Contents