Linux下安装使用nfs

# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core)
# uname -r
3.10.0-862.el7.x86_64
# setenforce 0
# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
# firewall-cmd --zone=public --permanent --add-service=nfs
# firewall-cmd --complete-reload
# yum -y install nfs-utils
# rpm -aq|grep nfs-utils
nfs-utils-1.3.0-0.54.el7.x86_64
# mkdir /nfs_dir
# vim /nfs_dir/welcome
  This is NFS test file.
//NFS相关配置 

//相关配置文件:
//配置文件:/etc/exports
//配置文件目录:/etc/exports.d
//NFS服务的主配置文件为/etc/exports,用于定义共享的目录以及客户端的权限,格式如下:
/PATH/TO/SOME_DIR clients1(export_options,...) clients2(export_options,...)

//其中clients支持以下几种格式:
//single host:ipv4,ipv6,FQDN
//network:address/netmask
//wildcards:主机名通配,例如,*.magedu.com
//netgroups:NIS域内的主机组,@group_name
//anonymous:使用*通配所有主机
  //export_options的常见参数可以分为以下两类:
//General Options:
    //ro:客户端挂载后,其权限为只读,默认选项;
    //rw:读写权限;
    //sync:同时将数据写入到内存与硬盘中;
    //async:异步,优先将数据保存到内存,然后再写入硬盘;
    //Secure:要求请求源的端口小于1024
//User ID Mapping:
    //root_squash:当NFS客户端使用root用户访问时,映射到NFS服务器的匿名用户;
    //no_root_squash:当NFS客户端使用root用户访问时,映射到NFS服务器的root用户;
    //all_squash:全部用户都映射为服务器端的匿名用户;
    //anonuid=UID:将客户端登录用户映射为此处指定的用户uid;
    //anongid=GID:将客户端登录用户映射为此处指定的用户gid
//更多参数信息可以通过命令 man exports 查看帮助手册

//配置NFS
# vim /etc/exports
/nfs_dir 192.168.4.*(rw,sync,root_squash)
# systemctl start nfs-server

//客户端测试
//查看nfs能挂载的选项
# showmount -e 192.168.4.119
Export list for 192.168.4.119:
/nfs_dir 192.168.4.*
# mount -t nfs 192.168.4.119:/nfs_dir /mnt
# cd /mnt/
# ls
welcome
# cat welcome 
This is NFS test file.
# touch file
touch: cannot touch ‘file’: Permission denied

//可以看到无法在共享目录下创建文件,明明已经给分配了rw权限,这是因为root_squash把我们的访问权限压缩为nobody权限,自然无法对该目录进行写入操作。
//对NFS的配置文件重新进行修改:

# vim /etc/exports
/nfs_dir 192.168.4.*(rw,sync,no_root_squash)
//使用exportfs重读NFS配置,不需要重启服务
# exportfs -rv
exporting 192.168.4.*:/nfs_dir

//客户端重新测试:
# touch file
# ll
total 4
-rw-r--r--. 1 root root  0 Jun 28 14:11 file  //可以看到属主属组为root
-rw-r--r--. 1 root root 23 Jun 28 11:08 welcome
# echo 123 > file
# rm file
rm: remove regular empty file ‘file’? y  //可以删除文件
# ll
total 4
-rw-r--r--. 1 root root 23 Jun 28 11:08 welcome

//很明显,将客户端访问共享文件用户映射为NFS服务器上的root是一种不安全的做法,我们可以指定客户端映射到NFS服务器的用户,配置如下:

# useradd nfsuser -s /sbin/nologin
# id nfsuser
uid=1003(nfsuser) gid=1003(nfsuser) groups=1003(nfsuser)
# chown -R nfsuser:nfsuser /nfs_dir/
# vim /etc/exports
# cat /etc/exports
/nfs_dir 192.168.4.*(rw,sync,secure,all_squash,anonuid=1003,anongid=1003)
# exportfs -rv
exporting 192.168.4.*:/nfs_dir
# systemctl restart nfs

//客户端进行测试:
# touch file
# ll
total 4
-rw-r--r--. 1 1003 1003  0 Jun 28 14:27 file
-rw-r--r--. 1 1003 1003 23 Jun 28 11:08 welcome
# echo 123 > file
# cat welcome 
This is NFS test file.
# rm file
rm: remove regular file ‘file’? y
# ll
total 4
-rw-r--r--. 1 1003 1003 23 Jun 28 11:08 welcome

猜你喜欢

转载自blog.csdn.net/qq_35590198/article/details/80855348