Linux下NFS设定方法

NFS是Linux/Unix间文件共享的一种简单有效的方法,也是Kubernetes支持的文件存储方式之一。在这篇文章中我们将会详细介绍如何在CentOS7上进行NFS设定。

前提事项

由于不同发行版本的Linux或者Unix会略有不同的可能性,为方便起见,本文中NFS服务器和Client均使用CentOS。

机器用途 Hostname IP
NFS服务器 k8s-1 192.168.174.131
NFS客户端 k8s-2 192.168.174.132

软件安装、

NFS服务器详细信息

[root@ku8-1 ~]# uname -a
Linux ku8-1 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@ku8-1 ~]#
  • 1
  • 2
  • 3

rpcbind和nfs-utils

NFS服务器需要安装rpcbind和nfs-utils。

软件 版本
rpcbind rpcbind-0.2.0-38.el7.x86_64
nfs-utils nfs-utils-1.3.0-0.33.el7_3.x86_64
[root@ku8-1 ~]# rpm -qa |grep rpcbind
rpcbind-0.2.0-38.el7.x86_64
[root@ku8-1 ~]# 
[root@ku8-1 ~]# rpm -qa |grep nfs-utils
nfs-utils-1.3.0-0.33.el7_3.x86_64
[root@ku8-1 ~]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果没有安装,用如下命令安装即可

yum install -y rpcbind nfs-utils
  • 1

Client设定

Client端需要安装nfs-utlis,如果没有安装,用如下命令安装即可

yum install -y nfs-utils
  • 1

服务器设定

启动rpcbind服务

···
systemctl start rpcbind
systemctl status rpcbind
···

启动nfs服务

systemctl start nfs
systemctl status nfs
  • 1
  • 2
[root@ku8-1 ~]# systemctl start nfs
[root@ku8-1 ~]# systemctl status nfs
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: active (exited) since Tue 2017-05-02 08:28:24 EDT; 8s ago
  Process: 1436 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)
  Process: 1433 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)
  Process: 1432 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)
  Process: 2250 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 2249 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 2250 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

May 02 08:28:24 ku8-1 systemd[1]: Starting NFS server and ser....
May 02 08:28:24 ku8-1 systemd[1]: Started NFS server and serv....
Hint: Some lines were ellipsized, use -l to show in full.
[root@ku8-1 ~]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

相关文件和命令

文件名 说明
/etc/exports NFS设定文件,NFS服务器端需要设定的内容,其作用是设定Who拥有什么样的权限去访问此机器的哪个目录
/var/lib/nfs/etab 无需设定,用于纪录NFS服务器完整的权限设定,exports中没有设定的缺省值也会被列出
/var/lib/nfs/xtab 纪录NFS连接的相关信息
/usr/sbin/exportfs NFS设定管理命令,用于Server侧设定,通过此条命令使得exports的设定变得有效或者无效
/usr/sbin/showmount 用于显示NFS设定的相关信息,Server端和Client端均可。

设定exports文件

选项 说明
设定文件 /etc/exports
设定格式 共享目录 客户端IP或机器名(权限) …
[root@ku8-1 ~]# cat /etc/exports
/tmp/serverdir 192.168.174.132(rw,sync,no_root_squash) 192.168.174.133(ro,sync,no_root_squash)
[root@ku8-1 ~]# mkdir -p /tmp/serverdir
[root@ku8-1 ~]# echo "hello" >/tmp/serverdir/helloworld
[root@ku8-1 ~]# ll /tmp/serverdir/
total 4
-rw-r--r--. 1 root root 6 May  2 08:37 helloworld
[root@ku8-1 ~]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

权限选项说明

选项 说明
rw read-write,可读写;
ro read-only,只读;
sync 文件同时写入硬盘和内存;
async 文件暂存于内存,而不是直接写入内存;
no_root_squash NFS客户端root用户可具有root权限。
root_squash NFS客户端root用户也只是具有nobody或nfsnobody权限
all_squash NFS客户端均只是拥有匿名用户权限
anonuid 匿名用户的UID值。
anongid 匿名用户的GID值。

exportfs

使用exportfs命令使得修改后的exports文件起效,使用exportfs不用重启nfs服务。

步骤 执行命令
Step 1 exportfs
Step 2 showmount -e localhost

Client侧安装

yum install nfs-utils
  • 1

Client端验证

只读挂载

因为权限是ro,只读权限,所以touch命令不可以正常执行,执行的时候提示“Read-only file system”。

[root@ku8-3 ~]# showmount -e 192.168.174.131
Export list for 192.168.174.131:
/tmp/serverdir 192.168.174.133,192.168.174.132
[root@ku8-3 ~]# 
[root@ku8-3 ~]# ip addr |grep 192
    inet 192.168.174.133/24 brd 192.168.174.255 scope global ens33
[root@ku8-3 ~]# 
[root@ku8-3 ~]# mount -t nfs 192.168.174.131:/tmp/serverdir /tmp/client2
[root@ku8-3 ~]# cd /tmp/client2
[root@ku8-3 client2]# ls
helloworld
[root@ku8-3 client2]# cat helloworld 
hello
[root@ku8-3 client2]# touch ttt
touch: cannot touch ‘ttt’: Read-only file system
[root@ku8-3 client2]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

可读写挂载

因为权限是rw,所以touch命令也可以正常执行。

[root@ku8-2 ~]# mkdir -p /tmp/client
[root@ku8-2 ~]# mount -t nfs 192.168.174.131:/tmp/serverdir /tmp/client
[root@ku8-2 ~]# cd /tmp/client/
[root@ku8-2 client]# df .
Filesystem                     1K-blocks    Used Available Use% Mounted on
192.168.174.131:/tmp/serverdir  17811456 2549888  15261568  15% /tmp/client
[root@ku8-2 client]# ls
helloworld
[root@ku8-2 client]# cat helloworld 
hello
[root@ku8-2 client]# touch ttt
[root@ku8-2 client]# ls
helloworld  ttt
[root@ku8-2 client]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Server端确认

[root@ku8-1 ~]# cd /tmp/serverdir
[root@ku8-1 serverdir]# ls
helloworld  ttt
[root@ku8-1 serverdir]# 
  • 1
  • 2
  • 3
  • 4

另外一个Client端确认

[root@ku8-3 client2]# pwd
/tmp/client2
[root@ku8-3 client2]# ls 
helloworld  ttt
[root@ku8-3 client2]# 
  • 1
  • 2
  • 3
  • 4
  • 5

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自www.cnblogs.com/firsttry/p/10294460.html