Linux下资源共享之NFS服务的搭建!

简介:

NFS简介

NFS是Network File System的简写,即网络文件系统.

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

NFS好处

image

以下是NFS最显而易见的好处:

1.本地工作站使用更少的磁盘空间,因为通常的数据可以存放在一台机器上而且可以通过网络访问到。

2.用户不必在每个网络上机器里头都有一个home目录。Home目录 可以被放在NFS服务器上并且在网络上处处可用。

3.诸如软驱,CDROM,和 Zip(是指一种高储存密度的磁盘驱动器与磁盘)之类的存储设备可以在网络上面被别的机器使用。这可以减少整个网络上的可移动介质设备的数量。

NFS组成

NFS至少有两个主要部分:一台服务器和一台(或者更多)客户机。客户机远程访问存放在服务器上的数据。为了正常工作,一些进程需要被配置并运行。

实际应用

NFS 有很多实际应用。下面是比较常见的一些:

1.多个机器共享一台CDROM或者其他设备。这对于在多台机器中安装软件来说更加便宜跟方便。

2.在大型网络中,配置一台中心 NFS 服务器用来放置所有用户的home目录可能会带来便利。这些目录能被输出到网络以便用户不管在哪台工作站上登录,总能得到相同的home目录。

3.几台机器可以有通用的/usr/ports/distfiles 目录。这样的话,当您需要在几台机器上安装port时,您可以无需在每台设备上下载而快速访问源码。

 案例精讲NFS:

