views
- To share files between Linux computers using NFS, start by installing "nfs-kernel-server" (Ubuntu/Debian) or "nfs-utils" (Red Hat/Fedora) on the server.
- Create the directory you want to share, and add it to /etc/exports along with the hostnames/IP addresses that should have access.
- Install the NFS client package on the other Linux systems, mount the shared directory, and add it to /etc/fstab.
Creating the Server
Install the NFS server on the host system. You can install the latest version of NFS on your Linux server (NFS 4) using your distribution's package manager. For example, if you're using Ubuntu or another Debian-based Linux, run sudo apt install nfs-kernel-server in a terminal to install the NFS server package. On a Red Hat -based Linux server, use sudo dnf install nfs-utils.
Start the NFS server. Because NFS is a systemd service, you'll use systemctrl to start it. Run this command: Ubuntu/Debian: sudo systemctl start nfs-kernel-server.service Red Hat/Fedora/CentOS: sudo systemctl enable --now nfs-server Note that beginning with NFS 4, it's no longer necessary to use the rcpbind service with NFS.
Create a directory for sharing files (optional). If you're using NFS to store users' home directories on a central server instead of on individual clients, you can skip this step. If you want to store other general files in a directory that's accessible to other Linux systems on the network, you can create a directory for your NFS share. Here's how: Let's say you want to create a directory at /var/nfs/employees for files that employees need to access on their Linux systems. To create that directory, use sudo mkdir -p /var/nfs/employees. Using sudo ensures that the directory is owned by root on the server. This will prevent users with root access on their own systems from having root access on the server directory. Then, change the group ownership of the directory using sudo chown nobody:nogroup /var/nfs/employees to match how NFS translates any root operations on the client system. However, don't change group ownership of /home if you plan to also share home directories via NFS unless you want to break things. You can create multiple directories to share if you'd like. You can share as many directories as you need to.
Open /etc/exports for editing. Directories listed in /etc/exports are those you'll be sharing with NFS. You can use any text editor, such as Vim or Nano, to edit the file. Use sudo to edit the file. E.g., sudo vim /etc/exports. If you're setting up NFS for the first time, this file won't exist yet. Don't worry if it's blank.
List each directory you want to share from this server. The syntax you'll use is
Apply your configuration changes. When you make changes to /etc/exports, run the command sudo exportfs -ar to make them active.
Allow NFS through your firewall. Before you can mount NFS directories on client systems, you'll need to ensure the NFS service is allowed through your firewall. The commands will vary depending on the type of firewall you have.
For example, if you're using firewalld, use sudo firewall-cmd --add-service nfs –permanent.
For ufw, you can use sudo ufw allow from
Connecting Client Computers
Install NFS on the client systems. Each Linux system on your network that needs access to the shared files must install the NFS client. Ubuntu/Debian: sudo apt install nfs-common Red Hat, Fedora, CentOS: Use the same command you used to install NFS on the server sudo dnf install nfs-utils
Create a directory to mount the shared files. You can name this whatever you'd like. For example, you can type sudo mkdir /nfs to create a folder called "nfs." In our previous example, we created /var/nfs/employees on the server system. If we want to mount that directory at /nfs/employees on the client system, we'll also need to sudo mkdir /nfs/employees locally. Create a directory even if you're mounting home directories from the NFS server. E.g., you can create /nfs/home on this system.
Mount the shared directory to the client system. To do this, you'll use the mount command. In this example, we'll mount /var/nfs/employees from our server, whose IP address is 192.168.0.5, to the /nfs/employees directory on the client system: sudo mount 192.168.0.5:/var/nfs/employees /nfs/employees
Open /etc/fstab for editing. To ensure the files shared from the NFS server are mounted automatically at boot time, we'll want to add the mounts to /etc/fstab on the client systems. You can use any text editor, such as Vim, Nano, or Pico, to edit the file. Use sudo to edit the file. E.g., sudo vim /etc/fstab.
Add the NFS server name and directory to /etc/fstab. Add a separate entry for each directory you want to mount. The syntax is serverIP:sharedDirectory nfs rsize=8192,wsize=8192,timeo=14,intr. For a full explanation of the different values, use man nfs at the prompt. Replace serverIP with the IP address or hostname of the NFS server. Replace sharedDirectory with the local NFS mount point (e.g., /nfs/home). Using the above examples, the line might look like: 192.168.1.5:/export/Shared /sharedFiles nfs rsize=8192,wsize=8192,timeo=14,intr.
Test the mount. To make sure the shared files are mounted on the client system, type mount -a to view all mount points. You should now be able to access files from the server system on the client.
Comments
0 comment