在 CentOS 7 下配置 NFS 服务
NSF (Network-base File System) can allow computers (clients) to access files on another computer(server) over the network even the clients have different operating systems from the server. And this article will guide you exstablishing your own NFS service on CentOS 7, which is generally used as server os.
Table of Contents
- Prerequisites
- NFS and RPC Services
- Configure NSF Server
- Configure NSF Clients
- Configure Firewalld for NFS and RPC Services
Prerequisites
To establish your NFS service, you need at least two instances (one as server, and the remains as clients) with CentOS 7 fresh installed. CentOS 7 supports NFSv3 and NFSv4.
NFS and RPC Services
NFS service needs RPC service, which includes:
- rpcbind.service
- rpcbind.socket
- rpcidmapd.service
- rpc-statd.service
Configure NSF Server
Now we will configure our nfs server supposed that ip is 192.168.0.2
.
1. Install Required NFS and RPC Packages
1 | sudo yum install -y nfs-utils rpcbind |
2. Export Shared Directories
The fortmat of nfs config file /etc/exports is like:
1 | /path/to/shared client1_ip (options) [client2_ip(options)] |
Suppose you want to expose the dirs to client1 with ip 192.168.0.3
, the file is like:
1 | /path/to/shared 192.168.0.3(rw,sync,no_wdelay) |
One example:
1 | /opt/shared 192.168.0.3(rw,sync) |
If you wish all clients have rights to access to the server, you can make it by the following configure:
1 | /path/to/shared *(ro,sync) |
Caution: do not grant writing right to everyone.
3. Create Shared Directories
1 | mkdir -p /path/to/shared |
4. Enable and Start Services
1 | sudo systemctl enable --now nfs-server |
5. Export the Share
1 | ## exportfs -r will re-exports entries in /etc/exports |
Configure NSF Clients
1. Install required packages
1 | ## install nfs-utils |
2. Mount the Shared NFS Directory
1 | sudo mkdir -p /local/path |
The following is an example
1 | sudo mount -t nfs -o rw,nosuid 192.168.0.2/opt/shared /opt/shared |
This mounts /opt/shared on 192.168.0.2 to /opt/shared on local machine
If you want to mount at boot time, you can add the following line to /etc/fstab
, and backup /etc/fstab
before changing it:
1 | remote_host:/path/to/shared /local/path nfs rw,nosuid 0 0 |
Configure Firewalld for NFS and RPC Services
If you enable the firewalld service, you need to allow the nfs, mountd, rpc-bind services through the firewall.
1 | sudo firewall-cmd --permanent --add-service=nfs |
Now you can access files on 192.168.0.2 from clients.