NFS
NFS是NetworkFileSystem的简写,即网络文件系统,网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS.NFS允许一个系统在网络上与它人共享目录和文件。通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件。
1.nfs
net file systemctl
nfs-untils
2.nfs的启用
在服务端
yum insatll nfs-utils -y
systemctl start nfs-server
systemctl enable nfs-server
systemctl stop firewalld
3.配置共享
mkdir /nfsdir
vim /etc/exports
/nfsdir *(sync)
本机目录 共享的客户端ip(*:所有) (共享方式)
exportfs -rv ##r刷新v显示
[root@localhost ~]# exportfs -r
[root@localhost ~]# exportfs -v
/nfsdir <world>(ro,wdelay,root_squash,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash) ##默认的设置
测试
在客户端
showmount -e 172.25.254.102 ##showmount命令用于查询NFS服务器的相关信息
Export list for 172.25.254.102:
/nfsdir *
在服务端
touch /nfsdir/file{1..5}
在客户端
mount 172.25.254.102:/nfsdir /mnt
root@localhost ~]# cd /mnt
[root@localhost mnt]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 26 20:25 file1
-rw-r--r-- 1 root root 0 Feb 26 20:25 file2
-rw-r--r-- 1 root root 0 Feb 26 20:25 file3
-rw-r--r-- 1 root root 0 Feb 26 20:25 file4
-rw-r--r-- 1 root root 0 Feb 26 20:25 file5
在服务端
配置共享文件
vim /etc/exports
/nfsdir 172.25.254.0/24(sync) 172.25.254.202(rw,anonuid=1001,anongid=1001)
##共享文件的路径 ##共享的网段 ##共享的方式(同时写入硬盘) 给某个客户端的权限(ro可读,rw可写,指定所写的都为1001用户)
exportfs -rv ##刷新显示
exportfs ##读取/etc/exports中的信息,并将有关的directories(或filesystems)分享给NFS clients.
/nfsdir 172.25.254.202
/nfsdir 172.25.254.0/24
chmod 777 /nfsdir/ ##给客户端对这个文件的所有权限
测试
在客户端
mount 172.25.254.102:/nfsdir /mnt ##挂载过不用挂载
[root@localhost ~]# cd /mnt
[root@localhost mnt]# touch lpl ##指定的客户端(在服务端没给 chmod 777 /nfsdir/ 时)
touch: cannot touch ‘lpl’: Permission denied
[root@localhost mnt]# touch lpl
touch: cannot touch ‘lpl’: Read-only file system ##未指定的客户端
[root@localhost mnt]# touch lpl ##即是指定客户端 又在服务端给过文件权限的
[root@localhost mnt]# ll
-rw-r--r-- 1 1001 1001 0 Feb 27 22:45 lpl
4.在客户端的自动挂载与自动卸载
yum insatll autofs
[root@localhost ~]# ll /net ##在未开启autofs服务时
ls: cannot access /net: No such file or directory
[root@localhost ~]# systemctl start autofs
[root@localhost ~]# systemctl enable autofs
[root@localhost ~]# ls -ld /net
drwxr-xr-x 2 root root 0 Feb 26 21:20 /net
[root@localhost ~]# umount /mnt
[root@localhost ~]# cd /net/172.25.254.102
[root@localhost 172.25.254.102]# ls
nfsdir
[root@localhost ~]# df
172.25.254.102:/nfsdir 10473984 3192832 7281152 31% /net/172.25.254.102/nfsdir
[root@localhost ~]# cd /net/172.25.254.102/nfsdir/ ##当你用的时候自动挂载
自动卸载
vim /etc/sysconfig/autofs
TIMEOUT=3 ##自动卸载时间
[root@localhost nfsdir]# cd ##cd出来后等3s就会自动卸载(在TIMEOUT时间内df会重置卸载时间)
自定义挂载点
vim /etc/auto.master
/misc /etc/auto.misc
/mnt /etc/auto.nfs ##自己指定的文件 (配置文件的子文件;/mnt 挂载点的上个目录)
vim /etc/auto.nfs ##编辑
westos -ro,vers=3.0 172.25.254.102:/nfsdir
##挂载点 ##挂载参数 ##挂载内容
[root@localhost ~]# cd /mnt/westos ##cd到挂载点,自动挂载
[root@localhost ~]# df
172.25.254.102:/nfsdir 10473984 3192832 7281152 31% /mnt/westos
[root@localhost westos]# cd ##cd出去 等3s自动卸载
exportfs命令:
功能说明 :NFS共享管理
server上执行此命令时,读取/etc/exports中的信息,并将有关的directories(或filesystems)分享给NFS clients.
语法格式
exportfs [必要参数][选择参数][目录]
功能描述
exportfs 命令:用于管理NFS(Network File System)文件系统,在不直接编辑/etc/exports文件的情况下,可用exportfs来操作 命令属性 磁盘管理 命令属权限 超级用户 普通用户
-a 递增式更新,对/etc/exports 增加或修改的部分进行挂载和卸载
-i<文件> 指定配置文件
-r 更新配置,重新读取/etc/exports
-u 卸载指定目录
-o 使用指定参数
-v 显示共享详细情况
【NFS共享的常用参数】
ro 只读访问
rw 读写访问
sync 所有数据在请求时写入共享
async NFS在写入数据前可以相应请求
secure NFS通过1024以下的安全TCP/IP端口发送
insecure NFS通过1024以上的端口发送
wdelay 如果多个用户要写入NFS目录,则归组写入(默认)
no_wdelay 如果多个用户要写入NFS目录,则立即写入,当使用async时,无需此设置。
hide 在NFS共享目录中不共享其子目录
no_hide 共享NFS目录的子目录
subtree_check 如果共享/usr/bin之类的子目录时,强制NFS检查父目录的权限(默认)
no_subtree_check 和上面相对,不检查父目录权限
all_squash 共享文件的UID和GID映射匿名用户anonymous,适合公用目录。
no_all_squash 保留共享文件的UID和GID(默认)
root_squash root用户的所有请求映射成如anonymous用户一样的权限(默认)
no_root_squash root用户具有根目录的完全管理访问权限
anonuid=xxx 指定NFS服务器/etc/passwd文件中匿名用户的UID
anongid=xxx 指定NFS服务器/etc/passwd文件中匿名用户的GID