Linux教程

【NFS】Linux安装NFS

本文主要是介绍【NFS】Linux安装NFS,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Linux上NFS 服务端 客户端的安装及配置

1. 系统环境

$ cat /proc/version 
Linux version 3.10.0-327.el7.x86_64 (mockbuild@x86-034.build.eng.bos.redhat.com) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Thu Oct 29 17:29:29 EDT 2015

2. NFS服务端搭建

2.1 安装并检查NFS和RPC服务

# 安装
$ yum install nfs-utils rpcbind -y
# 检查
$ rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-32.el7.x86_64
nfs-utils-1.3.0-0.21.el7.x86_64

2.2 启动RPC服务和NFS服务 (必须先启动RPC服务,然后再启动NFS服务)

$ systemctl start rpcbind
$ systemctl start nfs
$ ps -ef|grep rpc
rpc        960     1  0  2020 ?        00:01:15 /sbin/rpcbind -w
rpcuser  11716     1  0 13:57 ?        00:00:00 /usr/sbin/rpc.statd --no-notify
root     11724     2  0 13:57 ?        00:00:00 [rpciod]
root     11728     1  0 13:57 ?        00:00:00 /usr/sbin/rpc.idmapd
root     11743     1  0 13:57 ?        00:00:00 /usr/sbin/rpc.mountd
root     12483 20145  0 13:57 pts/1    00:00:00 grep --color=auto rpc
$ ps -ef|grep nfs
root     11755     2  0 13:57 ?        00:00:00 [nfsd4]
root     11756     2  0 13:57 ?        00:00:00 [nfsd4_callbacks]
root     11760     2  0 13:57 ?        00:00:00 [nfsd]
root     11761     2  0 13:57 ?        00:00:00 [nfsd]
root     11762     2  0 13:57 ?        00:00:00 [nfsd]
root     11763     2  0 13:57 ?        00:00:00 [nfsd]
root     11764     2  0 13:57 ?        00:00:00 [nfsd]
root     11765     2  0 13:57 ?        00:00:00 [nfsd]
root     11766     2  0 13:57 ?        00:00:00 [nfsd]
root     11767     2  0 13:57 ?        00:00:00 [nfsd]
root     12644 20145  0 13:57 pts/1    00:00:00 grep --color=auto nfs
$ lsof -i:111
COMMAND PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
rpcbind 960  rpc    6u  IPv4 90650864      0t0  UDP *:sunrpc 
rpcbind 960  rpc    8u  IPv4 90650866      0t0  TCP *:sunrpc (LISTEN)
rpcbind 960  rpc    9u  IPv6 90650867      0t0  UDP *:sunrpc 
rpcbind 960  rpc   11u  IPv6 90650869      0t0  TCP *:sunrpc (LISTEN)

$ 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
    100024    1   udp  51177  status
    100024    1   tcp  55076  status
    100005    1   udp  20048  mountd
    100005    1   tcp  20048  mountd
    100005    2   udp  20048  mountd
    100005    2   tcp  20048  mountd
    100005    3   udp  20048  mountd
    100005    3   tcp  20048  mountd
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    3   tcp   2049  nfs_acl
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    3   udp   2049  nfs_acl
    100021    1   udp  60484  nlockmgr
    100021    3   udp  60484  nlockmgr
    100021    4   udp  60484  nlockmgr
    100021    1   tcp  38648  nlockmgr
    100021    3   tcp  38648  nlockmgr
    100021    4   tcp  38648  nlockmgr

2.3 设置开机自启动并检查

$ systemctl enable rpcbind
$ systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
$ systemctl list-unit-files --type=service|grep "enabled"|egrep "rpcbind|nfs"
nfs-server.service                            enabled 
nfs.service                                   enabled

2.4 创建共享目录

$ mkdir /data/nfs-server-images
$ chown -R nfsnobody.nfsnobody /data/nfs-server-images
$ ls -ld /data/nfs-server-images
drwxr-x--- 2 nfsnobody nfsnobody 4096 1月   6 14:18 /data/nfs-server-images

2.5 设置允许接入的网址和目录

$ cat>>/etc/exports<<EOF
#NFS server share directories
/data/nfs-server-images 10.1.236.0/24(ro,sync,no_root_squash)
EOF
$ cat /etc/exports
#NFS server share directories
/data/nfs-server-images 10.1.236.0/24(ro,sync,no_root_squash)

2.6 平滑重启NFS服务并检查服务

$ systemctl reload nfs
$ cat /var/lib/nfs/etab
/data/nfs-server-images	10.1.236.0/24(ro,sync,wdelay,hide,nocrossmnt,secure,no_root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,ro,secure,root_squash,no_all_squash)

2.7 本地查看挂载目录

$ showmount -e 10.1.236.58
Export list for 10.1.236.58:
/data/nfs-server-images 10.1.236.0/24

3. NFS客户端搭建

3.1 安装NFS和RPC服务并检查

$ yum install nfs-utils rpcbind -y
$ rpm -qa nfs-utils rpcbind
nfs-utils-1.3.0-0.21.el7.x86_64
rpcbind-0.2.0-32.el7.x86_64

3.2 启动RPC服务(不需要启动NFS服务,原因是不安装NFS服务的话没有showmount这个命令)

$ systemctl start rpcbind 
$ ps -ef|grep rpc
rpc       9119     1  0 14:38 ?        00:00:00 /sbin/rpcbind -w
root      9201  6816  0 14:39 pts/0    00:00:00 grep --color=auto rpc
[root@ocdp-42-59 data]# lsof -i:111
COMMAND  PID USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
rpcbind 9119  rpc    6u  IPv4 2910094566      0t0  UDP *:sunrpc 
rpcbind 9119  rpc    8u  IPv4 2910094568      0t0  TCP *:sunrpc (LISTEN)
rpcbind 9119  rpc    9u  IPv6 2910094569      0t0  UDP *:sunrpc 
rpcbind 9119  rpc   11u  IPv6 2910094571      0t0  TCP *:sunrpc (LISTEN)

