1. 程式人生 > >Linux之NFS網絡文件系統

Linux之NFS網絡文件系統

solved 記錄 nfs服務 服務啟動 exe tran nfs mount prot rpc服務

  NFS(Network File System)即網絡文件系統,是FreeBSD支持的文件系統中的一種,它允許網絡中的計算機之間通過TCP/IP網絡資源共享。在NFS的應用中,本地NFS的客戶端應用可以透明地讀寫位於遠端NFS服務器上的文件,就像訪問本地文件一樣。

1、好處

(1)節省本地存儲空間,將常用的數據存放在一臺NFS服務器上且可以通過網絡訪問,那麽本地終端將可以減少自身存儲空間的使用。

(2)用戶不需要在網絡中的每臺計算機上都建有Home目錄,Home目錄可以放在NFS服務器上且可以在網絡上被訪問使用。

(3)一些存儲設備如軟驅、CDROM和ZIP等都可以在網絡上被別的機器使用。這可以減少整個網絡上移動介質設備的數量。

2、組成

NFS體系至少有兩個主要部分:一臺NFS服務器和若幹臺客戶機,如圖所示

技術分享圖片

客戶機可以通過TCP/IP網絡遠程訪問存放在NFS上的數據。

NFS實現是通過RPC服務來實現的

技術分享圖片

實現過程:

(1)NFS RPC的主要功能是記錄每個NFS功能所對應的端口號,並將信息傳遞給請求數據的NFS客戶端,從而實現數據的傳輸。

(2)NFS服務啟動時會隨機取用數個端口,並主動向RPC服務註冊取用的相關端口信息,RPC服務就知道每個端口對應的NFS功能了,然後RPC會用固定的端口(111)來監聽NFS客戶端的請求,將正確的NFS端口傳給NFS的客戶端

服務啟動順序:RPC--》NFS

3、服務器端安裝,配置過程

(1)檢查是否安裝NFS RPC服務

