nfs introduction and example demonstration

Posted by zoran on Tue, 01 Feb 2022 03:42:19 +0100

NFS

nfs features
NFS (Network File System), namely Network File System, is one of the file systems supported by FreeBSD. It allows computers in the network to share resources through TCP/IP network

nfs working mechanism
nfs is based on rpc to realize network file system sharing.

RPC
RPC (Remote Procedure Call Protocol), a Remote Procedure Call Protocol, is a protocol that requests services from remote computer programs through the network without understanding the underlying network technology.
RPC Protocol assumes the existence of some transmission protocols, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans the transport layer and application layer.
RPC adopts client / server mode. The requester is a client, and the service provider is a server.

rpc working mechanism
The client program initiates an RPC system call and sends it to another host (server) based on TCP protocol
The server listens on a socket. After receiving the system call request from the client, it executes the received request and its passed parameters through the local system call, and returns the result to the local service process
After receiving the returned execution result, the service process of the server encapsulates it into a response message, and then returns it to the client through rpc Protocol
The client call process receives the reply information, gets the result of the process, and then calls the execution to proceed.

showmount command:
showmount 
		-a		//Displays all client hosts of the specified NFS server and the directories to which they are connected
		-d		//Displays all output directories connected by clients in the specified NFS server
		-e		//Displays the shared directory of all outputs on the specified NFS server

Instance requirements
Open the / nfs/shared directory for all users to consult materials
The open / nfs/upload directory is the data upload directory of the 192.168.149.0/24 network segment, and maps all users and their user groups to NFS upload, with both UID and GID of 300

Server (ip: 192.168.10.40)

Install and start nfs, turn off firewall and selinux

[root@C82 ~]# dnf install -y nfs-utils
[root@C82 ~]# systemctl start nfs-server
[root@C82 ~]# ss -antl
State  Recv-Q Send-Q   Local Address:Port    Peer Address:Port Process                                                        
LISTEN 0      64             0.0.0.0:2049         0.0.0.0:*                                                                   
LISTEN 0      64             0.0.0.0:44645        0.0.0.0:*                                                                   
LISTEN 0      128            0.0.0.0:111          0.0.0.0:*                                                                   
LISTEN 0      128            0.0.0.0:20048        0.0.0.0:*                                                                   
LISTEN 0      128            0.0.0.0:22           0.0.0.0:*                                                                   
LISTEN 0      128            0.0.0.0:54425        0.0.0.0:*                                                                   
LISTEN 0      64                [::]:2049            [::]:*                                                                   
LISTEN 0      128               [::]:111             [::]:*                                                                   
LISTEN 0      128               [::]:20048           [::]:*                                                                   
LISTEN 0      128               [::]:45973           [::]:*                                                                   
LISTEN 0      128               [::]:22              [::]:*                                                                   
LISTEN 0      64                [::]:41435           [::]:* 
[root@C82 ~]# systemctl stop firewalld
[root@C82 ~]# setenforce 0 

Edit the / etc/exports file

[root@C82 ~]# vim /etc/exports
[root@C82 ~]# cat /etc/exports
/nfs/shared     *(ro)
/nfs/upload     192.168.10.*(rw,anonuid=300,anongid=300)

Create mapped users, share directories, set permissions, and restart the nfs service

[root@C82 ~]# groupadd -r -g 300 nfs-upload
[root@C82 ~]# useradd -r -u 300 -g 300 nfs-upload
[root@C82 ~]# id nfs-upload
uid=300(nfs-upload) gid=300(nfs-upload) group=300(nfs-upload)
[root@C82 ~]# mkdir -p /nfs/upload
[root@C82 ~]# mkdir -p /nfs/shared
[root@C82 ~]# setfacl -m u:nfs-upload:rwx /nfs/upload/
[root@C82 ~]# setfacl -m g:nfs-upload:rwx /nfs/upload/
[root@C82 ~]# systemctl restart nfs-server
Client (ip: 192.168.10.20)

Install and start nfs, turn off firewall and selinux

[root@localhost ~]# dnf -y install nfs-utils
[root@localhost ~]# systemctl start nfs-utils
[root@localhost ~]# ss -antl
State  Recv-Q  Send-Q   Local Address:Port   Peer Address:Port Process                                                        
LISTEN 0       128            0.0.0.0:111         0.0.0.0:*                                                                   
LISTEN 0       128            0.0.0.0:22          0.0.0.0:*                                                                   
LISTEN 0       128               [::]:111            [::]:*                                                                   
LISTEN 0       128               [::]:22             [::]:*                                                                   
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

Manual mount

[root@localhost ~]# mount -t nfs 192.168.10.40:/nfs /media/
[root@localhost ~]# df -h
 file system             Capacity used available used% Mount point
devtmpfs             876M     0  876M    0% /dev
tmpfs                895M     0  895M    0% /dev/shm
tmpfs                895M   33M  863M    4% /run
tmpfs                895M     0  895M    0% /sys/fs/cgroup
/dev/mapper/cs-root   17G  1.7G   16G   10% /
/dev/nvme0n1p1      1014M  195M  820M   20% /boot
tmpfs                179M     0  179M    0% /run/user/0
192.168.10.40:/nfs    17G  1.8G   16G   11% /media

test
Server

[root@C82 ~]# cd /nfs/shared
[root@C82 shared]# ll
 Total consumption 0
[root@C82 shared]# touch ppp

client

[root@localhost ~]# cd /media/
[root@localhost media]# ls
shared  upload
[root@localhost media]# cd shared/
[root@localhost shared]# ls
ppp
[root@localhost shared]# rm -rf ppp
rm: Cannot delete'ppp': Read-only file system 

Server

[root@C82 shared]# cd ../upload/
[root@C82 upload]# ll
 Total consumption 0
[root@C82 upload]# touch qqq
[root@C82 upload]# ll
 Total consumption 0
-rw-r--r--. 1 root root 0 6 July 00:12 qqq

client

[root@localhost shared]# cd ../upload/
[root@localhost upload]# ll
 Total consumption 0
-rw-r--r--. 1 root root 0 6 July 00:12 qqq
[root@localhost upload]# rm -rf qqq
[root@localhost upload]# ll
 Total consumption 0

Topics: Linux CentOS nfs