4、nfs服务器的搭建


4.1、nfs服务介绍:

samba服务器一般互联网企业不会使用

nfs服务的端口是不固定的,需要先启动rpc服务对nfs服务端口进行注册

4.2、安装nfs:

rpm -qa nfs-utils rpcbind

yum install nfs-utils rpcbind -y

另一种安装的方法:

yum grouplist “NFS file server”-y

rpm -qa nfs-utils rpcbind

4.3、配置nfs文件:

配置参数

rw:表示可以读写的权限

sync:请求或写入数据时,数据同步写入到nfsServer磁盘后才能返回,用于小并发;

优点:数据安全不会丢失;缺点:性能比不启动改参数差;

async:写入数据时会先写入缓冲区,直到硬盘有 空挡再写入磁盘,用于大并发;

优点:写入效率高; 缺点:若服务器宕机或不正常关机会导致会导致缓冲区中的数据丢失;

vim /etc/exports

#shared /data/ nfs

#要共享的目录,哪些网段可以访问该共享

/data 172.16.1.0/24(rw,sync) #只对root进行其他用户权限的压缩,其它用户不压缩(不能访问)

4.4、创建nfs共享目录:

用户访问nfs服务时,使用的是nfsnobody系统虚拟用户进行访问,这是当yum安装时自动生成的用户;

mkdir /data

chown -R nfsnobody:nfsnobody /data/

ls -ld /data/

drwxr-xr-x 2 nfsnobody nfsnobody 4096 10月 9 10:34 /data/

4.5、启动rpc和nfs服务:

/etc/init.d/rpcbind start

/etc/init.d/rpcbind status

netstat -tunlp | grep "rpcbind"

rpc服务端口号为111

/etc/inirt.d/nfs start

/etc/init.d/nfs status

4.5.1、查看rpc是否有注册的nfs端口:

rpcinfo -p loclhost

可以看到nfs注册的端口号为2049

4.5.2、如果重新配置了 /etc/exports文件,只需要平滑重启nfs服务:

/etc/init.d/nfs reload

4.6、检查配置的文件是否生效:

[root@nfs01 ~]# cat /var/lib/nfs/etab

/data 172.16.1.0/24(rw,syncoc,wdelay,hide,nrossmnt,secure,root_squash

,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,root_squash,no_all_squash)

[root@nfs01 ~]# showmount -e 172.16.1.31

Export list for 172.16.1.31:

/data 172.16.1.0/24

4.7、设置rpc和nfs开机自启动:

chkconfig rpcbind on

chkconfig nfs on

4.8、查看nfs和rpc相关的进程:

[root@nfs01 ~]# ps -ef | egrep "nfs|rpc" | grep -v "grep"

rpc 1331 1 0 09:54 ? 00:00:00 rpcbind

rpcuser 1353 1 0 09:54 ? 00:00:00 rpc.statd #检查文件的一致性

root 1403 2 0 09:54 ? 00:00:00 [rpciod/0]

root 1412 1 0 09:54 ? 00:00:00 rpc.rquotad#磁盘配额进程

root 1417 1 0 09:54 ? 00:00:00 rpc.mountd#权限管理验证

root 1424 2 0 09:54 ? 00:00:00 [nfsd4]

root 1425 2 0 09:54 ? 00:00:00 [nfsd4_callbacks]

root 1426 2 0 09:54 ? 00:00:00 [nfsd] #nfs 主进程

root 1427 2 0 09:54 ? 00:00:00 [nfsd]

root 1428 2 0 09:54 ? 00:00:00 [nfsd]

root 1429 2 0 09:54 ? 00:00:00 [nfsd]

root 1430 2 0 09:54 ? 00:00:00 [nfsd]

root 1431 2 0 09:54 ? 00:00:00 [nfsd]

root 1432 2 0 09:54 ? 00:00:00 [nfsd]

root 1433 2 0 09:54 ? 00:00:00 [nfsd]

root 1462 1 0 09:54 ? 00:00:00 rpc.idmapd

4.9、nfs客户端:

yum install nfs-utils rpcbind -y

nfs客户端只需要启动rpc服务,根据不同的系统版本可以不装nfs,最好是装上比较好,nfs服务不需要启动;

rpm -qa nfs-utils rpcbind

/etc/init.d/rpcbind start

/etc/init.d/rpcbind status

