当前位置: 首页 > 工具软件 > NFS > 使用案例 >

Linux安装NFS

阮昊阳
2023-12-01

Linux安装NFS

一、检测

检查本地是否已经安装nfs服务

rpm -qa | grep nfs-utils
rpm -qa | grep rpcbind

出现以下标识说明已经安装

[melonrind@melonrind ~]$ rpm -qa | grep rpcbind
rpcbind-0.2.0-49.el7.x86_64
[melonrind@melonrind ~]$ rpm -qa | grep nfs-utils
nfs-utils-1.3.0-0.66.el7_8.x86_64

二、安装

需要进行目录共享的两台服务器上均需要安装

1、在线安装

在线安装可以安装nfs-utils及其依赖

yum install nfs-utils -y

2、离线安装

需要先在可以联网的环境中下载nfs-utils及其依赖包

#该命令需要root权限
yum -y install nfs-utils --downloadonly --downloaddir /home/melonrind/nfspackages
#最终显示以下信息说明所有依赖包已经下载完成
依赖关系解决

===========================================================================================
 Package                   架构           版本                       源               大小
===========================================================================================
正在安装:
 nfs-utils                 x86_64         1:1.3.0-0.68.el7.2         updates         413 k
为依赖而安装:
 gssproxy                  x86_64         0.7.0-30.el7_9             updates         111 k
 keyutils                  x86_64         1.5.8-3.el7                base             54 k
 libbasicobjects           x86_64         0.1.1-32.el7               base             26 k
 libcollection             x86_64         0.7.0-32.el7               base             42 k
 libevent                  x86_64         2.0.21-4.el7               base            214 k
 libini_config             x86_64         1.3.1-32.el7               base             64 k
 libnfsidmap               x86_64         0.25-19.el7                base             50 k
 libpath_utils             x86_64         0.2.1-32.el7               base             28 k
 libref_array              x86_64         0.1.5-32.el7               base             27 k
 libtirpc                  x86_64         0.2.4-0.16.el7             base             89 k
 libverto-libevent         x86_64         0.2.5-4.el7                base            8.9 k
 quota                     x86_64         1:4.01-19.el7              base            179 k
 quota-nls                 noarch         1:4.01-19.el7              base             90 k
 rpcbind                   x86_64         0.2.0-49.el7               base             60 k
 tcp_wrappers              x86_64         7.6-77.el7                 base             78 k

事务概要
===========================================================================================
安装  1 软件包 (+15 依赖软件包)

总下载量:1.5 M
安装大小:4.3 M
Background downloading packages, then exiting:
(1/16): libbasicobjects-0.1.1-32.el7.x86_64.rpm                     |  26 kB  00:00:00
(2/16): gssproxy-0.7.0-30.el7_9.x86_64.rpm                          | 111 kB  00:00:00
(3/16): keyutils-1.5.8-3.el7.x86_64.rpm                             |  54 kB  00:00:00
(4/16): libcollection-0.7.0-32.el7.x86_64.rpm                       |  42 kB  00:00:00
(5/16): libpath_utils-0.2.1-32.el7.x86_64.rpm                       |  28 kB  00:00:00
(6/16): libtirpc-0.2.4-0.16.el7.x86_64.rpm                          |  89 kB  00:00:00
(7/16): libverto-libevent-0.2.5-4.el7.x86_64.rpm                    | 8.9 kB  00:00:00
(8/16): libini_config-1.3.1-32.el7.x86_64.rpm                       |  64 kB  00:00:00
(9/16): libnfsidmap-0.25-19.el7.x86_64.rpm                          |  50 kB  00:00:00
(10/16): nfs-utils-1.3.0-0.68.el7.2.x86_64.rpm                      | 413 kB  00:00:00
(11/16): quota-4.01-19.el7.x86_64.rpm                               | 179 kB  00:00:00
(12/16): tcp_wrappers-7.6-77.el7.x86_64.rpm                         |  78 kB  00:00:00
(13/16): libref_array-0.1.5-32.el7.x86_64.rpm                       |  27 kB  00:00:05
(14/16): rpcbind-0.2.0-49.el7.x86_64.rpm                            |  60 kB  00:00:05
(15/16): quota-nls-4.01-19.el7.noarch.rpm                           |  90 kB  00:00:05
(16/16): libevent-2.0.21-4.el7.x86_64.rpm                           | 214 kB  00:00:09
-------------------------------------------------------------------------------------------
总计                                                       156 kB/s | 1.5 MB  00:00:09
exiting because "Download Only" specified

