1. what is nfs used for?

nfs is network file system, it is used when multiple computers need to access one directory.

2. Computer Environment

OS: Ubuntu 20.04

3. Install and config nfs server

3.1. Install nfs server

#执行以下命令安装NFS服务器,
​#apt会自动安装nfs-common、rpcbind等13个软件包
​sudo apt install nfs-kernel-server

3.2 create directory to share

# create directory /home/maye/nfs  to share on nfs server host
# Attention: It's better to create directory to share under directory home, not under root directory, 
# because It may have error: permission not allowed to read or write the directory, 
# if under root directory.
sudo mkdir -p /home/maye/nfs
​

3.3 write config file to set exported directories.

sudo gedit /etc/exports

-->
#设置共享目录
/home/maye/nfs *(rw,sync,no_subtree_check,no_root_squash)

Note:
* means any ip can mount this exported directory.

3.4 start nfs service

# before starting nfs serivce, rpcbind service needs to be running (namely active).
# check status of rpcbind service
$ systemctl status rpcbind
# if rpcbind service not active, start it.
$ systemctl start rpcbind  
$ systemctl start nfs-kernel-server
(base) maye@maye-Inspiron-5547:~/github_repository/tensorflow_ecosystem/distribution_strategy$ systemctl status nfs-kernel-server
● nfs-server.service - NFS server and services
     Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor >
    Drop-In: /run/systemd/generator/nfs-server.service.d
             └─order-with-mounts.conf
     Active: active (exited) since Fri 2024-02-09 15:09:21 CST; 2 days ago
   Main PID: 947 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 14115)
     Memory: 0B
        CPU: 0
     CGroup: /system.slice/nfs-server.service

2月 09 15:09:16 maye-Inspiron-5547 systemd[1]: Starting NFS server and servic>
2月 09 15:09:21 maye-Inspiron-5547 systemd[1]: Finished NFS server and servic>
lines 1-13/13 (END)

Note:

  1. service status "active (exited)" means that the service has finished what it needs to do, and exited, not that the service is not ok.
# see the export list for localhost
$ showmount -e

-->
Export list for maye-Inspiron-5547:
/home/maye/nfs *

4. Install and config nfs client

4.1 install nfs client

#在需要连接到NFS服务器的客户端机器上,
#需要执行以下命令,安装nfs-common软件包。
#apt会自动安装nfs-common、rpcbind等12个软件包
sudo apt install nfs-common

#显示指定的NFS服务器上export出来的目录
sudo showmount -e nfs-server-ip

4.2 create directory which the nfs server exported directory will mount to.

sudo mkdir -p /home/maye/mnt/nfs

4.3 mount shared directory

# on nfs client
# /home/maye/nfs is the directory exported on nfs server in this example, 
# /home/maye/mnt/nfs is the directory on nfs client to which 
# the shared directory is mounted to in this example,
# change them to the actual paths in your case.
sudo mount -t nfs nfs-server-ip:/home/maye/nfs /home/maye/mnt/nfs 

# check the mounted directory
$ sudo df -h

-->
nfs-server-ip:/home/maye/nfs 457G 182G 253G 42% /home/maye/mnt/nfs

5. stop nfs service

# on nfs server
$ sudo systemctl stop nfs-kernel-server

Note:

  1. after mounting a nfs shared directory on nfs client, the mount will always exist even if nfs server is powered off, until umount it. So when nfs server is powered on, and nfs service is started again, the mounted path on nfs client can be accessed again, no need to mount it again.

References:
https://zhuanlan.zhihu.com/p/606174368?utm_id=0