NFS(网络文件系统)服务可以将远程linux系统上的文件共享资源挂载到本地的主机目录上,从而使得本地主机(linux客户端)基于TCP/IP协议,像使用本地主机上的资源读写远程linux系统上的共享文件。
RHEL7系统默认是安装了NFS服务,NFS服务配置起来很简单,先使用yum软件仓库来检查RHEL7系统当中是否安装了NFS软件包。
[root@server0 ~]# yum install nfs-utils
Loaded plugins: langpacks
rhel_dvd | 4.1 kB 00:00:00
(1/2): rhel_dvd/group_gz | 134 kB 00:00:00
(2/2): rhel_dvd/primary_db | 3.4 MB 00:00:01
Package 1:nfs-utils-1.3.0-0.el7.x86_64 already installed and latest version
Nothing to do
实验环境,两台linux主机,如下
NFS服务器 操作系统 NFS客户端 操作系统
172.25.0.11(server0) RHEL7 172.25.0.10(desktop0) RHEL7
服务端配置
(1)清空NFS服务器上iptables的防火墙的默认策略,以免默认的防火墙策略禁止正常的NFS服务,同时关闭firewall。
[root@server0 ~]# iptables -F
[root@server0 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@server0 ~]# systemctl stop firewalld.service
(2)在NFS服务器上建立用于NFS文件共享的目录,并设置足够的权限确保其他人也有写入权限。
[root@server0 ~]# chmod -Rf 777 /nfsfile/
-R : 对目前目录下的所有文件与子目录进行相同的权限变更(即以递回的方式逐个变更)
-f : 若该文件权限无法被更改也不要显示错误讯息
[root@server0 ~]# touch /nfsfile/readme
[root@server0 nfsfile]# echo "hellow world" > /nfsfile/readme
[root@server0 nfsfile]# cat readme
hellow world
(3)在NFS服务程序配置文件为/etc/exports,默认情况下里面没有任何内容。格式是:共享目录的路径 允许访问的NFS客户端(共享权限参数)的格式,定义共享目录相应的权限。
将/nfsfile目录共享给172.25.0.0/24网段内所有主机,这些主机都拥有读写权限,在数据写入到NFS服务器的硬盘后才会结束操作,最大限度的使得数据不会丢失,以及把来访客户端的ROOT管理员映射为本地的匿名用户。下面是需要使用到的参数:
ro 只读
rw 只写
root_squash 当NFS客户端以root管理员身份访问时,映射为NFS服务器的匿名用户。
no_root_squash 当NFS客户端以root管理员身份访问时,映射为NFS服务器root管理员。
all_squash 无论NFS客户端使用什么账号访问,均映射为NFS服务器的匿名用户。
sync 同时将数据写入到内存与硬盘当中,保证数据不丢失。
async 先将数据保存到内存,然后再写入磁盘;这样效率更加高,但是可能会丢失数据。
[root@server0 nfsfile]# vi /etc/exports
[root@server0 nfsfile]# cat /etc/exports
/nfsfile 172.25.0.*(rw,sync,root_squash)
注意,NFS客户端和地址权限之间没有空格。
(4)启动和启用NFS服务程序,由于使用NFS服务进行文件共享之前,需要使用RPC(remote procedure call,远程过程调用)服务将NFS服务器IP地址和端口信息发送给客户端,在启动NFS服务之前,还需要重启并且启用rpcbind服务程序,并将两个服务加入到开机启动项当中。
[root@server0 nfsfile]# systemctl enable rpcbind
[root@server0 nfsfile]# systemctl enable nfs-server
启动服务:注意先启动rpcbind再启动nfs
[root@server0 nfsfile]# systemctl restart rpcbind
[root@server0 nfsfile]# systemctl restart nfs-server
客户端配置
NFS客户端配置比较简单,先试用showmount命令查询NFS服务器的远程共享信息,其输出格式为“共享的目录名称 允许使用客户端地址”
showmount参数以及作用:
-e显示NFS服务器列表
-a 显示本机挂载的文件资源情况
-v 显示版本号
(1)在客户端查看NFS服务器端的挂载信息
[root@desktop0 ~]# showmount -e 172.25.0.11
Export list for 172.25.0.11:
/nfsfile 172.25.0.*
(2)在客户端创建一个挂载目录,使用mount命令结合-t参数,指定要挂载的文件系统类型的类型,并在命令后面写上服务器IP地址,服务器上共享目录以及挂载到本地系统,即客户端的目录。
[root@desktop0 ~]# mount -t nfs 172.25.0.11:/nfsfile /nfsfile
[root@desktop0 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.0G 7.0G 31% /
devtmpfs 906M 0 906M 0% /dev
tmpfs 921M 80K 921M 1% /dev/shm
tmpfs 921M 17M 904M 2% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
172.25.0.11:/nfsfile 10G 3.0G 7.0G 31% /nfsfile
[root@desktop0 ~]# cat /nfsfile/readme
hellow world
(3)如果需要永久挂载记得写到fstab文件里面
[root@desktop0 ~]# cat /etc/fstab
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
172.25.0.11:/nfsfile /nfsfile nfs defaults 0 0
[root@desktop0 ~]# mount -a
[root@desktop0 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.0G 7.0G 31% /
devtmpfs 906M 0 906M 0% /dev
tmpfs 921M 80K 921M 1% /dev/shm
tmpfs 921M 17M 904M 2% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
172.25.0.11:/nfsfile 10G 3.0G 7.0G 31% /nfsfile
这里最后要注意,在客户端对服务端的就行操作的时候都是使用匿名用户。(因为在服务端/etc/exports下面的参数root_squash)
[root@desktop0 ~]# touch /nfsfile/text.txt
[root@desktop0 ~]# ls -l /nfsfile/
total 4
-rwxrwxrwx. 1 root root 13 Jun 19 02:10 readme
-rw-r--r--. 1 nfsnobody nfsnobody 0 Jun 19 03:24 text.txt
[root@server0 ~]# cat /var/lib/nfs/etab
/nfsfile 172.25.0.10(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,no_all_squash)
--在客户端使用root用户对挂载目录进行操作,默认情况是实验65534用户进行登录操作的,查看的权限就是other用户的权限(root_squash 当NFS客户端以root管理员身份访问时,映射为NFS服务器的匿名用户)
如果出现下面情况就要考虑在服务端目录权限问题,针对的是other用户。
[root@desktop0 point]# touch a.txt
touch: cannot touch ?..txt?. Permission denied
解决办法就是
这个时候在服务端
[root@server0 ~]# chmod o+w /share/
在客户端
[root@desktop0 ~]# touch /point/a.txt
[root@desktop0 ~]# ls -l /point/
total 0
-rw-r--r--. 1 root root 0 Apr 5 20:08 1.txt
-rw-r--r--. 1 nfsnobody nfsnobody 0 Apr 5 20:17 a.txt