一共16个rpm包

在当前下载目录进行安装

[root@melonrind nfspackages]# rpm -ivh *.rpm --force --nodeps
准备中...                          ################################# [100%]
正在升级/安装...
   1:libtirpc-0.2.4-0.16.el7          ################################# [  6%]
   2:rpcbind-0.2.0-49.el7             ################################# [ 13%]
   3:libref_array-0.1.5-32.el7        ################################# [ 19%]
   4:libevent-2.0.21-4.el7            ################################# [ 25%]
   5:libcollection-0.7.0-32.el7       ################################# [ 31%]
   6:libbasicobjects-0.1.1-32.el7     ################################# [ 38%]
   7:libverto-libevent-0.2.5-4.el7    ################################# [ 44%]
   8:tcp_wrappers-7.6-77.el7          ################################# [ 50%]
   9:quota-nls-1:4.01-19.el7          ################################# [ 56%]
  10:quota-1:4.01-19.el7              ################################# [ 63%]
  11:libpath_utils-0.2.1-32.el7       ################################# [ 69%]
  12:libini_config-1.3.1-32.el7       ################################# [ 75%]
  13:gssproxy-0.7.0-30.el7_9          ################################# [ 81%]
  14:libnfsidmap-0.25-19.el7          ################################# [ 88%]
  15:keyutils-1.5.8-3.el7             ################################# [ 94%]
  16:nfs-utils-1:1.3.0-0.68.el7.2     ################################# [100%]
[root@melonrind nfspackages]# rpm -qa|grep rpcbind
rpcbind-0.2.0-49.el7.x86_64
[root@melonrind nfspackages]# rpm -qa|grep nfs
libnfsidmap-0.25-19.el7.x86_64
nfs-utils-1.3.0-0.68.el7.2.x86_64

三、配置NFS(服务端)

在配置时,分为服务端和客户端。服务端即被共享的服务器,共享的文件真实存在在此服务器上;客户端即共享服务器,共享目录指向服务端目录。

1、创建共享目录(两台服务器)

[root@melonrind data]# mkdir share
[root@melonrind data]# cd share/
[root@melonrind share]# ll
总用量 0
[root@melonrind share]# pwd
/data/share
[root@melonrind share]#

2、服务端配置共享目录

[root@rocketmq-nameserver1 share]# vi /etc/exports

添加共享命令

/data/share/ 192.168.72.163(rw,sync,no_root_squash,no_all_squash)

参数说明:

/data/share/: 共享目录
192.168.72.163: 客户端IP
ro:共享目录只读;
rw:共享目录可读可写;
all_squash:所有访问用户都映射为匿名用户或用户组;
no_all_squash(默认):访问用户先与本机用户匹配,匹配失败后再映射为匿名用户或用户组;
root_squash(默认):将来访的root用户映射为匿名用户或用户组;
no_root_squash:来访的root用户保持root帐号权限;
anonuid=<UID>:指定匿名访问用户的本地用户UID,默认为nfsnobody(65534);
anongid=<GID>:指定匿名访问用户的本地用户组GID,默认为nfsnobody(65534);
secure(默认):限制客户端只能从小于1024的tcp/ip端口连接服务器;
insecure:允许客户端从大于1024的tcp/ip端口连接服务器;
sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
async:将数据先保存在内存缓冲区中,必要时才写入磁盘;
wdelay(默认):检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率;
no_wdelay:若有写操作则立即执行,应与sync配合使用;
subtree_check(默认) :若输出目录是一个子目录,则nfs服务器将检查其父目录的权限;
no_subtree_check :即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;

四、启动服务端

[root@rocketmq-nameserver1 share]# systemctl status rpcbind
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
   Active: inactive (dead)
[root@rocketmq-nameserver1 share]# systemctl enable rpcbind
[root@rocketmq-nameserver1 share]# systemctl start rpcbind
[root@rocketmq-nameserver1 share]# systemctl status rpcbind
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
   Active: active (running) since 五 2021-12-24 11:20:57 CST; 3s ago
  Process: 24325 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 24326 (rpcbind)
   CGroup: /system.slice/rpcbind.service
           └─24326 /sbin/rpcbind -w

12月 24 11:20:57 rocketmq-nameserver1 systemd[1]: Starting RPC bind service...
12月 24 11:20:57 rocketmq-nameserver1 systemd[1]: Started RPC bind service.
[root@rocketmq-nameserver1 share]# systemctl status nfs-server
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@rocketmq-nameserver1 share]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@rocketmq-nameserver1 share]# systemctl start nfs-server
[root@rocketmq-nameserver1 share]# systemctl status nfs-server
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
   Active: active (exited) since 五 2021-12-24 11:23:20 CST; 3s ago
  Process: 24724 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 24720 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 24724 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