3.3 设置开机自启动并检查

$ systemctl enable rpcbind
$ systemctl list-unit-files --type=service|grep "enabled"|grep "rpcbind"

3.4 检查服务端的NFS挂载目录是否OK

$ showmount -e 10.1.236.58
Export list for 10.1.236.58:
/data/nfs-server-images 10.1.236.0/24

3.5 挂载设置开机自动挂载

$ mkdir /data/nfs-client-images
$ mount -t nfs 10.1.236.58:/data/nfs-server-images /data/nfs-client-images
$ echo "mount -t nfs 10.1.236.58:/data/nfs-server-images /data/nfs-client-images">>/etc/rc.local

#提示:也可以放入/etc/fstab,如果要放入/etc/fstab,需启用netfs服务。这是因为fstab会优先于网络被Linux系统加载。网络没启动时执行fstab会导致连不上NFS服务器端,无法实现开机自动挂载。

3.6 查看是否挂载成功

$ df -h
Filesystem                           Size  Used Avail Use% Mounted on
/dev/mapper/rootvg-lv_root            36G   25G   12G  69% /
devtmpfs                              16G     0   16G   0% /dev
tmpfs                                 16G     0   16G   0% /dev/shm
tmpfs                                 16G  1.6G   15G  10% /run
tmpfs                                 16G     0   16G   0% /sys/fs/cgroup
/dev/vda1                            509M  110M  399M  22% /boot
tmpfs                                3.2G     0  3.2G   0% /run/user/995
tmpfs                                3.2G     0  3.2G   0% /run/user/0
tmpfs                                3.2G     0  3.2G   0% /run/user/1039
/dev/vdb                             493G  185G  283G  40% /data
10.1.236.58:/data/nfs-server-images  493G  129G  339G  28% /data/nfs-client-images #我们刚挂载的

4. 验证是否可以共享

在NFS Server共享目录添加一个文件

$ ll nfs-server-images/
总用量 4
-rw-r----- 1 root root 12 1月   6 14:35 test.txt

在NFS Client共享目录查看文件已经同步

$ ll /data/nfs-client-images/
total 4
-rw-r----- 1 root root 12 Jan  6 14:35 test.txt

在NFS Client尝试删除文件

# 在2.5步我们设置共享目录权限为(ro,sync),表示只读权限,并且请求或写入数据时,数据同步写入到NFS Server的硬盘后才返回
$ rm test.txt 
rm: remove regular file ‘test.txt’? y
rm: cannot remove ‘test.txt’: Read-only file system

5. NFS权限说明

权限 说明
rw 表示可读写权限
ro 表示只读权限
sync 请求或写入数据时,数据同步写入到NFSServer的硬盘才返回(优点,数据安全不会丢,缺点,性能比不启用该参数要差)
async 写入时数据会先写到内存缓冲区,只到硬盘有空档才会再写入磁盘,这样可以提高写入效率!;风险为若服务器宕机或不正常关机,会损失缓冲区为写入磁盘的数据(解决办法:服务器主板加电池或加UPS不间断电源)!
no_root_squash 访问NFS Server共享的用户如果是root的话,它对该共享目录具有root权限。这个配置原本是为无盘客户端准备的,用户避免应用
root_squash 如果访问NFS Server共享目录的用户是root,则它的权限将被压缩或匿名用户,同时它的UID和GID通常会变成nfsnobody帐号身份
all_squash 不管访问NFS Server共享目录的用户身份如何,它的权限都将被压缩成匿名用户,同时它的UID和GID通常会变成nfsnobody帐号身份。在早期多个NFS客户端同时写入NFS Server数据时,这个参数很有用*在生产中配置NFS的重要技巧1)、确保所有客户端服务器对NFS共享目录都具备相同的用户访问权限    a、all_squash把所有客户端都压缩成固定的匿名用户(UID相同)    b、就是aninuid,anongid指定的UID和GID的用户2)、所有的客户端和服务端都需要有一个相同的UID和GID用户,即nfsnobody(UID必须相同)
anonuid 参数以anon*开头即指anonymous匿名用户,这个用户的UID设置值通常为nfsnobody的UID值,当然也可以自行设置这个UID值。但是,UID必须存在于/etc/passwd中。在多NFS Clients时,如多台Web Server共享一个NFS目录,通过这个参数可以使得不同的NFS Clients写入的数据对所有NFS Clients保持同样的用户权限,即为配置的匿名UID对应用户权限,这个参数很有用,一般默认即可
anongid 同anonuid,区别就是把uid(用户id)换成gid(组id)
noatime 在文件系统上不更新inode访问时间
nosuid 不允许设置用户标识或设置组标识为才能生效
intr 当服务器宕机或者无法到达时,允许中断NFS请求
noexec 当服务器宕机或者无法到达时,允许中断NFS请求
rsize=8192和wsize=8192 通过设定大的同时传输的数据块大小(bytes),以提高NFS读写的速度
nodiratime 不允许更新文件系统上的目录 inode 访问记录

问题解决

设置共享目录只读是防止客户端删除image

发现把目录设置为只读ro之后,NFS端无法解压tar包,或者拷贝tar包到yum repo目录

解决办法:

# 修改权限,需要no_root_squash
$ cat >/etc/exports<<EOF
#NFS server share directories
/data/nfs-server-images 10.1.236.0/24(ro,sync,no_root_squash)
EOF

$ systemctl reload nfs
这篇关于【NFS】Linux安装NFS的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!