Skip to main content

How to Set Up and Use NFS on Linux

 


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.



Popular posts from this blog

Cockpit vs. Webmin: A Detailed Comparison for Linux Administration

  Introduction In the realm of Linux system administration, having efficient tools for managing and monitoring servers is crucial. Two popular tools that system administrators often use are  Cockpit  and  Webmin . Both of these tools provide a graphical interface accessible via a web browser, simplifying the management of Linux systems. This blog post will explore what Cockpit and Webmin are, their purposes, a comparison table, and additional information to help you choose the right tool for your needs. What is Cockpit? Overview Cockpit is a web-based graphical interface for managing Linux systems. It is designed to be easy to use, enabling both experienced and novice administrators to manage their systems effectively. Cockpit integrates seamlessly with the system’s existing infrastructure, providing real-time monitoring and management capabilities. Purpose Cockpit is primarily used for: Monitoring system performance and resource usage Managing system services Handli...

How to Set Up Custom Screen Resolution on Fedora 38 Permanently

  If you are using Fedora 38 as your operating system, you may have encountered some issues with the screen resolution. The default resolution may not be suitable for your monitor or your preferences, and you may want to change it to a higher or lower value. However, changing the resolution from the Settings menu may not work properly, or it may not persist after a reboot. In this blog post, I will show you how to set up a custom screen resolution on Fedora 38 permanently using some simple commands and configuration files. The first step is to disable the Wayland display server, which is the default display server for Fedora 38. Wayland is a modern and secure display server, but it may not support some custom resolutions or drivers. To disable Wayland, you need to edit the /etc/gdm/custom.conf file as root. You can use any text editor of your choice, such as nano, vim, or gedit. To open the file with nano, for example, you can type the following command in the terminal: sudo nano ...

Key Concepts and Tools for a Linux System Administrator

  A Linux System Administrator needs to have a comprehensive understanding of various concepts and tools to manage, configure, and maintain Linux systems effectively. Below is a categorized list of essential skills and tools with brief descriptions. Category Key Concepts & Tools Description Operating System Linux Distributions (e.g., Fedora, Ubuntu, CentOS) Knowledge of different Linux distributions, their package management systems, and unique features. Kernel Configuration and Management Understanding how to configure and optimize the Linux kernel for different workloads. System Boot Process (GRUB, systemd) Familiarity with the boot process, bootloaders, and system initialization processes. Command Line Skills Bash Shell Scripting Ability to write and debug shell scripts for automation of tasks. Core Commands (ls, cp, mv, rm, find, grep, awk, sed) Proficiency in using basic and advanced command-line utilities for system management. File System File System Hierarchy Standard (...