Linux网络服务_nfs文件系统
nfs文件系统
1.nfs简单介绍
NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。
nfs适用于类linux操作系统
cifs适用于windows环境
软件nfs-utils
查看帮助man 5 exports
2.nfs的启用
系统本身就有不用安装
在server服务器
yum install nfs-utils.x86_64 -y
systemctl start nfs-server
systemctl enable nfs-server
systemctl stop firewalld
测试,在desktop客户端
showmount -e 172.25.254.103 //出现下一行则说明安装并运行成功
//显示NFS服务器上所有的共享目录。
export list for 172.25.254.103: //共享目录的列表
3.配置共享
server下
vim /etc/exports
本集目录 共享客户端(共享方式)
/nfsdir *(sync) //sync:实时同步
exportfs -rv 同步共享
【实验】
[[email protected] ~]# yum list nfs-utils //查看是否安装,installed为已安装
[[email protected] ~]# yum list nfs-utils
[[email protected] ~]# showmount -e 172.25.254.103 //服务器未开启服务
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
开启服务
[[email protected] ~]# systemctl status nfs //查看状态
Active: inactive (dead)
[[email protected] ~]# systemctl start nfs-server
[[email protected] ~]# systemctl status nfs //已开启
Active: active (exited)
[[email protected] ~]# systemctl stop firewalld //关闭火墙
[[email protected] ~]# systemctl enable nfs-server //开机自动启动
[[email protected] ~]# showmount -e 172.25.254.103 //客户端访问成功
Export list for 172.25.254.103:
实时共享
[[email protected] ~]# mkdir /nfsdir //服务器端创建共享目录
[[email protected] ~]# ls -ld /nfsdir/ //查看共享目录
drwxr-xr-x. 2 root root 6 Feb 26 20:22 /nfsdir/
[[email protected] ~]# vim /etc/exports //创建共享脚本文件,指向目录并共享
1 /nfsdir *(sync)
[[email protected] ~]# exportfs -rv //实时同步共享
exporting *:/nfsdir
[[email protected] ~]# showmount -e 172.25.254.103 //客户端查看共享目录菜单
Export list for 172.25.254.103:
/nfsdir * //创建的共享目录成功
[[email protected] ~]# mount 172.25.254.103:/nfsdir /mnt/ //客户端以挂载的方式登入共享目录并可操作
[[email protected] ~]# df //查看挂载
172.25.254.103:/nfsdir 10473984 3160576 7313408 31% /mnt
[[email protected] ~]# touch /nfsdir/file1 //服务器创建文件以验证
[[email protected] ~]# ls /mnt/ //客户端查看
file1
[[email protected] ~]# touch /mnt/file2 //此时/mnt为只读
touch: cannot touch ‘/mnt/file2’: Read-only file system
4.更改用户身份
查看帮助man 5 exports
[[email protected] ~]# vim /etc/exports //创建共享脚本文件,指向目录并共享
1 /nfsdir *(sync)
此处的*可以改为网关模式
172.25.254.0/24 其他的写法不可
2 /nfsdir 172.25.254.203(rw,anonuid=1001,anongid=1001,no_root_squash)
rw读写,ro只读 uid,gid标示创建文件的用户身份
no_root_squash出现,是谁创建的文件,用户和组就是谁,前面的uid,gid失效
[[email protected] ~]# exportfs -rv //实时同步
客户端创建文件验证
【实验】
更改读写权限
[[email protected] ~]# vim /etc/exports
/nfsdir 172.25.254.0/24(sync)
/nfsdir 172.25.254.203(rw)
[[email protected] ~]# exportfs -rv
[[email protected] ~]# touch /mnt/file2 //此时为权限不足
touch: cannot touch ‘/mnt/file2’: Permission denied
[[email protected] ~]# chmod 777 /nfsdir/
[[email protected] ~]# touch /mnt/file2
[[email protected] ~]# ll /mnt/
total 0
-rw-r–r– 1 root root 0 Feb 26 20:24 file1
-rw-r–r– 1 nfsnobody nfsnobody 0 Feb 26 20:50 file2
!!!!此时用户身份时nfsnobody
更改身份
[[email protected] ~]# id 1001
uid=1001(westos) gid=1001(westos) groups=1001(westos),1002(lee)
[[email protected] ~]# vim /etc/exports
/nfsdir 172.25.254.0/24(sync)
/nfsdir 172.25.254.203(rw,anonuid=1001)
[[email protected] ~]# exportfs -rv
[[email protected] ~]# touch /mnt/file3
-rw-r–r– 1 nfsnobody nfsnobody 0 Feb 26 20:50 file2
-rw-r–r– 1 1001 nfsnobody 0 Feb 26 20:53 file3
[[email protected] ~]# vim /etc/exports
/nfsdir 172.25.254.203(rw,anonuid=1001,anongid=1002)
[[email protected] ~]# id 1002
uid=1002(lee) gid=1002(lee) groups=1002(lee),1001(westos)
[[email protected] ~]# exportfs -rv
[[email protected] ~]# touch /mnt/file4
[[email protected] ~]# ll /mnt/
-rw-r–r– 1 1001 nfsnobody 0 Feb 26 20:53 file3
-rw-r–r– 1 1001 1002 0 Feb 26 20:54 file4
[[email protected] ~]# vim /etc/exports
/nfsdir 172.25.254.203(rw,anonuid=1001,anongid=1002,no_root_squash)
[[email protected] ~]# exportfs -rv
[[email protected] ~]# touch /mnt/file5
-rw-r–r– 1 root root 0 Feb 26 20:56 file5
[[email protected] ~]# su - student
[[email protected] ~]$ touch /mnt/file6
[[email protected] ~]$ ll /mnt/
-rw-rw-r– 1 student student 0 Feb 26 20:57 file6
[[email protected] ~]$ exit
[[email protected] ~]# useradd westos
[[email protected] ~]# su - westos
[[email protected] ~]$ touch /mnt/file7
[[email protected] ~]$ ll /mnt/
-rw-rw-r– 1 westos westos 0 Feb 26 20:58 file7
[[email protected] ~]# ll /mnt/
total 0
-rw-r–r– 1 root root 0 Feb 26 20:24 file1
-rw-r–r– 1 nfsnobody nfsnobody 0 Feb 26 20:50 file2
-rw-r–r– 1 westos nfsnobody 0 Feb 26 20:53 file3
-rw-r–r– 1 westos 1002 0 Feb 26 20:54 file4
-rw-r–r– 1 root root 0 Feb 26 20:56 file5
-rw-rw-r– 1 student student 0 Feb 26 20:57 file6
-rw-rw-r– 1 westos westos 0 Feb 26 20:58 file7
!!!!!!!!!!!!!!!
1.注意,若在服务器用su - 切换
若 服务器中1002 为 lee 并切换至lee在/mnt下创建文件file8
-rw-rw-r– 1 lee lee 0 Feb 26 20:58 file7
在客户端中,id1002为penguin,则用ll /mnt查看时file8,用户和用户组显示为penguin
-rw-rw-r– 1 penguin penguin 0 Feb 26 20:58 file7
2.若不加no_root_squash切换到sudent创建,用户和用户组依旧时student
5.用的时候自动挂载,不用的时候自动卸载
在客户端中操作
umount /mnt/
yum install autofs -y
systemctl start autofs
systemctl enable autofs.service
【实验】
安装软件
[[email protected] ~]# umount /mnt/
[[email protected] ~]# yum install autofs -y
Installed:
autofs.x86_64 1:5.0.7-40.el7
systemctl start autofs
cd /net/
cd 172.25.254.103
df 查看当前挂载
cd nfsdir/
df 已自动挂载
cd
df 等待后自动卸载
配置文件vim /etc/sysconfig/autofs
重启服务systemctl restart autofs.service
服务启动
[[email protected] ~]# ls -ld /net //未启动软件无该目录
ls: cannot access /net: No such file or director
[[email protected] ~]# systemctl start autofs
[[email protected] ~]# systemctl enable autofs.service
验证自动挂载
[[email protected] ~]# ls -ld /net //启动服务后有/net服务
drwxr-xr-x 2 root root 0 Feb 26 21:21 /net
[[email protected] ~]# cd /net/
[[email protected] net]# ls //此时无内容
[[email protected] net]# ls -a
. ..
[[email protected] net]# pwd
/net
[[email protected] net]# cd 172.25.254.103 //连接到共享目录
[[email protected] 172.25.254.103]# pwd
/net/172.25.254.103 //位置为/net下的/172.25.254.103
[[email protected] 172.25.254.103]# ls
nfsdir
[[email protected] 172.25.254.103]# df //此时为自动挂载
[[email protected] 172.25.254.103]# cd nfsdir/
[[email protected] nfsdir]# df //已自动挂载
172.25.254.103:/nfsdir 10473984 3160704 7313280 31% /net/172.25.254.103/nfsdir
[[email protected] nfsdir]# cd
[[email protected] ~]# df //未自动卸载,默认时长300s
172.25.254.103:/nfsdir 10473984 3160704 7313280 31% /net/172.25.254.103/nfsdir
[[email protected] ~]# vim /etc/sysconfig/autofs //更改配置文件
13 TIMEOUT=3
[[email protected] ~]# systemctl restart autofs.service //重启服务
[[email protected] ~]# cd /net/
[[email protected] net]# ls
[[email protected] net]# cd 172.25.254.103
[[email protected] 172.25.254.103]# ls
nfsdir
[[email protected] 172.25.254.103]# cd nfsdir/
[[email protected] nfsdir]# df
172.25.254.103:/nfsdir 10473984 3160576 7313408 31% /net/172.25.254.103/nfsdir
[[email protected] nfsdir]# cd
[[email protected] ~]# df //此时已自动卸载
[[email protected] ~]# pwd
/root
6.自定义挂载点
编辑主配置文件vim /etc/auto.master
编写配置文件vim /etc/auto.nfs
重启服务systemctl restart autofs.service
查看挂载参数mount
【实验】
[[email protected] ~]# vim /etc/auto.master //编辑主配置文件,指定挂载目录配置文件
7 /misc /etc/auto.misc
8 /mnt /etc/auto.nfs
!!!!此时配置文件中写的是挂载点的上层文件目录,这里为/mnt
[[email protected] ~]# vim /etc/auto.nfs //编写指定挂载目录配置文件
westos 172.25.254.103:/nfsdir
[[email protected] ~]# systemctl restart autofs.service
[[email protected] ~]# cd /mnt
[[email protected] mnt]# ls
[[email protected] mnt]# cd westos
[[email protected] westos]# ls
file1 file2 file3 file4 file5 file6 file7 file8
[[email protected] westos]# df
172.25.254.103:/nfsdir 10473984 3160704 7313280 31% /mnt/westos
[[email protected] westos]# mount //查看挂载参数
(rw,relatime //默认为rw
[[email protected] westos]# vim /etc/auto.nfs
westos -ro,vers=3.0 172.25.254.103:/nfsdir
[[email protected] westos]# systemctl restart autofs.service
[[email protected] ~]# cd /mnt/westos
[[email protected] westos]# ls
file1 file2 file3 file4 file5 file6 file7 file8
[[email protected] westos]# df
172.25.254.103:/nfsdir 10473984 3160704 7313280 31% /mnt/westos
[[email protected] westos]# mount //查看挂载参数
(ro,relatime,vers=3 //已变更为更改后的版本