Today we will go over an alternate way of setting up shares on CentOS 7. In an earlier post we saw how we can share stuff with Samba, which is the preferred way, especially if you have mixed environments. But today I also wanted to go through the process using the veteran: NFS! For this example, we’ll be looking at the newest version, NFS4, which adds performance and security features, but also operates a little bit differently than its predecessors.
The Network File System allows clients to mount remote filesystems as if they were present locally. By default, the NFS server is listening on port 2049. In version 4, NFS requires the use of TCP.
If you don’t have it already, install the nfs-utils package. Next, start NFS with the command: systemctl start nfs. For the share, I created some directory and put a file in it:
chmod 777 /var/nfs-share/
Now we have to edit the /etc/exports file, which contains the configuration for the shares. The format is as follows:
You can specify the client by hostname, domain name, IP address or network range. In this example, I will allow all hosts on my local subnet to have read write access to the share, and permit root users to retain their privileges:
On the client, I first made a directory for the shares:
To mount the share, the command looks like this:
mount -t nfs -o options server:/export /mount/directory
In my case, I mounted the exported share with:
mount -t nfs 192.168.217.131:/var/nfs-share /mnt/nfs/
Then I went to the directory and read the file:
root@kali:/mnt/nfs# cat file.txt
When done, unmount the filesystem:
If instead of manually mounting the share, we would want it automatically mounted at boot, we’d have to edit /etc/fstab:
# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
# <file system> <mount point> <type> <options> <dump> <pass>
192.168.217.131:/var/nfs-share /mnt/nfs nfs rw 0 0
Now reboot the client and check that the share was mounted:
mount | grep nfs
192.168.217.131:/var/nfs-share on /mnt/nfs type nfs4 (rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=0.0.0.0,local_lock=none,addr=192.168.217.131)
So, it’s pretty easy and fast to set up NFS shares, but keep in mind this tutorial only scratched the surface of the available configuration options.
/ Q: What's tiny and yellow and very, \
| very, dangerous? A: A canary with the |
\ super-user password. /