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   同步共享

【实验】

[root@desktop203 ~]# yum list nfs-utils               //查看是否安装,installed为已安装
[root@server103 ~]# yum list nfs-utils
[root@desktop203 ~]# showmount -e 172.25.254.103       //服务器未开启服务
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

开启服务

[root@server103 ~]# systemctl status nfs           //查看状态
Active: inactive (dead)
[root@server103 ~]# systemctl start nfs-server
[root@server103 ~]# systemctl status nfs           //已开启
Active: active (exited)
[root@server103 ~]# systemctl stop firewalld    //关闭火墙
[root@server103 ~]# systemctl enable nfs-server          //开机自动启动
[root@desktop203 ~]# showmount -e 172.25.254.103 //客户端访问成功
Export list for 172.25.254.103:

实时共享

[root@server103 ~]# mkdir /nfsdir           //服务器端创建共享目录
[root@server103 ~]# ls -ld /nfsdir/           //查看共享目录
drwxr-xr-x. 2 root root 6 Feb 26 20:22 /nfsdir/
[root@server103 ~]# vim /etc/exports     //创建共享脚本文件,指向目录并共享
1 /nfsdir *(sync)
[root@server103 ~]# exportfs -rv            //实时同步共享
exporting *:/nfsdir
[root@desktop203 ~]# showmount -e 172.25.254.103           //客户端查看共享目录菜单
Export list for 172.25.254.103:
/nfsdir * //创建的共享目录成功
[root@desktop203 ~]# mount 172.25.254.103:/nfsdir /mnt/  //客户端以挂载的方式登入共享目录并可操作
[root@desktop203 ~]# df                           //查看挂载
172.25.254.103:/nfsdir 10473984 3160576 7313408 31% /mnt
[root@server103 ~]# touch /nfsdir/file1     //服务器创建文件以验证
[root@desktop203 ~]# ls /mnt/                  //客户端查看
file1
[root@desktop203 ~]# touch /mnt/file2     //此时/mnt为只读
touch: cannot touch ‘/mnt/file2’: Read-only file system

4.更改用户身份

查看帮助man 5 exports

这里写图片描述
[root@server103 ~]# 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失效
[root@server103 ~]# exportfs -rv //实时同步
客户端创建文件验证

【实验】

更改读写权限

[root@server103 ~]# vim /etc/exports
/nfsdir       172.25.254.0/24(sync)
/nfsdir       172.25.254.203(rw)
[root@server103 ~]# exportfs -rv
[root@desktop203 ~]# touch /mnt/file2           //此时为权限不足
touch: cannot touch ‘/mnt/file2’: Permission denied
[root@server103 ~]# chmod 777 /nfsdir/
[root@desktop203 ~]# touch /mnt/file2
[root@desktop203 ~]# 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

更改身份

[root@server103 ~]# id 1001
uid=1001(westos) gid=1001(westos) groups=1001(westos),1002(lee)
[root@server103 ~]# vim /etc/exports
/nfsdir     172.25.254.0/24(sync)
/nfsdir     172.25.254.203(rw,anonuid=1001)
[root@server103 ~]# exportfs -rv
[root@desktop203 ~]# 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

[root@server103 ~]# vim /etc/exports
/nfsdir       172.25.254.203(rw,anonuid=1001,anongid=1002)
[root@server103 ~]# id 1002
uid=1002(lee) gid=1002(lee) groups=1002(lee),1001(westos)
[root@server103 ~]# exportfs -rv
[root@desktop203 ~]# touch /mnt/file4
[root@desktop203 ~]# 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

[root@server103 ~]# vim /etc/exports
/nfsdir       172.25.254.203(rw,anonuid=1001,anongid=1002,no_root_squash)
[root@server103 ~]# exportfs -rv
[root@desktop203 ~]# touch /mnt/file5
-rw-r–r– 1   root    root    0 Feb 26 20:56 file5
[root@desktop203 ~]# su - student
[student@desktop203 ~]$ touch /mnt/file6
[student@desktop203 ~]$ ll /mnt/
-rw-rw-r– 1   student    student    0 Feb 26 20:57 file6
[student@desktop203 ~]$ exit
[root@desktop203 ~]# useradd westos
[root@desktop203 ~]# su - westos
[westos@desktop203 ~]$ touch /mnt/file7
[westos@desktop203 ~]$ ll /mnt/
-rw-rw-r– 1    westos     westos     0 Feb 26 20:58 file7
[root@desktop203 ~]# 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