[root@localhost ~]# rpm -qa nfs-utils rpcbind
[root@localhost ~]# yum install nfs-utils rpcbind -y
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                   
| 3.7 kB 00:00 extras | 3.4 kB 00:00 extras/primary_db | 30 kB 00:00 updates | 3.4 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package nfs-utils.x86_64 1:1.2.3-75.el6_9 will be installed --> Processing Dependency: nfs-utils-lib >= 1.1.0-3 for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 --> Processing Dependency: libtirpc >= 0.2.1-11 for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 --> Processing Dependency: keyutils >= 1.4-4 for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 --> Processing Dependency: python-argparse for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 --> Processing Dependency: libgssglue.so.1(libgssapi_CITI_2)(64bit) for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 --> Processing Dependency: libgssglue for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 --> Processing Dependency: libevent for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 --> Processing Dependency: libtirpc.so.1()(64bit) for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 --> Processing Dependency: libnfsidmap.so.0()(64bit) for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 --> Processing Dependency: libgssglue.so.1()(64bit) for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 --> Processing Dependency: libevent-1.4.so.2()(64bit) for package: 1:nfs-utils-1.2.3-75.el6_9.x86_64 ---> Package rpcbind.x86_64 0:0.2.0-13.el6_9.1 will be installed --> Running transaction check ---> Package keyutils.x86_64 0:1.4-5.el6 will be installed ---> Package libevent.x86_64 0:1.4.13-4.el6 will be installed ---> Package libgssglue.x86_64 0:0.1-11.el6 will be installed ---> Package libtirpc.x86_64 0:0.2.1-13.el6_9 will be installed ---> Package nfs-utils-lib.x86_64 0:1.1.5-13.el6 will be installed ---> Package python-argparse.noarch 0:1.2.1-2.1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================== Package Arch Version Repository Size ============================================================================== Installing: nfs-utils x86_64 1:1.2.3-75.el6_9 updates 336 k rpcbind x86_64 0.2.0-13.el6_9.1 updates 51 k Installing for dependencies: keyutils x86_64 1.4-5.el6 base 39 k libevent x86_64 1.4.13-4.el6 base 66 k libgssglue x86_64 0.1-11.el6 base 23 k libtirpc x86_64 0.2.1-13.el6_9 updates 82 k nfs-utils-lib x86_64 1.1.5-13.el6 base 71 k python-argparse noarch 1.2.1-2.1.el6 base 48 k Transaction Summary ============================================================================== Install 8 Package(s) Total download size: 716 k Installed size: 2.0 M Downloading Packages: (1/8): keyutils-1.4-5.el6.x86_64.rpm | 39 kB 00:00 (2/8): libevent-1.4.13-4.el6.x86_64.rpm | 66 kB 00:00 (3/8): libgssglue-0.1-11.el6.x86_64.rpm | 23 kB 00:00 (4/8): libtirpc-0.2.1-13.el6_9.x86_64.rpm | 82 kB 00:00 (5/8): nfs-utils-1.2.3-75.el6_9.x86_64.rpm | 336 kB 00:00 (6/8): nfs-utils-lib-1.1.5-13.el6.x86_64.rpm | 71 kB 00:00 (7/8): python-argparse-1.2.1-2.1.el6.noarch.rpm | 48 kB 00:00 (8/8): rpcbind-0.2.0-13.el6_9.1.x86_64.rpm | 51 kB 00:00 ------------------------------------------------------------------------------ Total 1.1 MB/s | 716 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : libgssglue-0.1-11.el6.x86_64 1/8 Installing : libtirpc-0.2.1-13.el6_9.x86_64 2/8 Installing : rpcbind-0.2.0-13.el6_9.1.x86_64 3/8 Installing : python-argparse-1.2.1-2.1.el6.noarch 4/8 Installing : libevent-1.4.13-4.el6.x86_64 5/8 Installing : keyutils-1.4-5.el6.x86_64 6/8 Installing : nfs-utils-lib-1.1.5-13.el6.x86_64 7/8 Installing : 1:nfs-utils-1.2.3-75.el6_9.x86_64 8/8 Verifying : rpcbind-0.2.0-13.el6_9.1.x86_64 1/8 Verifying : libtirpc-0.2.1-13.el6_9.x86_64 2/8 Verifying : keyutils-1.4-5.el6.x86_64 3/8 Verifying : libevent-1.4.13-4.el6.x86_64 4/8 Verifying : nfs-utils-lib-1.1.5-13.el6.x86_64 5/8 Verifying : libgssglue-0.1-11.el6.x86_64 6/8 Verifying : 1:nfs-utils-1.2.3-75.el6_9.x86_64 7/8 Verifying : python-argparse-1.2.1-2.1.el6.noarch 8/8 Installed: nfs-utils.x86_64 1:1.2.3-75.el6_9 rpcbind.x86_64 0:0.2.0-13.el6_9.1 Dependency Installed: keyutils.x86_64 0:1.4-5.el6 libevent.x86_64 0:1.4.13-4.el6 libgssglue.x86_64 0:0.1-11.el6 libtirpc.x86_64 0:0.2.1-13.el6_9 nfs-utils-lib.x86_64 0:1.1.5-13.el6 python-argparse.noarch 0:1.2.1-2.1.el6 Complete! [root@localhost ~]# rpm -qa nfs-utils rpcbind rpcbind-0.2.0-13.el6_9.1.x86_64 nfs-utils-1.2.3-75.el6_9.x86_64

(2)安裝完成後,啟動相關服務

[root@localhost ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-13.el6_9.1.x86_64
nfs-utils-1.2.3-75.el6_9.x86_64
[root@localhost ~]# /etc/init.d/rpcbind status
rpcbind is stopped
[root@localhost ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@localhost ~]# /etc/init.d/rpcbind status
rpcbind (pid  1958) is running...
[root@localhost ~]# /etc/init.d/nfs 
anaconda-ks.cfg     .bashrc             install.log.syslog
.bash_logout        .cshrc              .tcshrc
.bash_profile       install.log         
[root@localhost ~]# /etc/init.d/nfs status
rpc.svcgssd is stopped
rpc.mountd is stopped
nfsd is stopped
[root@localhost ~]# rpcinfo -p localhost
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper
[root@localhost ~]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
[root@localhost ~]# rpcinfo -p localhost
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper
    100005    1   udp  53776  mountd
    100005    1   tcp  45705  mountd
    100005    2   udp  59195  mountd
    100005    2   tcp  37339  mountd
    100005    3   udp  51079  mountd
    100005    3   tcp  42701  mountd
    100003    2   tcp   2049  nfs
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    2   tcp   2049  nfs_acl
    100227    3   tcp   2049  nfs_acl
    100003    2   udp   2049  nfs
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    2   udp   2049  nfs_acl
    100227    3   udp   2049  nfs_acl
    100021    1   udp  55657  nlockmgr
    100021    3   udp  55657  nlockmgr
    100021    4   udp  55657  nlockmgr
    100021    1   tcp  56873  nlockmgr
    100021    3   tcp  56873  nlockmgr
    100021    4   tcp  56873  nlockmgr

讓RPC、NFS服務開機自啟,在實際生產環境中都會將配置放在/etc/rc.local下面

[root@localhost ~]# vim /etc/rc.local 
[root@localhost ~]# cat /etc/rc.local 
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you dont
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/etc/init.d/rpcbind start
/etc/init.d/nfs start

(3)配置服務端NFS配置文件/etc/exports

[root@localhost ~]# cat /etc/exports 
##nfs server created by cairui at 2018-2-17
/home/cairui 192.168.181.128(rw)
[root@localhost ~]# /etc/init.d/nfs 
anaconda-ks.cfg     .bashrc             install.log.syslog
.bash_logout        .cshrc              .tcshrc
.bash_profile       install.log         .viminfo
[root@localhost ~]# /etc/init.d/nfs reload
[root@localhost ~]# /etc/init.d/nfs restart
Shutting down NFS daemon:                                  [  OK  ]
Shutting down NFS mountd:                                  [  OK  ]
Shutting down NFS services:                                [  OK  ]
Shutting down RPC idmapd:                                  [  OK  ]
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]

