多语言展示
当前在线:315今日阅读:142今日分享:25

Linux环境下搭建NFS服务器

NFS,是Network File System的简写,即网络文件系统。网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS.NFS允许一个系统在网络上与他人共享目录和文件。通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件。
工具/原料

2台模拟服务器,xuegod63服务器端,xuegod64客户端

方法/步骤
1

安装nfs• [root@xuegod63 ~]# yum -y install nfs*管理命令• [root@xuegod63 ~]# yum install mount

2

先查看2049端口是否开放:[root@xuegod63 ~]# netstat -antpu | grep 2049tcp        0      0 0.0.0.0:2049                0.0.0.0:*                   LISTEN      -tcp        0      0 :::2049                     :::*                        LISTEN     +启动NFS服务[root@xuegod63 ~]# service nfs startStarting NFS services:                                     [  OK  ]Starting NFS quotas:                                       [  OK  ]Starting NFS mountd:                                       [  OK  ]Starting NFS daemon:                                       [  OK  ]Starting RPC idmapd:                                       [  OK  ]再次查看端口监听状态[root@xuegod63 ~]# netstat -antpu | grep 2049tcp        0      0 0.0.0.0:2049                0.0.0.0:*                   LISTEN      -tcp        0      0 :::2049                     :::*                        LISTEN     +

3

配置开机自动启动[root@xuegod63 ~]# chkconfig nfs on服务的使用方法showmount -e NFS服务器IP例:[root@xuegod64 ~]# showmount -e 192.168.1.63Export list for 192.168.1.63:挂载[root@xuegod64 ~]# mount 192.168.1.63:/tmp /opt修改配置文件,实战举例[root@xuegod63 ~]# vim /etc/exports/media  *(rw)重启服务[root@xuegod63 ~]# service nfs restart

4

客户端查看:[root@xuegod64 ~]# showmount -e 192.168.1.63Export list for 192.168.1.63:/media *

5

挂载共享[root@xuegod64 ~]# mount -t nfs 192.168.1.63:/media/ /opt/[root@xuegod64 ~]# df -hFilesystem            Size  Used Avail Use% Mounted on/dev/sda2             9.7G  4.0G  5.2G  44% /tmpfs                 996M   80K  996M   1% /dev/shm/dev/sda1             485M   39M  421M   9% /boot/dev/sr0              3.7G  3.7G     0 100% /mnt192.168.1.63:/media/  9.7G  4.0G  5.3G  43% /opt

6

验证写入权限[root@xuegod64 ~]# touch /opt/a.txttouch: 无法创建'/opt/a.txt': 权限不够 解决方法:设置访问权限一般包含2部分1)服务本身权限2)目录访问权限nfs默认使用nfsnobody用户[root@xuegod63 ~]# grep nfs /etc/passwdrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologinnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin修改权限[root@xuegod63 ~]# chmod 777 -R /media/或[root@xuegod63 ~]# chown nfsnobody.nfsnobody -R /media/再次验证写入权限[root@xuegod64 ~]# touch /opt/a.txt[root@xuegod64 ~]# ll !$ll /opt/a.txt-rw-r--r-- 1 nfsnobody nfsnobody 0 5月  24 2016 /opt/a.txt

7

共享参数做一些特殊处理 [root@xue63 a]# cat /etc/exports/tmp/a/no_root_squash      *(rw,no_root_squash)/tmp/a/sync               192.168.0.0/24(rw,sync)/tmp/a/ro                  192.168.1.64(ro)/tmp/a/all_squash             192.168.0.0/24(rw,all_squash,anonuid=500,anongid=500)/tmp/a/async                    192.168.3.0/255.255.255.0(async)/tmp/a/rw          192.168.3.0/255.255.255.0(rw)    192.168.4.0/255.255.255.0(rw)/tmp/a/root_squash   *(rw,root_squash)     sync/async:数据同步写入硬盘/不同步写入在内存中缓存root_squash(压制):如果用root登录nfs,使其身份自动切换成nfsnobody。no_root_squash:如果用root登录nfs,使其身份就是root。all_squash:用户登录nfs时,指定身份为UID/GID的用户。注意:在发布共享目录的格式中除了共享目录是必跟参数外,其他参数都是可选的。并且共享目录与客户端之间及客户端与客户端之间需要使用空格符号,但是客户端与参数之间是不能有空格的

