[centOS7] Super detailed and complete tutorial on physical machine configuration NFS (Network File System)

To configure NFS (Network File System) on a CentOS 7 physical machine, follow the steps below:
Goal: Achieve A shared directory, B access A
Prerequisite: Prepare two hosts A and B, and
prepare them in the same LAN environment

system role IP
CentOS7 server (A) 192.168.81.137
CentOS7 client (B) 192.168.81.138

1. Configure the A-side
1. Install the NFS software package: sudo yum install nfs-utils.
Open the terminal or command line interface, and execute the installation command as the root user or a user with administrator rights on the physical machine. 
2. How to share the home directory
first Step 2: Install the NFS software package.
Step 2: Configure the NFS server. Edit
the NFS server configuration file /etc/exports.
Open the terminal and execute the command: sudo vi /etc/exports
configuration file to add the configuration of the shared directory: /home *(rw,sync, no_root_squash)

Save and close the file: wq

update configuration: sudo exportfs -r
3. Start the NFS service
. Execute the command to start the NFS service: sudo systemctl start nfs-server

4. Set the NFS service to start automatically at boot:
execute the command to set the NFS service to start automatically at boot. Start: sudo systemctl enable nfs-server

5. Configure the firewall:
If the firewall (firewalld) on CentOS 7 is running, you need to allow NFS traffic to pass through the firewall.
Execute the following command to open the port of the NFS service:
sudo firewall-cmd --permanent --zone =public --add-service=nfs
sudo firewall-cmd --permanent --zone=public --add-service=rpc-bind
sudo firewall-cmd --permanent --zone=public --add-service=mountd
sudo firewall-cmd --reload
6. How Share a new directory
Step 1: Install the NFS software package
Step 2: Create a new shared directory: sudo mkdir /shared_directory
Step 3: Configure the NFS server
Edit the NFS server configuration file /etc/exports
Open the terminal and execute the command: sudo vi /etc /exports
configuration file Add the configuration of the new shared directory: /shared_directory *(rw, sync, no_root_squash)

Save and close the file: wq

Update the configuration: sudo exportfs -r
7. Comments

parameter Meaning
192.168.X.XXX Client IP address
/home Is the directory that needs to be shared
/shared_directory It is a new directory that needs to be shared.
‘* Indicates that all clients are allowed access
rw Indicates read and write access is allowed
sync Indicates that data is written to the memory and hard disk synchronously to ensure data security.
rsync Data is temporarily stored in memory rather than written directly to the hard drive
no_root_squash Indicates that the root user is allowed to access the shared directory
no_all_squash No matter what user the NFS client uses when connecting to the server, it will not have anonymous user rights for the directories shared by the server.

8. If you specify an IP client to access the /home directory, for example: a client with an IP address of 192.168.81.137

parameter Meaning
/home *(rw,sync,no_root_squash) Allow all clients to access /homethe directory in read-write mode (rw) and maintain synchronization (sync). no_root_squashoption will allow clients to access the shared directory with root privileges.
/home 192.168.X.XXX(rw,sync,no_root_squash) Allow 81.137 clients to access /homethe directory in read-write mode (rw) and maintain synchronization (sync). no_root_squashoption will allow clients to access the shared directory with root privileges.

9. Check the mounting status of the nfs server: showmount -e localhost

Now, your CentOS 7 physical machine has been configured with an NFS server. Other clients can access the shared directory using the NFS protocol.

2. Configure B-side
1. Install the NFS software package: sudo yum install nfs-utils.
Open the terminal or command line interface, and execute the command installation on the client as the root user or a user with administrator rights.

2. Mount on the client. Shared directory:
Execute the following command on the client that wants to access the shared directory: sudo mkdir /mnt/home
Execute the following command on the client that wants to access the shared directory: sudo mkdir /mnt/shared_directory
3. Mount the NFS shared directory:
execute Command to mount the NFS shared directory to the client's mount point, replace <NFS_server_IP> with the server's IP address, and <home / shared_directory> with the path
template of the shared directory: sudo mount -t nfs <NFS_server_IP>:/home /mnt/ home
replacement: sudo mount -t nfs 192.168.81.137:/home /mnt/ home
template: sudo mount -t nfs <NFS_server_IP>:/shared_directory /mnt/ shared_directory
replacement: sudo mount -t nfs 192.168.81.137:/ shared_directory /mnt/ shared_directory
4. Verify the mount:
execute the command to check whether the mount is successful: df -h
5. If successful, the mount point /mnt/ home` is listed in the output, indicating that the shared directory has been successfully mounted on the client.

6. If you want the shared directory to be automatically mounted every time you start the client, you can mount Add the information to the client's /etc/fstabfile:
Step 1: Edit the configuration file /etc/fstab.
Open the terminal and execute the command: vi /etc/fstab.
Execute the command to add the NFS shared directory mounting information to the client's /etc/fstab file. Replace <NFS_server_IP> with the IP address of the server
Template: <NFS_server_IP>:/home /mnt/home nfs defaults 0 0
Replace: 192.168.81.137:/home /mnt/home nfs defaults 0 0
Template: <NFS_server_IP>:/shared_directory /mnt/shared nfs defaults 0 0
Replace: 192.168.81.137:/shared_directory /mnt/shared_directory nfs defaults 0 0

7. Use the command to test whether the mount configuration is correct: sudo mount -a
If no error message appears, it means the mount is Configuration loaded successfully. You can restart the client to verify that the shared directory is automatically mounted on startup.
3. Possible problems
: Error: mount.nfs: No route to host
This error usually means that the target host cannot be reached. Please make sure that your machine can access the IP address of the target machine and that the NFS service on the target machine is running before executing this command.
You can solve this problem in the following ways:
1. Use the command to check the status of the NFS service: sudo systemctl status nfs

2. Check the network connection
Check the network connection between your machine and the target machine to ensure that they can access each other. You can try using the ping command to test the network connection, for example:


3. Check the target machine's firewall settings
If the target machine's firewall is enabled, make sure that the NFS service port has been added to the firewall rules. NFS uses TCP and UDP port 2049 by default. Use the command to check the firewall settings on the target machine: firewall-cmd --list-all

4. Add firewall rules

4. Access the mounted shared Home directory
To access the mounted shared Home directory, you can mount it on the local system Click on the path to access. In the above example, we are using  /mnt/home .
1. Use the command to access the mounted shared Home directory: cd /mnt/home

This will switch to the mounted shared Home directory. Now you can use regular file operation commands (for example  ls, cp, mv) to view and operate the Home directory files on the remote host. After you complete the operation on the shared Home directory, you can use  cd the command to return to other directories
5. Verify that A creates the file and B can query
1. Create ty.txt on the server
and use the command to create the target path: mkdir -p /mnt/home
Use the command to create the target file: touch /mnt/home/ty.txt.
Query the command to find the target file: ls /mnt/home/ty.txt

2. Query ty.txt on the client.
Now, you can access and use the hanger on the client. Download the shared directory `/mnt/shared_directory` and perform read and write operations on the files in it. Please ensure that the client can communicate normally with the CentOS 7 physical machine and allow NFS traffic to pass through the client's firewall.

Guess you like

Origin blog.csdn.net/weixin_71435518/article/details/131871795