4、客戶端配置

[root@localhost ~]# rpm -qa | grep nfs
nfs-utils-lib-1.1.5-13.el6.x86_64
nfs-utils-1.2.3-75.el6_9.x86_64
[root@localhost ~]# rpm -qa | grep rpc
rpcbind-0.2.0-13.el6_9.1.x86_64
libtirpc-0.2.1-13.el6_9.x86_64
[root@localhost ~]# /etc/init.d/rpcbind status
rpcbind is stopped
[root@localhost ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@localhost ~]# /etc/init.d/rpcbind status
rpcbind (pid  1463) is running...
[root@localhost ~]# /etc/init.d/nfs status
rpc.svcgssd is stopped
rpc.mountd is stopped
nfsd is stopped
[root@localhost ~]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
[root@localhost ~]# showmount -e 192.168.181.129
Export list for 192.168.181.129:
/home/cairui 192.168.181.128
[root@localhost ~]# ping 192.168.181.129
PING 192.168.181.129 (192.168.181.129) 56(84) bytes of data.
64 bytes from 192.168.181.129: icmp_seq=1 ttl=64 time=0.348 ms
64 bytes from 192.168.181.129: icmp_seq=2 ttl=64 time=0.706 ms
64 bytes from 192.168.181.129: icmp_seq=3 ttl=64 time=0.512 ms
^C
--- 192.168.181.129 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2760ms
rtt min/avg/max/mdev = 0.348/0.522/0.706/0.146 ms
[root@localhost ~]# iptables -F
[root@localhost ~]# showmount -e 192.168.181.129
Export list for 192.168.181.129:
/home/cairui 192.168.181.128

客戶端進行NFS掛載

[root@localhost ~]# mount -t nfs 192.168.181.129:/home/cairui /mnt
[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              18G  1.4G   16G   9% /
tmpfs                 491M     0  491M   0% /dev/shm
/dev/sda1             190M   30M  150M  17% /boot
192.168.181.129:/home/cairui
                       18G  809M   16G   5% /mnt

5、測試

服務端

[root@localhost ~]# cd /home/cairui/
[root@localhost cairui]# touch 1.txt
[root@localhost cairui]# ls -ll /home/cairui/
total 0
-rw-r--r--. 1 root root 0 Feb 17 17:15 1.txt
[root@localhost cairui]# ls -ld /home/cairui/
drwx------. 2 cairui cairui 4096 Feb 17 17:15 /home/cairui/
[root@localhost cairui]# chmod 777 /home/cairui/
[root@localhost cairui]# ls -ld /home/cairui/
drwxrwxrwx. 2 cairui cairui 4096 Feb 17 17:15 /home/cairui/

 [root@localhost cairui]# mkdir nfs

客戶端

[root@localhost ~]# cd /mnt/
-bash: cd: /mnt/: Permission denied
[root@localhost ~]# ls -ll /mnt/
ls: cannot open directory /mnt/: Permission denied
[root@localhost ~]# cd /m
media/ mnt/   
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
1.txt
[root@localhost mnt]# ls
1.txt  nfs
[root@localhost mnt]# 

八.啟動自動掛載nfs文件系統

格式:<server>:<remote/export>

</local/directory> nfs <option> 0 0

#vim /etc/fstab

技術分享圖片

最後一行為後加的

保存退出,重啟系統

Linux之NFS網絡文件系統