Ubuntu配置NFS

xiaoxiao2021-02-28  175

NFS 是Network File System的缩写,即网络文件系统,一种使用于分散式文件系统的协定

NFS通过网络让不同的机器、不同的操作系统能够彼此分享个别的数据,例如将/home设置为共享目录,然后共享给网络中的其它服务器,这样每台机器就不必单独建立自己的/home路径了,NFS独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享,是在类Unix系统间实现磁盘文件共享的一种方法

NFS本身没有提供信息传输的协议和功能,而是使用RPC服务完成这部分功能。可以说NFS本身就是使用RPC服务的一个程序,或者说NFS也是一个RPC SERVER。所以只要用到NFS的地方都要启动RPC服务,不论是NFS server还是NFS client,可以这么理解RPC和NFS之间的关系:NFS是一个文件系统,而RPC负责信息的传输

1. 系统环境 ubuntu 16.04

2. 安装NFS

因为我已经安装好了,所以提示已安装,新安装的话通过安装日志可以发现总共会安装keyutils,libgssglue1,libnfsidmap2,libtirpc1,nfs-common,nfs-kernel-server,rpcbind这7个包。

zhang@pp:~$ sudo apt-get install nfs-kernel-server sudo: 无法解析主机:pp [sudo] zhang 的密码: 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 nfs-kernel-server 已经是最新版 (1:1.2.8-9ubuntu12.1)。

很多文档中安装的包为portmap,但是这个包已经被rpcbind替代

zhang@pp:~$ sudo apt-get install portmap sudo: 无法解析主机:pp 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 注意,选中 'rpcbind' 而非 'portmap' rpcbind 已经是最新版 (0.2.3-0.2)。

rpcbind包安装完成后会自动启动rpcbind服务

zhang@pp:~$ ps -ef|grep rpcbind root 991 1 0 09:07 ? 00:00:00 /sbin/rpcbind -f -w zhang 4284 4250 0 10:00 pts/11 00:00:00 grep --color=auto rpcbind

但是由于目前NFS的配置文件为空,NFS服务并没有启动

3. 配置NFS服务

从安装日志中我们可以发现NFS服务的配置文件为/etc/exports,并且这个文件在安装过程中已经生成好了,我们所要做的就是将NFS配置信息添加到这个文件中

root@pp:~# vim /etc/exports # /etc/exports: the access control list for filesystems which may be exported # to NFS clients. See exports(5). # # Example for NFSv2 and NFSv3: # /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check) # # Example for NFSv4: # /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check) # /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check) /home/share 192.168.137.129(rw,no_root_squash,async)

其中中的最后一行为新添加的NFS配置,NFS配置信息格式如下:

<共享目录> [客户端1 选项(访问权限,用户映射,其他)] [客户端2 选项(访问权限,用户映射,其他)] 1.共享目录: 共享目录是指NFS系统中需要共享给客户机使用的目录 2.客户端: 客户端是指网络中可以访问NFS共享目录的计算机 *客户端常用的指定方式: (1)指定ip地址的主机:192.168.0.1 (2)指定子网中的所有主机:192.168.0.0/255.255.255.0 (3)指定域名的主机:www.sean.com (4)指定域中的所有主机:*.sean.com (5)所有主机:* 3.选项: 选项用来设置输出目录的访问权限、用户映射等,NFS主要有3类选项: *访问权限选项: (1)设置输出目录只读:ro (2)设置输出目录读写:rw *用户映射选项: (1)all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody) (2)no_all_squash:与all_squash取反(默认设置) (3)root_squash:将root用户及所属组都映射为匿名用户或用户组(默认设置) (4)no_root_squash:与rootsquash取反 (5)anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx) (6)anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx) *其它选项: (1)secure:限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置) (2)insecure:允许客户端从大于1024的tcp/ip端口连接服务器 (3)sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性 (4)async:将数据先保存在内存缓冲区中,必要时才写入磁盘 (5)wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置) (6)no_wdelay:若有写操作则立即执行,应与sync配合使用 (7)subtree:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置) (8)no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率

