Last modified: July 19, 2020
This article is written in: 🇺🇸
NFS, or Network File System, is a protocol that allows different computers to share files over a network as if they were on the local machine. This means you can access files on another computer just like you would access files on your own, making collaboration and resource sharing much easier. NFS is compatible with various operating systems, including Linux, macOS, and Windows.
Client Network Server
+--------+ +--------+
| | <-- NFS Protocol Communication --> | |
| User | | NFS |
| Space | | Server |
+--------+ +--------+
| |
+--------+ +--------+
| NFS | | NFS |
| Client | | Daemon |
+--------+ +--------+
| |
+--------+ +--------+
| File | | File |
| System | | System |
+--------+ +--------+
| |
+--------+ +--------+
| Disk | | Disk |
| Storage| | Storage|
+--------+ +--------+
In this diagram, the client interacts with the NFS server over the network. The NFS client software translates local file operations into NFS protocol requests, which are sent to the server. The server processes these requests and accesses its local file system accordingly, providing the requested data back to the client.
If you're looking to share directories from a CentOS 7 machine so that other computers on the network can access them, you'll need to set up that machine as an NFS server. Here's how you can do it step by step.
First, you'll need to install the NFS utilities, which include essential services and tools for NFS functionality. Open your terminal and run:
yum install nfs-utils nfs-utils-lib
This command downloads and installs the nfs-utils
and nfs-utils-lib
packages. After running it, you should see output indicating that the packages have been successfully installed, along with any dependencies.
NFS relies on several background services to operate correctly. These services include rpcbind
, nfs-server
, nfs-lock
, and nfs-idmap
. Each service plays a role in handling network communication, file locking, and user ID mapping.
To enable these services to start at boot time and to start them immediately, execute the following commands:
systemctl enable rpcbind
systemctl start rpcbind
systemctl enable nfs-server
systemctl start nfs-server
systemctl enable nfs-lock
systemctl start nfs-lock
systemctl enable nfs-idmap
systemctl start nfs-idmap
For example, after starting the nfs-server
service, you can check its status by running:
systemctl status nfs-server
You should see output similar to:
â—Ź nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
Active: active (exited) since Wed 2024-10-10 12:34:56 EDT; 10s ago
This output confirms that the NFS server service is active and running.
Now it's time to specify which directories you want to share with clients and define the access permissions. This is done by editing the /etc/exports
file.
Open the exports file with your favorite text editor:
vi /etc/exports
Add the following line to share the /opt/shared
directory with clients on the 192.168.1.0/24
network:
/opt/shared 192.168.1.0/24(rw,sync,no_root_squash,no_all_squash)
Here's what each part means:
/opt/shared
is the directory on the server that you want to share.192.168.1.0/24
is the range of IP addresses that are allowed to access the share.(rw,sync,no_root_squash,no_all_squash)
is the options that control access permissions and behavior.To explain the options in more detail, let's look at them in a table:
Option | Description |
rw |
Allows both read and write access to the shared directory. |
sync |
Ensures that changes are written to disk before the server replies to the client. |
no_root_squash |
Allows the root user on the client machine to have root privileges on the shared directory. |
no_all_squash |
Preserves the original user and group IDs when accessing files on the server. |
By setting these options, you're controlling how clients interact with the shared directory, including who can read or write files and how user permissions are handled.
After editing the exports file, you need to inform the NFS server of the changes. This is done using the exportfs
command:
exportfs -r
This command re-reads the /etc/exports
file and updates the NFS server's table of exported file systems without requiring a restart.
To confirm that your directory is being shared correctly, you can use:
exportfs -v
The output will display the shared directories along with their options, something like:
/opt/shared 192.168.1.0/24(rw,wdelay,root_squash,no_subtree_check,secure)
This confirms that /opt/shared
is exported and accessible to clients in the specified IP range with the defined options.
For clients to access the NFS share, the server's firewall must allow NFS-related traffic. You can adjust the firewall settings using the firewall-cmd
utility:
firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=mountd
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --reload
These commands open the necessary ports for NFS services and reload the firewall configuration to apply the changes immediately.
To ensure that all changes are properly applied, it's a good idea to restart the NFS server:
systemctl restart nfs-server
This restarts the NFS server service, ensuring that it recognizes the new configuration and that any pending changes are implemented.
Once the NFS server is set up, the next step is to configure a client machine so it can access the shared directory. Here are the steps to set up an NFS client on CentOS 7.
Just like on the server, the client needs the NFS utilities installed to communicate with the NFS server:
yum install nfs-utils nfs-utils-lib
This installs the necessary packages for NFS client functionality.
The rpcbind
service is essential for NFS communication on the client side as well. Enable and start it by running:
systemctl enable rpcbind
systemctl start rpcbind
You can verify that it's running with:
systemctl status rpcbind
Decide where you want the NFS share to be mounted on your client system. For example, you might create a directory under /mnt
:
mkdir /mnt/nfs_shared
This directory will serve as the access point for the shared files from the server.
Now you can mount the NFS share using the mount
command:
mount -t nfs 192.168.1.100:/opt/shared /mnt/nfs_shared
Breaking down this command:
-t nfs
specifies the file system type as NFS.192.168.1.100:/opt/shared
indicates the NFS server's IP address and the shared directory./mnt/nfs_shared
is the local directory where the NFS share will be mounted.After running this command, the contents of /opt/shared
on the server will be accessible under /mnt/nfs_shared
on the client.
To ensure that the NFS share is mounted correctly, you can use:
mount | grep nfs
This should display something like:
192.168.1.100:/opt/shared on /mnt/nfs_shared type nfs (rw,addr=192.168.1.100)
Alternatively, you can list the contents of the mounted directory:
ls /mnt/nfs_shared
This should show the files and directories that are present on the server's shared directory.
If you want the NFS share to be mounted automatically every time the client machine boots up, you can add an entry to the /etc/fstab
file.
Open the /etc/fstab
file:
vi /etc/fstab
Add the following line at the end:
192.168.1.100:/opt/shared /mnt/nfs_shared nfs defaults 0 0
This tells the system to mount the NFS share at /mnt/nfs_shared
using default options during the boot process.
When using NFS to share files across different systems, managing permissions becomes a critical aspect to ensure that users have appropriate access to shared resources. One of the main challenges arises from the way NFS handles user and group identities, specifically through User IDs (UIDs) and Group IDs (GIDs). Understanding how these identifiers work and the potential caveats can help prevent permission issues and security risks.
In Unix-like systems, every file and directory is associated with a UID and GID, which determine the ownership and group association. Permissions are then applied based on these IDs, controlling read, write, and execute access for the owner, group, and others.
When an NFS client accesses files on an NFS server, the client uses its local UID and GID to determine permissions. The NFS server, however, relies on its own UID and GID mappings to enforce access controls. If the UIDs and GIDs do not match between the client and server, users might experience unexpected permissions—either being denied access to files they should have rights to or gaining access to files they shouldn't.
For example, suppose a user named "alice" has a UID of 1000 on the NFS client but a different UID on the NFS server. When "alice" tries to access a file she owns on the client, the server might not recognize her as the owner, leading to permission issues.
One of the main caveats with NFS permissions is that UIDs and GIDs are numerical and local to each system. There's no inherent mapping of usernames to UIDs across different machines unless explicitly managed. This can lead to several issues:
To mitigate these issues, it's important to establish a consistent mapping of UIDs and GIDs across all systems involved in NFS sharing. Here are some approaches to achieve this:
idmapd
service. This allows for consistent permissions without relying on matching numerical IDs.idmapd
for NFSv4Using idmapd
can simplify UID and GID management by mapping usernames between client and server. Here's how to set it up:
I. Install and Configure idmapd
Ensure that nfs-utils
is installed, which includes idmapd
. Edit the /etc/idmapd.conf
file on both the client and server to set the same domain:
[General]
Domain = example.com
II. Start the idmapd
Service
Enable and start the idmapd
service on both systems:
systemctl enable nfs-idmapd
systemctl start nfs-idmapd
III. Mount the NFS Share Using NFSv4
On the client, mount the NFS share specifying NFS version 4:
mount -t nfs4 server.example.com:/shared /mnt/shared
Root squashing is an NFS security feature that maps requests from the root user (UID 0) on the client to an anonymous or unprivileged user on the server, typically nfsnobody
. This prevents a root user on a client machine from having root privileges on the NFS server, enhancing security.
While root squashing protects the server, it can also cause permission issues when administrative tasks require root access to shared files. If necessary, you can disable root squashing by modifying the export options in /etc/exports
:
/shared 192.168.1.0/24(rw,sync,no_root_squash)
However, disabling root squashing should be done cautiously, as it can expose the server to security risks.
no_all_squash
and all_squash
OptionsThe no_all_squash
option (default behavior) preserves the original UID and GID of users accessing the NFS share. Conversely, the all_squash
option maps all user requests to the anonymous user, which can be useful in environments where you want to restrict all client access to a single user identity on the server.
For example, to map all client access to the nfsnobody
user, you can use:
/shared 192.168.1.0/24(rw,sync,all_squash)
If users encounter "Permission Denied" errors when accessing NFS shares, it's often due to UID and GID mismatches or incorrect export configurations. To troubleshoot:
/etc/exports
are correctly set and that the client IP is allowed.Imagine a user named "bob" needs access to a shared directory over NFS. On the server, "bob" has a UID of 1001, but on the client, his UID is 1002. When "bob" tries to access files he owns on the server, the server doesn't recognize him as the owner because the UIDs don't match. As a result, he might be denied access or have limited permissions.
To resolve this, you could:
idmapd
to map "bob's" username to the correct UID, allowing consistent permissions without changing UIDs.While NFS simplifies file sharing across networks, it's important to implement security measures to protect your data.
no_root_squash
as it can grant root access to clients, which is a potential security risk.If you need to view the current shared directories and their options on the server, you can use:
exportfs -v
This will display detailed information about each exported directory.
If you decide to stop sharing a directory, you can unexport it with:
exportfs -u /opt/shared
This command stops the NFS server from sharing /opt/shared
.
If clients are having trouble accessing the NFS share, here are some steps you can take:
/etc/exports
file for any typos or incorrect options.For better performance, especially in environments with heavy file access, consider the following:
async
option in /etc/exports
to allow asynchronous writes. This can improve performance but may risk data integrity in the event of a crash.rsize
and wsize
to control the read and write buffer sizes.For example, mounting with specific read and write sizes:
mount -t nfs -o rsize=8192,wsize=8192 192.168.1.100:/opt/shared /mnt/nfs_shared
This sets the read and write buffer sizes to 8KB, which can improve performance depending on your network conditions.
NFS can be used in mixed operating system environments. For example, macOS and Windows systems can also act as NFS clients.
mount
command in the terminal or through the Finder's "Connect to Server" option.mount
command in the command prompt.There are different versions of NFS, each with its own features:
rpcbind
service. Describe how each component contributes to the operation of NFS and why they are necessary for the server to function correctly.rpcbind
service in NFS, including how it registers and maps network services to the correct ports. Explain why rpcbind
is essential for NFS communication and the consequences of it not being active./etc/exports
file and setting appropriate access permissions. Explain the purpose of the /etc/exports
file and why it is necessary for defining shared directories./etc/exports
file, such as ro
(read-only), rw
(read-write), and sync
(synchronized writes). Discuss how these options affect client access and the security of shared directories.mount
command, and verify the connection by accessing the files in the mounted directory. Explain how the client can confirm that the connection is stable and functioning as expected.exportfs
command to view, refresh, and unexport shared directories on the NFS server. Describe how exportfs
is used for real-time management of NFS shares and how it complements the configuration in the /etc/exports
file.