chkconfig --list rpcbind on #开机自启动

查看是否能连上服务器端

[root@web01 ~]# showmount -e 172.16.1.31

Export list for 172.16.1.31:

/data 172.16.1.0/24

4.9.1、在客户端挂载服务器:

mount -t nfs 172.16.1.31:/data /mnt/ #重启会失效;

通常情况下不能在/etc/fstab配置自动挂载,是因为在linux中是先启动挂载硬盘的服务再启动网络服务的通常会报错;

但是如果在开机自启动服务里设置并启动了netfs服务,放入fstab里也是可以开机自动挂载的。

设置netfs服务(先启动网络再启动/etc/fstab):

[root@web01 lc]# /etc/init.d/netfs start

Mounting filesystems: [确定]

[root@web01 lc]# chkconfig netfs on

[root@web01 lc]# chkconfig --list netfs

netfs 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

proc /proc proc defaults 0 0

172.16.1.31:/data/ /mnt/ nfs defaults 0 0

放到/etc/rc.local中进行开机自启动:

此挂载不会受操作系统先挂载硬盘再启动网络的影响;

/bin/mount -t nfs 172.16.1.31:/data /mnt/

4.9.2、查看挂载情况:

df -hT

172.16.1.31:/data/ nfs 57G 1.5G 53G 3% /mnt

cat /proc/mounts

172.16.1.31:/data/ /mnt nfs4 rw,relatime,vers=4,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,

port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.8,minorversion=0,local_lock=none,addr=172.16.1.31 0 0

4.10、常见错误:

selinux没关

iptables没关(telnet登录测试)

客户端没有安装nfs-utils

showmount -e 不能使用

服务器端的共享目录没有进行授权

程序未注册

rpc和nfs的启动顺序不对

nfs排错

ping nfsserver的ip地址

telnet nfsserver_ip 111

showmount -e 172.16.1.31

4.11、nfs服务器自己搭建:

客户端用户的uid,gid和服务端的gid uid必须保持一致性,权限的一致性,这样不会出现文件权限无法访问的问题,用户安装nfs-utils后默认的是nfsnobody

4.11.1、服务器端:

(1)安装 nfs-utils rpcbind 包;

(2)创建虚拟系统用户:

useradd -u 888 -Ms /sbin/nologin web01

ls -l /etc/passwd

web01:x:888:888::/home/web01:/sbin/nologin

(3)配置服务参数:

vim /etc/exports

/data1 172.16.1.0/24(rw,sync,all_squash,anonuid=888,anongid=888) #压缩所有用户的权限

(4)创建目录:

mkdir /data1

chown -R web01:web01 /data1

(5)启动服务:

/etc/init.d/rpcbind

/etc/init.d/rpcbind status

/etc/init.d/nfs start

/etc/init.d/nfs status

rpcinfo -p localhost

(6)查看配置文件:

showmount -e 172.16.1.31

cat /var/lib/nfs/etba

4.11.2、客户端配置:

(1)安装nfs-utils,rpcbind安装包;

(2)验证服务是否开启:

showmount -e 172.16.1.31

(3)创建虚拟系统用户

useradd -u 888 -Ms /sbin/nologin web01

ls -l /etc/passwd

web01:x:888:888::/home/web01:/sbin/nologin

(4)创建挂载目录:

mkdir /mnt1

(5)挂接:

mount -t nfs 172.16.1.31:/data1/ /mnt1/

(6)查看挂接情况:

df -hT

cat /proc/mounts

172.16.1.31:/data1/ /a nfs4 rw,relatime,vers=4,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,

timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.8,minorversion=0,local_lock=none,addr=172.16.1.31 0 0

4.11.3、测试:

(1)客户端情况:

(2)服务端情况:

4.12、总结:

创建虚拟系统用户的目的是为了更好的屏蔽不同用户的权限,所有的操作都在这个虚拟用户上进行操作,方便,而且用户的权限是775(文件夹),664(文件),需要对虚拟系统用户进行权限的设定umask 022这样创建出的文件就是644,文件夹是755的权限,就会更安全(linux一切皆权限)。

在root用户下配置,实现644,755的文件系统就好

rsync:读取能读取的文件赋权;

nfs:赋予用户什么样的权限进行;


































猜你喜欢

转载自www.cnblogs.com/LiuChang-blog/p/12313814.html