第14章 NFS服务搭建与配置
14.1 NFS介绍
NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。NFS的数据传输基于RPC(remote procedure call)协议。
应用场景
A,B,C三台机器上需要被访问到的文件是一样的,A共享数据出来,B和C分别取挂载A共享的数据目录,从而B和C访问到的数据和A上的一致。
14.2 NFS服务端安装配置
准备两台虚拟机,一台作为服务端,一台作为客户端。
服务端192.168.230.135
1[root@cham002 ~]# yum install -y nfs-utils rpcbind 2 3 4[root@cham002 ~]# vim /etc/exports 5/home/nfstestdir 192.168.230.145/24(rw,sync,all_squash,anonuid=1000,anongid=1000) 6#指定要进行分享的目录;指定要共享该目录的机器,ip可以ip段,也可以写ip。 7 8:wq 9 10创建分享目录并制定权限: 11[root@cham002 ~]# mkdir /home/nfstestdir 12[root@cham002 ~]# chmod 777 /home/nfstestdir 13 14安装完rpcbind之后服务会自动启动 15[root@cham002 ~]# netstat -lnpt 16Active Internet connections (only servers) 17Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 18tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 11547/rpcbind 19tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6019/nginx: master 20tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1913/sshd 21tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2498/master 22tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 6019/nginx: master 23tcp6 0 0 :::3306 :::* LISTEN 9831/mysqld 24tcp6 0 0 :::111 :::* LISTEN 11547/rpcbind 25tcp6 0 0 :::22 :::* LISTEN 1913/sshd 26tcp6 0 0 ::1:25 :::* LISTEN 2498/master 27 28[root@cham002 ~]# ps aux |grep rpc 29rpc 11547 0.0 0.1 64964 1044 ? Ss 22:57 0:00 /sbin/rpcbind -w 30root 11700 0.0 0.0 112680 976 pts/2 S+ 23:16 0:00 grep --color=auto rpc 31 32 33 34
启动NFS服务
1[root@cham002 ~]# systemctl start nfs 2[root@cham002 ~]# ps aux |grep nfs 3root 11740 0.0 0.0 0 0 ? S< 23:17 0:00 [nfsd4_callbacks] 4root 11746 0.0 0.0 0 0 ? S 23:17 0:00 [nfsd] 5root 11747 0.0 0.0 0 0 ? S 23:17 0:00 [nfsd] 6root 11748 0.0 0.0 0 0 ? S 23:17 0:00 [nfsd] 7root 11749 0.0 0.0 0 0 ? S 23:17 0:00 [nfsd] 8root 11750 0.0 0.0 0 0 ? S 23:17 0:00 [nfsd] 9root 11751 0.0 0.0 0 0 ? S 23:17 0:00 [nfsd] 10root 11752 0.0 0.0 0 0 ? S 23:17 0:00 [nfsd] 11root 11753 0.0 0.0 0 0 ? S 23:17 0:00 [nfsd] 12root 11770 0.0 0.0 112680 972 pts/2 S+ 23:17 0:00 grep --color=auto nfs 13[root@cham002 ~]# ps aux |grep rpc 14rpc 11547 0.0 0.1 64964 1304 ? Ss 22:57 0:00 /sbin/rpcbind -w 15root 11723 0.0 0.0 0 0 ? S< 23:17 0:00 [rpciod] 16root 11728 0.0 0.0 43824 544 ? Ss 23:17 0:00 /usr/sbin/rpc.idmapd 17root 11729 0.0 0.0 42564 944 ? Ss 23:17 0:00 /usr/sbin/rpc.mountd 18root 11772 0.0 0.0 112680 972 pts/2 S+ 23:18 0:00 grep --color=auto rpc 19 20将NFS服务加入开机启动项: 21[root@cham002 ~]# systemctl enable nfs 22Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
客户端192.168.230.145
1[root@test ~]# yum install -y nfs-utils 2[root@test ~]# netstat -lntp 3Active Internet connections (only servers) 4Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 5tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd 6tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 38651/nginx: master 7tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1337/sshd 8tcp6 0 0 :::3306 :::* LISTEN 38908/mysqld 9tcp6 0 0 :::111 :::* LISTEN 1/systemd 10tcp6 0 0 :::22 :::* LISTEN 1337/sshd 11[root@test ~]# ps aux |grep rpc 12rpc 61559 0.0 0.1 64964 1048 ? Ss 22:58 0:00 /sbin/rpcbind -w 13root 61727 0.0 0.0 112684 980 pts/0 R+ 23:16 0:00 grep --color=auto rpc 14 15检查客户端是否有权限访问服务端文件: 16[root@test ~]# showmount -e 192.168.230.135 17clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host) 18报错: 无法连接到服务端机器(网络不通)! 19 20解决办法: 21 22检查服务端NFS服务是否开启(监听111端口) 23如果确认服务端NFS服务已经开启,那么检查防火墙状态,关闭服务端和客户端firewalld和SELinux防火墙 24192.168.230.145 25[root@test ~]# systemctl stop firewalld 26[root@test ~]# getenforce 27Enforcing 28[root@test ~]# setenforce 0 29[root@test ~]# getenforce 30Permissive 31192.168.230.135 32[root@cham002 ~]# systemctl stop firewalld 33[root@cham002 ~]# systemctl stop firewalld 34[root@cham002 ~]# systemctl disable iptables 35Removed symlink /etc/systemd/system/basic.target.wants/iptables.service. 36[root@cham002 ~]# systemctl stop iptables 37 38解决完上述错误后再次执行命令: 39[root@test ~]# showmount -e 192.168.230.135 40Export list for 192.168.230.135: 41/home/nfstestdir 192.168.230.0/24 42即,客户端可以正常访问服务端机器,也可以看到共享的目录了。
开始挂载
1[root@test ~]# mount -t nfs 192.168.230.135:/home/nfstestdir /mnt/ 2[root@test ~]# df -h 3文件系统 容量 已用 可用 已用% 挂载点 4/dev/sda3 17G 8.5G 7.7G 53% / 5devtmpfs 483M 0 483M 0% /dev 6tmpfs 493M 0 493M 0% /dev/shm 7tmpfs 493M 6.8M 486M 2% /run 8tmpfs 493M 0 493M 0% /sys/fs/cgroup 9/dev/sda1 197M 109M 88M 56% /boot 10tmpfs 99M 0 99M 0% /run/user/0 11192.168.230.135:/home/nfstestdir 17G 7.0G 9.2G 44% /mnt 12[root@test ~]# 13 14192.168.230.145 15[root@test ~]# cd /mnt/ 16[root@test mnt]# ls 17testdir 18[root@test mnt]# touch champinlinux.111 19[root@test mnt]# ls -l 20总用量 0 21-rw-r--r-- 1 cham cham 0 1月 17 00:03 champinlinux.111 22drwxr-xr-x 2 root root 6 1月 17 00:01 testdir 23 24[root@test mnt]# id cham 25uid=1000(cham) gid=1000(cham) 组=1000(cham),1007(user5) 26 27 28 29192.168.230.135 30 31[root@cham002 ~]# ls -l /home/nfstestdir/ 32总用量 0 33-rw-r--r-- 1 user1 user1 0 1月 17 00:03 champinlinux.111 34drwxr-xr-x 2 root root 6 1月 17 00:01 testdir 35[root@cham002 ~]# id user1 36uid=1000(user1) gid=1000(user1) 组=1000(user1) 37 38 39 40 41
NFS配置选项
• rw 读写
• ro 只读
• sync 同步模式,内存数据实时写入磁盘
• async 非同步模式
• no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
• root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
• all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
• anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid
14.4 exportfs命令
exportfs命令用来管理当前NFS共享的文件系统列表。
• exportfs -arv //不用重启nfs服务,配置文件就会生效
192.168.230.145把它先卸载掉然后再重启nfs, 如果挂载了几十台机器每一台都要去卸载么?
1[root@test ~]# umount /mnt 2umount.nfs4: /mnt: device is busy 3[root@test ~]# df -h 4文件系统 容量 已用 可用 已用% 挂载点 5/dev/sda3 17G 8.5G 7.7G 53% / 6devtmpfs 483M 0 483M 0% /dev 7tmpfs 493M 0 493M 0% /dev/shm 8tmpfs 493M 6.8M 486M 2% /run 9tmpfs 493M 0 493M 0% /sys/fs/cgroup 10/dev/sda1 197M 109M 88M 56% /boot 11tmpfs 99M 0 99M 0% /run/user/0 12192.168.230.135:/home/nfstestdir 17G 7.0G 9.2G 44% /mnt 13[root@test ~]# umount -l /mnt 14[root@test ~]# df -h 15文件系统 容量 已用 可用 已用% 挂载点 16/dev/sda3 17G 8.5G 7.7G 53% / 17devtmpfs 483M 0 483M 0% /dev 18tmpfs 493M 0 493M 0% /dev/shm 19tmpfs 493M 6.8M 486M 2% /run 20tmpfs 493M 0 493M 0% /sys/fs/cgroup 21/dev/sda1 197M 109M 88M 56% /boot 22tmpfs 99M 0 99M 0% /run/user/0
•常用选项
• -a 全部挂载或者全部卸载
• -r 重新挂载
• -u 卸载某一个目录
• -v 显示共享目录
• 以下操作在服务端上
•vim /etc/exports //增加
/tmp/ 192.168.230.0/24(rw,sync,no_root_squash)
192.168.230.135
1[root@cham002 ~]# exportfs -arv 2exporting 192.168.230.0/24:/home/nfstestdir 3 4 5验证一下 6[root@cham002 ~]# vim /etc/exports 7/home/nfstestdir 192.168.230.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) 8/tmp 192.168.230.145 (rw,sync,no_root_squash) 9 10[root@cham002 ~]# exportfs -arv 11exportfs: No options for /tmp 192.168.230.145: suggest 192.168.230.145(sync) to avoid warning 12exportfs: No host name given with /tmp (rw,sync,no_root_squash), suggest *(rw,sync,no_root_squash) to avoid warning 13exporting 192.168.230.145:/tmp 14exporting 192.168.230.0/24:/home/nfstestdir 15exporting *:/tmp 16 17192.168.230.145 18[root@test ~]# showmount -e 192.168.230.135 19Export list for 192.168.230.135: 20/home/nfstestdir 192.168.230.0/24 21/tmp (everyone) 22[root@test ~]# mount -t nfs 192.168.230.135:/tmp/ /mnt/ 23[root@test ~]# df -h 24文件系统 容量 已用 可用 已用% 挂载点 25/dev/sda3 17G 8.5G 7.7G 53% / 26devtmpfs 483M 0 483M 0% /dev 27tmpfs 493M 0 493M 0% /dev/shm 28tmpfs 493M 6.8M 486M 2% /run 29tmpfs 493M 0 493M 0% /sys/fs/cgroup 30/dev/sda1 197M 109M 88M 56% /boot 31tmpfs 99M 0 99M 0% /run/user/0 32192.168.230.135:/tmp 17G 7.0G 9.2G 44% /mnt 33[root@test ~]# ls /mnt 34champ.sock test.com.log-20180108 35mysql2.sql test.com.log-20180109 36mysql_all.sql test.com.log-20180110 37mysqlbak180116.sql test.com.log-20180112 38mysql.sock test.com.log-20180115 39php-fcgi.sock test.com.log-20180116 40systemd-private-4045549bb7d44cbd9149ab7506b1c43c-vmtoolsd.service-iF9vYt user.sql 41test.com.log 42[root@test ~]# vim 123.txt 43123 44[root@test ~]# ls -l /mnt/ 45总用量 1964 46-rw-r--r-- 1 root root 16 1月 18 08:09 1212.txt 47srw-rw-rw- 1 root root 0 1月 16 23:48 champ.sock 48-rw-r--r-- 1 root root 30848 1月 16 00:33 mysql2.sql 49-rw-r--r-- 1 root root 1306193 1月 16 00:29 mysql_all.sql 50-rw-r--r-- 1 root root 653300 1月 16 00:14 mysqlbak180116.sql 51srwxrwxrwx 1 user5 user5 0 1月 16 23:48 mysql.sock 52srw-rw-rw- 1 root root 0 1月 16 23:48 php-fcgi.sock 53drwx------ 3 root root 17 1月 16 23:48 systemd-private-4045549bb7d44cbd9149ab7506b1c43c-vmtoolsd.service-iF9vYt 54-rw-r--r-- 1 root root 0 1月 17 00:00 test.com.log 55-rw-r--r-- 1 root root 0 1月 5 00:00 test.com.log-20180108 56-rw-r--r-- 1 root root 1583 1月 9 02:21 test.com.log-20180109 57-rw-r--r-- 1 root root 0 1月 10 00:00 test.com.log-20180110 58-rw-r--r-- 1 root root 0 1月 11 00:00 test.com.log-20180112 59-rw-r--r-- 1 root root 0 1月 13 00:00 test.com.log-20180115 60-rw-r--r-- 1 root root 0 1月 16 00:00 test.com.log-20180116 61-rw-r--r-- 1 root root 7002 1月 16 00:22 user.sql 62 63192.168.230.135 64[root@cham002 ~]# ls -l /mnt/ 65总用量 4 66-rw-r--r-- 1 root root 16 11月 6 14:12 23.txt 67[root@cham002 ~]# ls -l /tmp/ 68总用量 1964 69-rw-r--r-- 1 root root 16 1月 18 08:09 1212.txt 70srw-rw-rw- 1 root root 0 1月 16 23:48 champ.sock 71-rw-r--r-- 1 root root 30848 1月 16 00:33 mysql2.sql 72-rw-r--r-- 1 root root 1306193 1月 16 00:29 mysql_all.sql 73-rw-r--r-- 1 root root 653300 1月 16 00:14 mysqlbak180116.sql 74srwxrwxrwx 1 mysql mysql 0 1月 16 23:48 mysql.sock 75srw-rw-rw- 1 root root 0 1月 16 23:48 php-fcgi.sock 76drwx------ 3 root root 17 1月 16 23:48 systemd-private-4045549bb7d44cbd9149ab7506b1c43c-vmtoolsd.service-iF9vYt 77-rw-r--r-- 1 root root 0 1月 17 00:00 test.com.log 78-rw-r--r-- 1 root root 0 1月 5 00:00 test.com.log-20180108 79-rw-r--r-- 1 root root 1583 1月 9 02:21 test.com.log-20180109 80-rw-r--r-- 1 root root 0 1月 10 00:00 test.com.log-20180110 81-rw-r--r-- 1 root root 0 1月 11 00:00 test.com.log-20180112 82-rw-r--r-- 1 root root 0 1月 13 00:00 test.com.log-20180115 83-rw-r--r-- 1 root root 0 1月 16 00:00 test.com.log-20180116 84-rw-r--r-- 1 root root 7002 1月 16 00:22 user.sql 85 86
说明:通常情况下,不限制root的情况多,就是no_root_squash
14.5 NFS客户端问题
针对NFS4版本在centos6中应用存在如下问题:
客户端挂载共享目录后,不管是root用户还是普通用户,创建新文件时属主、数组为nobody。
解决方法:
-
方法1:在客户端进行挂载时加上选项-o nfsvers=3
[root@test ~]# mount -t nfs -o nfsvers=3 192.168.230.135:/tmp/ /mnt/ 如果目录已经挂载,而又不想卸载,执行如下命令: [root@test ~]# mount -t nfs -oremount,nfsvers=3 192.168.230.135:/tmp/ /mnt/
-
方法2:客户端和服务端都需要
•客户端和服务端都需要
• vim /etc/idmapd.conf //
• 把“#Domain = local.domain.edu” 改为 “Domain = xxx.com” (这里的xxx.com,随意定义吧),然后再重启rpcidmapd服务(cenos7 是没有这个服务的,重启rpcbind就行)
