NFS (Network File System) is a Linux feature that allows you to share files and directories between computers and servers on a network. NFS is useful for saving disk space, simplifying file management, and providing centralized access to data. In this blog post, we will show you how to set up and use NFS on Linux, using Ubuntu and Fedora as examples.
Installing NFS Packages
To use NFS, you need to install the NFS packages on both the server and the client machines. The server is the machine that hosts the files and directories that you want to share, and the client is the machine that connects to the server to access or upload files.
On Ubuntu, Linux Mint, and other Debian-based distros, you can install the NFS packages with the following command:
$ sudo apt install nfs-kernel-server nfs-common
On Fedora, CentOS, AlmaLinux, and other RHEL-based distros, you can install the NFS packages with the following command:
$ sudo dnf install nfs-utils
Creating and Exporting NFS Shares
After installing the NFS packages, you need to create the directories that you want to share on the server machine. For example, if you want to share a directory called /media/nfs, you can create it with the following command:
$ sudo mkdir -p /media/nfs
Then, you need to configure the permissions and ownership of the directory, depending on who you want to allow access to it. For example, if you want to give read and write access to all users, you can use the following commands:
$ sudo chmod 777 /media/nfs
$ sudo chown nobody:nogroup /media/nfs
Next, you need to export the directory by adding an entry to the /etc/exports file. This file contains the information about the NFS shares and their options. You can edit the file with your favorite text editor, such as nano:
$ sudo nano /etc/exports
In the file, you need to specify the path of the directory, the IP address or hostname of the client machine, and the options for the share. The options can be used to control the access level, performance, and security of the share. For example, the following entry will export the /media/nfs directory to the client machine with the IP address 192.168.1.100, and allow read and write access, asynchronous mode, and root squash:
/media/nfs 192.168.1.100(rw,async,no_root_squash)
You can find more information about the options and their meanings in the man page of exports.
After editing the /etc/exports file, you need to reload the NFS service to apply the changes. You can use the following command:
$ sudo exportfs -ra
You can also check the status of the NFS service with the following command:
$ sudo systemctl status nfs-server
Mounting and Using NFS Shares
Once you have exported the NFS shares on the server machine, you can mount them on the client machine. To do so, you need to create a mount point on the client machine, which is an empty directory where the NFS share will be mounted. For example, if you want to mount the NFS share at /mnt/nfs, you can create the directory with the following command:
$ sudo mkdir -p /mnt/nfs
Then, you need to mount the NFS share with the mount command. You need to specify the IP address or hostname of the server machine, the path of the NFS share on the server, and the path of the mount point on the client. For example, the following command will mount the /media/nfs share from the server with the IP address 192.168.1.10 to the /mnt/nfs mount point on the client:
$ sudo mount 192.168.1.10:/media/nfs /mnt/nfs
You can also specify some options for the mount command, such as the type of the file system, the access mode, and the mount options. For example, the following command will mount the NFS share with the type nfs, the read and write access mode, and the hard and intr mount options:
$ sudo mount -t nfs -o rw,hard,intr 192.168.1.10:/media/nfs /mnt/nfs
You can find more information about the options and their meanings in the man page of mount.
After mounting the NFS share, you can use it as a local directory on the client machine. You can list, create, delete, and modify files and directories on the NFS share, as long as you have the appropriate permissions. For example, you can use the following commands to create a file and a directory on the NFS share:
$ touch /mnt/nfs/test.txt
$ mkdir /mnt/nfs/test
You can also use the df command to check the disk usage and the free space of the NFS share:
$ df -h /mnt/nfs
To unmount the NFS share, you can use the umount command with the path of the mount point:
$ sudo umount /mnt/nfs
Automating NFS Mounts
If you want to mount the NFS share automatically when the client machine boots up, you can add an entry to the /etc/fstab file. This file contains the information about the file systems that are mounted at boot time. You can edit the file with your favorite text editor, such as nano:
$ sudo nano /etc/fstab
In the file, you need to add a line with the same format as the mount command, but with spaces instead of spaces. For example, the following line will mount the NFS share with the same options as before:
192.168.1.10:/media/nfs /mnt/nfs nfs rw,hard,intr 0 0
After editing the /etc/fstab file, you can test the mount with the mount -a command, which will mount all the file systems in the file:
$ sudo mount -a
If there are no errors, the NFS share will be mounted automatically at the next boot.
Conclusion
In this blog post, we have shown you how to set up and use NFS on Linux, using Ubuntu and Fedora as examples. NFS is a powerful and convenient feature that allows you to share files and directories between computers and servers on a network. We hope that this guide has helped you to understand and use NFS on your own system.