【实验】

安装软件

[root@desktop203 ~]# umount /mnt/
[root@desktop203 ~]# 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

服务启动

[root@desktop203 ~]# ls -ld /net           //未启动软件无该目录
ls: cannot access /net: No such file or director
[root@desktop203 ~]# systemctl start autofs
[root@desktop203 ~]# systemctl enable autofs.service

验证自动挂载

[root@desktop203 ~]# ls -ld /net           //启动服务后有/net服务
drwxr-xr-x 2 root root 0 Feb 26 21:21 /net
[root@desktop203 ~]# cd /net/
[root@desktop203 net]# ls                     //此时无内容
[root@desktop203 net]# ls -a
. ..
[root@desktop203 net]# pwd
/net
[root@desktop203 net]# cd 172.25.254.103       //连接到共享目录
[root@desktop203 172.25.254.103]# pwd
/net/172.25.254.103                                             //位置为/net下的/172.25.254.103
[root@desktop203 172.25.254.103]# ls
nfsdir
[root@desktop203 172.25.254.103]# df             //此时为自动挂载
[root@desktop203 172.25.254.103]# cd nfsdir/
[root@desktop203 nfsdir]# df                             //已自动挂载
172.25.254.103:/nfsdir      10473984    3160704       7313280     31%    /net/172.25.254.103/nfsdir
[root@desktop203 nfsdir]# cd
[root@desktop203 ~]# df //未自动卸载,默认时长300s
172.25.254.103:/nfsdir      10473984    3160704       7313280     31%    /net/172.25.254.103/nfsdir
[root@desktop203 ~]# vim /etc/sysconfig/autofs    //更改配置文件
13 TIMEOUT=3
[root@desktop203 ~]# systemctl restart autofs.service    //重启服务
[root@desktop203 ~]# cd /net/
[root@desktop203 net]# ls
[root@desktop203 net]# cd 172.25.254.103
[root@desktop203 172.25.254.103]# ls
nfsdir
[root@desktop203 172.25.254.103]# cd nfsdir/
[root@desktop203 nfsdir]# df
172.25.254.103:/nfsdir    10473984   3160576     7313408    31%   /net/172.25.254.103/nfsdir
[root@desktop203 nfsdir]# cd
[root@desktop203 ~]# df      //此时已自动卸载
[root@desktop203 ~]# pwd
/root

6.自定义挂载点

编辑主配置文件vim /etc/auto.master
编写配置文件vim /etc/auto.nfs
重启服务systemctl restart autofs.service
查看挂载参数mount

【实验】

[root@desktop203 ~]# vim /etc/auto.master       //编辑主配置文件,指定挂载目录配置文件
7   /misc     /etc/auto.misc
8   /mnt       /etc/auto.nfs
这里写图片描述

!!!!此时配置文件中写的是挂载点的上层文件目录,这里为/mnt

[root@desktop203 ~]# vim /etc/auto.nfs        //编写指定挂载目录配置文件
westos        172.25.254.103:/nfsdir
[root@desktop203 ~]# systemctl restart autofs.service
[root@desktop203 ~]# cd /mnt
[root@desktop203 mnt]# ls
[root@desktop203 mnt]# cd westos
[root@desktop203 westos]# ls
file1   file2  file3   file4   file5   file6  file7  file8
[root@desktop203 westos]# df
172.25.254.103:/nfsdir    10473984    3160704    7313280   31%   /mnt/westos
[root@desktop203 westos]# mount             //查看挂载参数

(rw,relatime                                                 //默认为rw
[root@desktop203 westos]# vim /etc/auto.nfs
westos     -ro,vers=3.0       172.25.254.103:/nfsdir
[root@desktop203 westos]# systemctl restart autofs.service
[root@desktop203 ~]# cd /mnt/westos
[root@desktop203 westos]# ls
file1  file2   file3   file4  file5  file6  file7  file8
[root@desktop203 westos]# df
172.25.254.103:/nfsdir   10473984    3160704    7313280   31%   /mnt/westos
[root@desktop203 westos]# mount           //查看挂载参数
(ro,relatime,vers=3                                    //已变更为更改后的版本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值