8

[root@xuegod63 tmp]# service nfs restart  #重启 [root@xuegod64 ~]# showmount -e 192.168.1.63Export list for 192.168.1.63:/tmp/a/root_squash    */tmp/a/no_root_squash */media                */tmp/a/all_squash     192.168.0.0/24/tmp/a/rw             192.168.4.0/255.255.255.0,192.168.3.0/255.255.255.0/tmp/a/async          192.168.3.0/255.255.255.0/tmp/a/ro             192.168.1.64/tmp/a/sync           192.168.1.0/24测试:[root@xuegod64 ~]# umount /opt/[root@xuegod64 ~]# mount 192.168.1.63:/tmp/a/no_root_squash /opt/[root@xuegod64 ~]# touch /opt/no_root_squash.txt[root@xuegod64 ~]# ll !$ll /opt/no_root_squash.txt-rw-r--r-- 1 root root 0 Mar  6 22:15 /opt/no_root_squash.txt [root@xuegod64 ~]# mount 192.168.0.63:/tmp/a/root_squash /mnt/[root@xuegod64 ~]# touch /mnt/root_squash.sh[root@xuegod64 ~]# ll /mnt/total 0-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 28  2016 root_squash.sh 测试:[root@xuegod63 a]# useradd cat[root@xuegod63 a]# id catuid=500(cat) gid=500(t cat 组=500(cat)[root@xuegod64 ~]# mount 192.168.1.63:/tmp/a/all_squash  /opt/[root@xuegod64 ~]# cd /opt/[root@xuegod64 opt]# ls[root@xuegod64 opt]# touch a.txt

9

触发式自动挂载autofs软件包要实现自动挂载涉及到两个文件,auto.master和auto.misc安装autofs,默认已经安装[root@xuegod64 ~]# yum -y install autofs/etc/auto.master 文件定义本地挂载点./etc/auto.misc 配置文件是用来设置需要挂载的文件系统类型和选项 创建本地挂载点[root@xuegod64 ~]# mkdir /tmp/a[root@xuegod64 ~]# vim /etc/auto.master/tmp/a  /etc/ auto.misc  --timeout=60#-timeout=60 挂载超时时间,单位为秒。可以修改这个参数。[root@xuegod64 ~]# vim /etc/auto.misc nfs     -fstype=nfs     192.168.1.63: /tmp/a/all_squash

10

重启autofs服务[root@xuegod64 ~]# service autofs restart停止 automount:                                           [确定]正在启动 automount:                                       [确定]注: 只有cd  /tmp/a/nfs  进去, 触发一下,才能自动挂载。 另外 nfs目录,不能提前创建,自动挂载时,系统自动创建nfs目录。 mount   192.168.1.63: /tmp/a/all_squash    /tmp/a/nfs[root@xuegod64 nfs]# df -h192.168.1.63: /tmp/a/all_squash    9.7G  3.9G  5.3G  43% /tmp/a/nfs重启autofs服务[root@xuegod64 ~]# service autofs restart停止 automount:                                           [确定]正在启动 automount:                                       [确定]注: 只有cd  /tmp/a/nfs  进去, 触发一下,才能自动挂载。 另外 nfs目录,不能提前创建,自动挂载时,系统自动创建nfs目录。mount   192.168.1.63: /tmp/a/all_squash    /tmp/a/nfs[root@xuegod64 nfs]# df -h192.168.1.63: /tmp/a/all_squash    9.7G  3.9G  5.3G  43% /tmp/a/nfs

注意事项
1

只有cd /tmp/a/nfs 进去, 触发一下,才能自动挂载。 另外 nfs目录,不能提前创建,自动挂载时,系统自动创建nfs目录。

2

只有cd /tmp/a/nfs 进去, 触发一下,才能自动挂载。 另外 nfs目录,不能提前创建,自动挂载时,系统自动创建nfs目录。

推荐信息