[root@gjp99 ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2A:5C:13  
          inet addr:192.168.10.99  Bcast:192.168.10.255  Mask:255.255.255.0 
          inet6 addr: fe80::20c:29ff:fe2a:5c13/64 Scope:Link 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
          RX packets:373 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:360 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:33572 (32.7 KiB)  TX bytes:28950 (28.2 KiB) 
          Interrupt:67 Base address:0x2000

[root@gjp99 ~]# rpcinfo –p    //查看本地的 
   program vers proto   port 
    100000    2   tcp    111  portmapper 
    100000    2   udp    111  portmapper 
    100024    1   udp    867  status 
    100024    1   tcp    870  status

[root@gjp99 ~]# rpcinfo -p 192.168.10.9  //查看远程的! 
   program vers proto   port 
    100000    2   tcp    111  portmapper 
    100000    2   udp    111  portmapper 
    100024    1   udp    900  status 
    100024    1   tcp    903  status

[root@gjp99 ~]# mkdir /public

[root@gjp99 ~]# cd /public 
[root@gjp99 public]# ll 
total 0

touch个文件gjp,便于顾客访问时作为参考! 
[root@gjp99 public]# echo "guo jiping"> gjp 
[root@gjp99 public]# ll 
total 4 
-rw-r--r-- 1 root root 11 Aug  3 17:07 gjp

[root@gjp99 ~]# man 5 exports

General Options 
      exportfs understands the following export options:

      secure This  option  requires  that requests originate on an internet port less than 
             IPPORT_RESERVED (1024). This option is on by default. To turn it off, specify 
             insecure.

      rw     Allow both read and write requests on this NFS volume. The default is to dis- 
             allow any request which changes  the  filesystem.   This  can  also  be  made 
             explicit by using the ro option.

      async  This  option  allows  the NFS server to violate the NFS protocol and reply to 
             requests before any changes made by that request have been committed to  sta- 
             ble storage (e.g. disc drive).

             Using  this  option might improve performance with version 2 only, but at the 
             cost that an unclean server restart (i.e. a crash) can cause data to be  lost 
             or corrupted.

      sync   Reply  to requests only after the changes have been committed to stable stor- 
             age (see async above).

[root@gjp99 ~]# vim /etc/exports 
[root@gjp99 ~]# cat  /etc/exports 
/public 192.168.10.0/24(ro) 
[root@gjp99 ~]# rpm -qa |grep nfs 
nfs-utils-1.0.9-42.el5 
nfs-utils-lib-1.0.8-7.6.el5

[root@gjp99 ~]# rpm -ql nfs-utils |less

/etc/rc.d/init.d/nfs  控制脚本的名字

[root@gjp99 ~]# chkconfig --list |grep nfs  查看nfs服务! 
nfs                0:off    1:off    2:off    3:off    4:off    5:off    6:off 
nfslock            0:off    1:off    2:off    3:on    4:on    5:on    6:off 
[root@gjp99 ~]# service nfs start 
Starting NFS services:                                     [  OK  ] 
Starting NFS quotas:                                       [  OK  ] 
Starting NFS daemon:                                       [  OK  ] 
Starting NFS mountd:                                       [  OK  ]

发现本地多了好多东西!

[root@gjp99 ~]# rpcinfo –p  
   program vers proto   port 
    100000    2   tcp    111  portmapper 
    100000    2   udp    111  portmapper 
    100024    1   udp    867  status 
    100024    1   tcp    870  status 
    100011    1   udp    903  rquotad 
    100011    2   udp    903  rquotad 
    100011    1   tcp    906  rquotad 
    100011    2   tcp    906  rquotad 
    100003    2   udp   2049  nfs 
    100003    3   udp   2049  nfs 
    100003    4   udp   2049  nfs 
    100021    1   udp  40890  nlockmgr 
    100021    3   udp  40890  nlockmgr 
    100021    4   udp  40890  nlockmgr 
    100003    2   tcp   2049  nfs 
    100003    3   tcp   2049  nfs 
    100003    4   tcp   2049  nfs 
    100021    1   tcp  51129  nlockmgr 
    100021    3   tcp  51129  nlockmgr 
    100021    4   tcp  51129  nlockmgr 
    100005    1   udp    930  mountd 
    100005    1   tcp    933  mountd 
    100005    2   udp    930  mountd 
    100005    2   tcp    933  mountd 
    100005    3   udp    930  mountd 
    100005    3   tcp    933  mountd

[root@gjp99 ~]# chkconfig nfs on

必须导出

[root@gjp99 ~]# exportfs –rv   // r可译为导出,v为详细信息! 
exporting 192.168.10.0/24:/public

测试:

192.168.10. 0/24的客户机192.168.10.9访问

[root@mail ~]# rpcinfo –p   //先查看是否有端口映射! 
   program vers proto   port 
    100000    2   tcp    111  portmapper 
    100000    2   udp    111  portmapper 
    100024    1   udp    900  status 
    100024    1   tcp    903  status 
[root@mail ~]# showmount -e 192.168.10.99 //查看服务器上的共享列表 
Export list for 192.168.10.99: 
/public 192.168.10.0/24

创建挂载点: 
[root@mail ~]# mkdir /mnt/nfs 
[root@mail ~]# mount 192.168.10.99:/public /mnt/nfs //手动挂载

[root@mail ~]# df -h 
Filesystem            Size  Used Avail Use% Mounted on 
/dev/sda2             9.5G  2.3G  6.7G  26% / 
/dev/sda1              99M   12M   83M  12% /boot 
tmpfs                 506M     0  506M   0% /dev/shm 
192.168.10.99:/public 
                      9.5G  2.3G  6.8G  26% /mnt/nfs 
[root@mail ~]# cd /mnt/nfs

[root@mail nfs]# ll   //看到服务器上的共享文件 
total 4 
-rw-r--r-- 1 root root 11 Aug  3 17:07 gjp

[root@mail nfs]# mount    
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 
192.168.10.99:/public on /mnt/nfs type nfs (rw,addr=192.168.10.99)

如何做到开机自动挂载?

[root@mail nfs]# pwd   查看当前目录 
/mnt/nfs 
[root@mail nfs]# cd   离开要卸载的目录 
[root@mail ~]# umount /mnt/nfs/   卸载光盘 
[root@mail ~]# mount 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

[root@mail ~]# vim /etc/fstab

192.168.10.99:/public   /mnt/nfs                nfs     ro,soft,intr    0 0

[root@mail ~]# mount -a 
[root@mail ~]# mount 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 
192.168.10.99:/public on /mnt/nfs type nfs (ro,soft,intr,addr=192.168.10.99)

自动挂载:

[root@mail ~]# rpm -qa |grep auto 
automake14-1.4p6-13 
autoconf-2.59-12 
automake-1.9.6-2.1 
autofs-5.0.1-0.rc2.131.el5 
automake17-1.7.9-7 
automake15-1.5-16 
automake16-1.6.3-8 
[root@mail ~]# chkconfig --list |grep auto 
autofs             0:off    1:off    2:off    3:on    4:on    5:on    6:off

[root@mail ~]# vim /etc/auto.master

10 /mnt/nfs  /etc/auto.nfs   --timeout=60    //1分钟之内如果没有操作,则自动挂断

[root@mail ~]# ll /etc/auto* 
-rw------- 1 root root 2726 Jul 29  2009 /etc/autofs_ldap_auth.conf 
-rw-r--r-- 1 root root  756 Sep  5 14:17 /etc/auto.master 
-rw-r--r-- 1 root root  581 Jul 29  2009 /etc/auto.misc 
-rwxr-xr-x 1 root root 1292 Jul 29  2009 /etc/auto.net 
-rwxr-xr-x 1 root root  715 Jul 29  2009 /etc/auto.smb 
[root@mail ~]# cp -p /etc/auto.misc /etc/auto.nfs

[root@mail ~]# vim /etc/auto.nfs

server1         -ro,soft,intr           192.168.10.99:/public

[root@mail ~]# service autofs restart    启动服务 
Stopping automount:                                        [  OK  ] 
Starting automount:                                        [  OK  ] 
[root@mail ~]# mount 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 
[root@mail ~]# cd /mnt/nfs 
[root@mail nfs]# ll 
total 0 
[root@mail nfs]# cd server1   进入此目录,才可以挂载! 
[root@mail server1]# mount 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 
192.168.10.99:/public on /mnt/nfs/server1 type nfs (ro,soft,intr,addr=192.168.10.99)

[root@mail ~]# mount    //1分钟过后,查看挂载点消失 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

服务器上配置:

[root@gjp99 public]# mkdir /gjp 
[root@gjp99 public]# cat /etc/exports 
/public 192.168.10.0/24(ro) 
/gjp    192.168.10.0/24(rw,sync)  //增加此行

注意:子网掩码后面不能有空格!

[root@gjp99 public]# ll -d /gjp 
drwxr-xr-x 2 root root 4096 Aug  3 18:12 /gjp 
[root@gjp99 public]# chmod o+wt /gjp

[root@gjp99 ~]# ll -d /gjp 
drwxr-xrwt 2 root root 4096 Oct 15 10:32 /gjp

[root@gjp99 ~]# exportfs -rv 
exporting 192.168.10.0/24:/public 
exporting 192.168.10.0/24:/gjp

客户端上:

[root@mail ~]# mkdir /mnt/gjp 
[root@mail ~]# mount 192.168.10.99:/gjp /mnt/gjp 
[root@mail ~]# mount 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 
192.168.10.99:/gjp on /mnt/gjp type nfs (rw,addr=192.168.10.99) 
[root@mail ~]# cd /mnt/gjp

[root@mail gjp]# ll 
total 0 
[root@mail gjp]# touch gjp

[root@mail gjp]# ll 
total 0 
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 15  2012 gjp

服务器上:

man exports

 image

[root@gjp99 ~]# vim /etc/exports

/public 192.168.10.0/24(ro) 
/gjp    192.168.10.0/24(rw,no_root_squash,sync)增加参数:取消映射关系

[root@gjp99 ~]# service nfs restart 
Shutting down NFS mountd:                                  [  OK  ] 
Shutting down NFS daemon:                                  [  OK  ] 
Shutting down NFS quotas:                                  [  OK  ] 
Shutting down NFS services:                                [  OK  ] 
Starting NFS services:                                     [  OK  ] 
Starting NFS quotas:                                       [  OK  ] 
Starting NFS daemon:                                       [  OK  ] 
Starting NFS mountd:                                       [  OK  ] 
[root@gjp99 ~]# service autofs restart 
Stopping automount:                                        [  OK  ]

Starting automount:                                        [  OK  ]

客户端测试:

[root@mail ~]# umount /mnt/gjp 
[root@mail ~]# mount 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 
192.168.10.99:/public on /mnt/nfs type nfs (ro,soft,intr,addr=192.168.10.99) 
[root@mail ~]# mount 192.168.10.99:/gjp /mnt/gjp 
[root@mail ~]# cd /mnt/gjp 
[root@mail gjp]# ll 
total 0 
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 15  2012 gjp 
[root@mail gjp]# touch guolu 
[root@mail gjp]# ll 
total 0 
-rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 15  2012 gjp 
-rw-r--r-- 1 root      root      0 Oct 15  2012 guolu 

取消映射后:发现root用户,更不安全!还是用映射的好!



本文转自 gjp0731 51CTO博客,原文链接:http://blog.51cto.com/guojiping/1026465

相关文章
|
3月前
|
Linux 网络安全 Docker
盘古栈云,创建带ssh服务的linux容器
创建带ssh服务的linux容器
336 146
|
4月前
|
缓存 安全 Linux
六、Linux核心服务与包管理
在没有网络的情况下,使用系统安装光盘是获取RPM包的常用方法。场景二:配置本地文件镜像源 (使用系统安装光盘/ISO)(检查RPM包的GPG签名以保证安全) 或。YUM/DNF包管理工具 (yum/dnf)(此处可以放置您为本主题制作的思维导图)处理依赖问题的危险选项 (应极力避免)(覆盖文件、替换已安装包)。(list) 则是列出文件。(query file) 是。(假设系统安装光盘已挂载到。信息 (verbose)。(upgrade) 选项。(all) 已安装的包。(package) 选项
433 12
|
9月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
814 25
|
11月前
|
存储 Linux Shell
Linux|Transfer.sh 轻松实现文件共享
Linux|Transfer.sh 轻松实现文件共享
Linux|Transfer.sh 轻松实现文件共享
|
10月前
|
Linux iOS开发 MacOS
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
313 0
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Git 服务
|
12月前
|
Linux Docker 容器
Linux 中停止 Docker 服务报 warning 导致无法彻底停止问题如何解决?
在 Linux 系统中,停止 Docker 服务时遇到警告无法彻底停止的问题,可以通过系统管理工具停止服务、强制终止相关进程、检查系统资源和依赖关系、以及重置 Docker 环境来解决。通过以上步骤,能够有效地排查和解决 Docker 服务停止不彻底的问题,确保系统的稳定运行。
906 19
|
12月前
|
监控 Linux
Linux systemd 服务启动失败Main process exited, code=exited, status=203/EXEC
通过以上步骤,可以有效解决 systemd 服务启动失败并报错 `Main process exited, code=exited, status=203/EXEC` 的问题。关键在于仔细检查单元文件配置、验证可执行文件的有效性,并通过日志分析具体错误原因。确保可执行文件路径正确、文件具有执行权限,并且可以独立运行,将有助于快速定位和解决问题。
5318 7
|
Ubuntu Unix Linux
Linux网络文件系统NFS:配置与管理指南
NFS 是 Linux 系统中常用的网络文件系统协议,通过配置和管理 NFS,可以实现跨网络的文件共享。本文详细介绍了 NFS 的安装、配置、管理和常见问题的解决方法,希望对您的工作有所帮助。通过正确配置和优化 NFS,可以显著提高文件共享的效率和安全性。
3082 7
|
Linux
linux中服务管理
在Linux服务管理中,从传统的System V init到Upstart,再到广泛采用的systemd,管理方式不断发展。systemd以其强大的功能和依赖解决成为现代Linux的核心。`systemctl`是管理服务的关键命令,如启动(`start`)、停止(`stop`)、重启(`restart`)服务及设置开机启动(`enable`)或取消(`disable`)。了解和服务管理,特别是systemd和`systemctl`的使用,对系统管理员至关重要。其他如xinetd用于控制网络服务,而特定环境下有OpenRC等工具。
239 2
|
安全 Ubuntu 网络协议
Linux中的服务管理
Linux系统可以同时运行数百个任务。其中大多数都属于操作系统环境的组成部分,不过可能也会有那么一两个你不需要的**守护进程**。有3种可用于启动守护进程和服务的工具,Linux发行版支持其中任意一种。传统的SysV系统使用/etc/init.d中的脚本。较新的systemd守护进程除了使用/etc/init.d之外,还用到了systemctl调用。还有些发行版使用的是upstart,配置脚本保存在/etc/init中。systemd如今已经取代了SysVinit系统。upstart是由Ubuntu开发并采用的,但是在14.04版中,已经改成了systemd。
346 3