Linux教程

linux NFS共享服务

本文主要是介绍linux NFS共享服务,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

NFS共享存储服务

服务端部署

安装NFS工具包spcbind服务
[root@nfs-31 ~]#yum install nfs-utils spcbind -y


检查rpcbind服务是否运行
[root@nfs-31 ~]#systemctl status rpcbind
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
[root@nfs-31 ~]#netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      964/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1401/master         
tcp6       0      0 :::22                   :::*                    LISTEN      964/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1401/master         



启动rpcbind服务,检查运行端口
[root@nfs-31 ~]#systemctl start rpcbind
[root@nfs-31 ~]#netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1754/rpcbind        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      964/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1401/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1754/rpcbind        
tcp6       0      0 :::22                   :::*                    LISTEN      964/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1401/master         
udp        0      0 0.0.0.0:657             0.0.0.0:*                           1754/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           1754/rpcbind        
udp6       0      0 :::657                  :::*                                1754/rpcbind        
udp6       0      0 :::111                  :::*                                1754/rpcbind        
[root@nfs-31 ~]#netstat -tunlp |grep rpc
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1754/rpcbind        
tcp6       0      0 :::111                  :::*                    LISTEN      1754/rpcbind        
udp        0      0 0.0.0.0:657             0.0.0.0:*                           1754/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           1754/rpcbind        
udp6       0      0 :::657                  :::*                                1754/rpcbind        
udp6       0      0 :::111                  :::*                                1754/rpcbind        

修改配置文件

一个目录为读写,一个目录为只读
[root@nfs-31 /etc]#vim exports
[root@nfs-31 /]#cat /etc/exports
/ops_data 172.16.1.0/24(rw,sync,all_squash)
/dev_data 172.16.1.0/24(ro,sync,all_squash)



创建,共享存储目录,以及修改文件的属主,属组
[root@nfs-31 /]#mkdir /ops_data
[root@nfs-31 /]#mkdir /dev_data
[root@nfs-31 /]#chown -R nfsnobody:nfsnobody /ops_data/
[root@nfs-31 /]#chown -R nfsnobody:nfsnobody /dev_data/


启动nfs服务,检查运行状态
[root@nfs-31 /]#systemctl start nfs
[root@nfs-31 /]#systemctl status  nfs
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: active (exited) since Fri 2022-04-22 11:26:57 CST; 11s ago
  Process: 2176 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
  Process: 2159 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 2158 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 2159 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

Apr 22 11:26:57 nfs-31 systemd[1]: Starting NFS server and services...
Apr 22 11:26:57 nfs-31 systemd[1]: Started NFS server and services.
[root@nfs-31 /]#ps -ef |grep nfs
root       2162      2  0 11:26 ?        00:00:00 [nfsd4_callbacks]
root       2168      2  0 11:26 ?        00:00:00 [nfsd]
root       2169      2  0 11:26 ?        00:00:00 [nfsd]
root       2170      2  0 11:26 ?        00:00:00 [nfsd]
root       2171      2  0 11:26 ?        00:00:00 [nfsd]
root       2172      2  0 11:26 ?        00:00:00 [nfsd]
root       2173      2  0 11:26 ?        00:00:00 [nfsd]
root       2174      2  0 11:26 ?        00:00:00 [nfsd]
root       2175      2  0 11:26 ?        00:00:00 [nfsd]
root       2190   1329  0 11:27 pts/0    00:00:00 grep --color=auto nfs




检查nfs服务端的共享情况,使用showmount命令查看
[root@nfs-31 /]#showmount -e 172.16.1.31
Export list for 172.16.1.31:
/dev_data 172.16.1.0/24
/ops_data 172.16.1.0/24




查看nfs服务端远程共享的所有参数,是系统自动生成的,以及我们配置文件里定义的,都是默认的
[root@nfs-31 /]#cat /var/lib/nfs/etab 
/dev_data	172.16.1.0/24(ro,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,ro,secure,root_squash,all_squash)
/ops_data	172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)

注意:

/etc/exports文件的语法不要写错,细心
修改/etc/exports文件后,注意要重启systemctl reload nfs或是exportfs -r重新加载配置,无需重启

客户端部署

一共两台机器

rsync-41和web-7

rsync-41部署

安装nfs工具包
[root@rsync-41 ~]#yum install nfs-utils -y


运行客户端的rpcbind程序
[root@rsync-41 ~]#systemctl start rpcbind

远程查看nfs服务器信息
[root@rsync-41 ~]#showmount -e 172.16.1.31
Export list for 172.16.1.31:
/dev_data 172.16.1.0/24
/ops_data 172.16.1.0/24


创建用于挂载的文件夹
[root@rsync-41 ~]#mkdir -p /data

挂载到可读写的目录
[root@rsync-41 ~]#mount -t nfs 172.16.1.31:/ops_data  /data

查看挂载情况
[root@rsync-41 ~]#df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.6G   16G  10% /
devtmpfs                 899M     0  899M   0% /dev
tmpfs                    911M     0  911M   0% /dev/shm
tmpfs                    911M  9.5M  902M   2% /run
tmpfs                    911M     0  911M   0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M  14% /boot
tmpfs                    183M     0  183M   0% /run/user/0
172.16.1.31:/ops_data     17G  1.6G   16G  10% /data

创建目录和文件,都可以执行
[root@rsync-41 ~]#cd /data/
[root@rsync-41 /data]#ls 
[root@rsync-41 /data]#mkdir 666啊
[root@rsync-41 /data]#touch 牛啊牛啊
[root@rsync-41 /data]#ls 
666啊  牛啊牛啊


服务端查看如下

web-7客户端部署

安装nfs工具包
[root@web-7 ~]#yum install nfs-utils -y


运行客户端的rpcbind程序
[root@web-7 ~]#systemctl start rpcbind


远程查看nfs服务器信息
[root@web-7 ~]#showmount -e 172.16.1.31
Export list for 172.16.1.31:
/dev_data 172.16.1.0/24
/ops_data 172.16.1.0/24


创建用于挂载的文件夹
[root@web-7 ~]#mkdir /data


挂载到只读的目录
[root@web-7 ~]#mount -t nfs 172.16.1.31:/dev_data  /data


查看挂载情况
[root@web-7 ~]#df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.6G   16G   9% /
devtmpfs                 899M     0  899M   0% /dev
tmpfs                    911M     0  911M   0% /dev/shm
tmpfs                    911M  9.5M  902M   2% /run
tmpfs                    911M     0  911M   0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M  14% /boot
tmpfs                    183M     0  183M   0% /run/user/0
172.16.1.31:/dev_data     17G  1.6G   16G  10% /data



创建目录和文件都没有权限,因为是只读的
[root@web-7 /data]#mkdir 666啊
mkdir: cannot create directory ‘666啊’: Read-only file system
[root@web-7 /data]#touch 牛啊牛啊
touch: cannot touch ‘牛啊牛啊’: Read-only file system

ls查看什么都没有
[root@web-7 /data]#ls 
[root@web-7 /data]#



服务端查看该文件夹

这篇关于linux NFS共享服务的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!