4.启动NFS服务

root@pp:~# service nfs-kernel-server start root@pp:~# sudo service nfs-kernel-server status sudo: 无法解析主机:pp ● nfs-server.service - NFS server and services Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor prese Active: active (exited) since 六 2017-08-05 09:07:16 CST; 1h 14min ago Process: 1002 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0 Process: 997 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS Main PID: 1002 (code=exited, status=0/SUCCESS) CGroup: /system.slice/nfs-server.service ......

通过命令可以看到文件夹已经被共享

root@pp:~# ps -ef|grep nfsd root 1007 2 0 09:07 ? 00:00:00 [nfsd] root 1008 2 0 09:07 ? 00:00:00 [nfsd] root 1009 2 0 09:07 ? 00:00:00 [nfsd] root 1010 2 0 09:07 ? 00:00:00 [nfsd] root 1011 2 0 09:07 ? 00:00:00 [nfsd] root 1012 2 0 09:07 ? 00:00:00 [nfsd] root 1013 2 0 09:07 ? 00:00:00 [nfsd] root 1014 2 0 09:07 ? 00:00:00 [nfsd] root 4526 4296 0 10:22 pts/11 00:00:00 grep --color=auto nfsd root@pp:~# showmount -e Export list for sean: /home/share 192.168.1.106

5.客户端挂载NFS共享目录

zhang@pp:~$ sudo mount 192.168.137.128:/home/sean/shareDir /home/sean/shared mount: wrong fs type, bad option, bad superblock on 192.168.137.128:/home/sean/shareDir, missing codepage or helper program, or other error (for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program) In some cases useful info is found in syslog - try dmesg | tail or so

上网查看了一下错误原因,是因为客户端没有安装nfs-common包

zhang@pp:~$ sudo apt-get install nfs-common

之后再次使用挂载命令即可,此时如果我们再次查看客户端挂载的磁盘,就可以看到NFS共享目录已经被挂载上了

zhang@pp:~$ df Filesystem 1K-blocks Used Available Use% Mounted on udev 488800 4 488796 1% /dev tmpfs 99904 1456 98448 2% /run . . . 192.168.137.128:/home/share 19478272 4288896 14176896 24% /home/share

此时如果我们在128的/home/sean/shareDir目录下创建一个文件,在129的/home/sean/shared目录下可以看到一个名称相同的文件,并且文件的内容也相同

6.开机启动NFS服务 安装完成之后查看系统服务

zhang@pp:~$ runlevel N 2 sean@sean:~$ ll /etc/rc2.d total 20 drwxr-xr-x 2 root root 4096 620 23:25 ./ drwxr-xr-x 132 root root 12288 626 20:16 ../ -rw-r--r-- 1 root root 677 217 12:59 README lrwxrwxrwx 1 root root 20 222 06:33 S20kerneloops -> ../init.d/kerneloops* ......

nfs-kernel-server是自动启动,就不需要再进行修改了,而rpcbind就比较麻烦了,找了半天才找到vi /etc/init/rpcbind-boot.conf

# portmap-boot description "Upstart job to start rpcbind on boot only" author "Clint Byrum" emits start-rpcbind start on virtual-filesystems and net-device-up IFACE=lo task exec initctl emit --no-wait start-rpcbind ON_BOOT=y

可以见到这个服务设为开机启动了

7.开机自动挂载共享盘 这时候我们需要修改的文件是/etc/fstab,命令格式如下:

<NFS服务端>:<共享目录> <本地目录> <文件系统类型> <选项> <dump> <pass>

每个字段的具体含义这里就不详细解释了,vi /etc/fstab,在文件中添加下面这行配置

192.168.137.128:/home/share /home/share nfs defaults 0 0

系统每次启动时即会自动挂载共享盘.

转载请注明原文地址: https://www.6miu.com/read-20289.html

最新回复(0)