12月 24 11:23:20 rocketmq-nameserver1 systemd[1]: Starting NFS server and services...
12月 24 11:23:20 rocketmq-nameserver1 systemd[1]: Started NFS server and services.
[root@rocketmq-nameserver1 share]#

启动后查看共享是否成功

[root@rocketmq-nameserver1 share]# showmount -e localhost
Export list for localhost:
/data/share 192.168.72.163

显示刚才配置的共享目录和IP说明共享生效

五、客户端配置

1、查看远程共享服务器是否共享成功

[root@melonrind share]# showmount -e 192.168.72.81
Export list for 192.168.72.81:
/data/share 192.168.72.163

这里查看时连接的是远程服务端IP

2、启动客户端服务

[root@melonrind share]# systemctl status rpcbind
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   Active: active (running) since 四 2021-09-30 09:14:18 CST; 2 months 24 days ago
  Process: 773 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 774 (rpcbind)
    Tasks: 1
   CGroup: /system.slice/rpcbind.service
           └─774 /sbin/rpcbind -w

9月 30 09:14:18 melonrind.ora systemd[1]: Starting RPC bind service...
9月 30 09:14:18 melonrind.ora systemd[1]: Started RPC bind service.
[root@melonrind share]# systemctl status nfs-server
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@melonrind share]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@melonrind share]# systemctl start nfs-server
[root@melonrind share]# systemctl status nfs-server
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
   Active: active (exited) since 五 2021-12-24 11:29:50 CST; 3s ago
  Process: 31978 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
  Process: 31961 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 31959 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 31961 (code=exited, status=0/SUCCESS)
    Tasks: 0
   CGroup: /system.slice/nfs-server.service

12月 24 11:29:50 melonrind.ora systemd[1]: Starting NFS server and services...
12月 24 11:29:50 melonrind.ora systemd[1]: Started NFS server and services.

3、挂载共享目录

mount -t nfs -o nolock 192.168.72.81:/data/share /data/share

说明:mount -t nfs -o nolock 服务端IP:共享目录绝对路径 本地挂载目录

4、查看挂载结果

[root@melonrind share]# df -Th
文件系统                  类型      容量  已用  可用 已用% 挂载点
devtmpfs                  devtmpfs  1.8G     0  1.8G    0% /dev
tmpfs                     tmpfs     1.8G  672M  1.2G   37% /dev/shm
tmpfs                     tmpfs     1.8G   91M  1.7G    5% /run
tmpfs                     tmpfs     1.8G     0  1.8G    0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs       100G   33G   68G   33% /
/dev/sda2                 xfs      1014M  226M  789M   23% /boot
/dev/sda1                 vfat     1022M   12M 1011M    2% /boot/efi
/dev/mapper/centos-var    xfs        10G  4.1G  6.0G   41% /var
/dev/mapper/centos-home   xfs       812G   98G  714G   12% /home
tmpfs                     tmpfs     366M  8.0K  366M    1% /run/user/42
tmpfs                     tmpfs     366M     0  366M    0% /run/user/1002
tmpfs                     tmpfs     366M     0  366M    0% /run/user/0
tmpfs                     tmpfs     366M     0  366M    0% /run/user/1000
192.168.72.81:/data/share nfs4      400G  239G  162G   60% /data/share

可以看到远程目录已经挂载

5、将挂载功能加入开机自启动

vi /etc/fstab
#在文件末尾添加一句命令
192.168.72.81:/data/share /data/share nfs defaults 0 0

六、测试共享

1、在服务端共享目录中创建一个文件test

[root@rocketmq-nameserver1 share]# ll
总用量 0
[root@rocketmq-nameserver1 share]# touch test
[root@rocketmq-nameserver1 share]# vi test
[root@rocketmq-nameserver1 share]# ll
总用量 4
-rw-r--r-- 1 root root 7 12月 24 11:35 test
[root@rocketmq-nameserver1 share]# pwd
/data/share

2、再去客户端共享目录中查看

[root@melonrind share]# cd /data/share/
[root@melonrind share]# ll
总用量 0
[root@melonrind share]# ll
总用量 4
-rw-r--r-- 1 root root 7 12月 24 11:35 test
[root@melonrind share]# pwd
/data/share

已经可以看到共享的文件了

 类似资料: