https://www.cnblogs.com/merely/p/10793877.html

https://help.aliyun.com/document_detail/90529.html

https://support.huaweicloud.com/intl/zh-cn/qs-sfs/zh-cn_topic_0034428728.html

https://blog.csdn.net/kevinhg/article/details/5967432

#######################################################################################################################################

1. 安装:

apt-get install -y nfs-kernel-server

2. 创建共享目录,并添加配置:

mkdir -p /nfs/share

在/etc/exports的末尾追加一行:/nfs/share  *(rw,sync,no_subtree_check,no_root_squash)

3. nfs是一个RPC程序,第1步安装成功后,使用它时需要映射提前映射好端口,映射端口,通过rpcbind设定

systemctl status rpcbind

4. 重启nfs-server:

systemctl restart nfs-server && systemctl enable nfs-server

systemctl status nfs-server

5. 挂载上面的共享
在/etc/fstab添加
192.168.22.22:/nfs/share /nfsfile   nfs rw,tcp,soft 0 0

如挂载到本机,添加共享文件:
echo 1111 > /nfs/share/1.txt
echo 2222 > /nfs/share/2.txt

挂载mount -a

ll /nfsfile
total 16
-rw-r--r-- 1 root root 5 Sep 15 08:49 1.txt
-rw-r--r-- 1 root root 5 Sep 15 08:49 2.txt

查看挂载状态

nfsstat -m
/nfsfile from 192.168.151.4:/nfs/share
Flags: rw,relatime,vers=4.2,rsize=524288,wsize=524288,namlen=255,soft,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.151.4,local_lock=none,addr=192.168.151.4

取消挂载 umount /nfsfile

注:redhat系linux中,低版本启动nfs方式为

/etc/rc.d/init.d/portmap start (在REDHAT中PORTMAP是默认启动的)
/etc/rc.d/init.d/nfs start

#######################################################################################################################################

https://blog.51cto.com/foolishfish/1372770  nfs服务器的配置以及fstab参数描述

nfs服务器的配置,配置文件/etc/exports:

ro 该主机对该共享目录有只读权限
rw 该主机对该共享目录有读写权限
root_squash 客户机用root用户访问该共享文件夹时,将root用户映射成匿名用户
no_root_squash 客户机用root访问该共享文件夹时,不映射root用户
all_squash 客户机上的任何用户访问该共享目录时都映射成匿名用户
anonuid 将客户机上的用户映射成指定的本地用户ID的用户
anongid 将客户机上的用户映射成属于指定的本地用户组ID
sync 资料同步写入到内存与硬盘中
async 资料会先暂存于内存中,而非直接写入硬盘
insecure 允许从这台机器过来的非授权访问

例子:
/ zhang (rw) wang (rw,no_root_squash)

表示共享服务器上的根目录(/)只有zhang和wang两台主机可以访问,且有读写权限;

zhang主机用root用户身份访问时,将客户机的root用户映射成服务器上的匿名用户(root_squash,该参数为缺省参数),相当于在服务器使用nobody用户访问目录;

wang主机用root用户身份访问该共享目录时,不映射root用户(no_root_squash),即相当于在服务器上用root身份访问该目录;

/root/share/ 192.168.1.20 (rw,insecure,sync,all_squash)   # 可以指定模糊匹配,如 192.168.0.*(rw)  *(ro)

表示共享服务器上的/root/share/目录只有192.168.1.20主机可以访问,且有读写权限;

此主机用任何身份访问时,将客户机的用户都映射成服务器上的匿名用户(all_squash),相当于在服务器上用nobody用户访问该目录(若客户机要在该共享目录上保存文件(即写操作),则服务器上的nobody用户对该目录必须有写的权限)

/home/ljm/ *.gdfs.edu.cn (rw,insecure,sync,all_squash)

表示共享/home/ljm/目录,*.gdfs.edu.cn域中所有的主机都可以访问该目录,且有读写权限

/home/share/ .gdfs.edu.cn (ro,sync,all_squash,anonuid=student,anongid=math)

表示共享目录/home/share/,*.gdfs.edu.cn域中的所有主机都可以访问,但只有只读的权限,所有用户都映射成服务器上的uid为student、gid为math的用户

启动nfs后又修改了/etc/exports,不用重启该服务,使用exports命令即可:

exportfs [-aruv]
-a 全部mount或umount文件/etc/exports中的内容
-r 重新mount文件/etc/exports中的共享内容
-u umount目录
-v 在export的时候,将详细的信息输出到屏幕上

例:
[root@localhost ~]# /usr/sbin/exportfs -rv   # 全部重新export一次

[root@localhost ~]# /usr/sbin/exportfs -au   # 全部卸载


nfs客户端的配置:
若是临时使用可直接执行mount命令:mount servername(or IP):共享目录 本地挂载目录
若客户机启动就自动挂载服务器的共享目录,则需修改客户机上的/etc/fstab文件

/etc/fstab格式(6个字段):
192.168.233.139:/share  /mnt  nfs  defaults  0 2
各个字段含义:fs_spec  fs_file  fs_type  fs_options  fs_dump  fs_pass
fs_spec:定义希望加载的文件系统所在的设备或远程文件系统,对于nfs则设为IP:/共享目录
fs_file:本地挂载点
fs_type:挂载类型
fs_options:挂载参数
fs_dump:该选项被“dump”命令使用来检查一个文件系统该以多快频率进行转储,若不需转储即为0
fs_pass:该字段被fsck命令使用来决定在启动时需要被扫描的文件系统的顺序,根文件系统“/”对应该字段值为1,其他文件系统为2,若该文件系统无需在启动时被扫描则为0
 

[root@localhost ~]#vi /etc/hosts.allow
Portmap:192.168.5.123:allow
[root@localhost ~]#vi /etc/hosts.deny
Portmap:ALL:deny

安全提醒:确保网络安全,使用nfs时结合tcp_wrappers来限制使用范围(如只想192.168.5.123主机可挂载nfs服务器上的共享目录),另外还可结合iptables来加强
关机时若nfs server上有client联机时,先关掉portmap与nfs两个系统服务。若无法正确将此2项服务关掉,用netstat –utlp找出PID,然后用kill杀掉进程才关机

nfsstat  查看NFS的运行状态,调整NFS运行大有帮助
rpcinfo  查看rpc执行信息,用于检测rpc运行情况

挂载nfs目录格式(语法):

mount -t nfs hostname(or IP):/directory /mount-point

卸载:

umount /本地挂载目录(本地client卸载方法,但用exports -au为server卸载)
 

showmout命令对于NFS的操作和查错有很大的帮助,参数:

-a :这个参数是一般在NFS SERVER上使用,是用来显示已经mount上本机nfs目录的client机器。
-e :显示指定的NFS SERVER上export出来的目录。

showmount -e IP(查看NFS服务器上共享了那些目录)
showmount -a IP(用于nfs server上,显示已经mount上本机NFS服务器的client(客户机)) 

#######################################################################################################################################

 

以下转载:在路上 » 【整理】Linux中fstab的语法和参数含义和mount NFS时相关参数含义

【背景】

【已解决】在远程Ubuntu服务器中开通NFS服务供别人访问服务器上的文件

期间,对于参考文档,写了下面的配置:

121.41.120.185:/home/share/image /root/nfs_client_root/ nfs auto,noatime,nolock,bg,nfsvers=4,intr,tcp,actimeo=1800 0 0

需要搞懂fstab的格式,语法。

【折腾过程】

fstab 语法

man fstab

man fstab bg

man fstab nolock

参考:

linux fstab 语法 – rajaruan – 51CTO技术博客

/etc/fstab Mount Process

fstab(5) – Linux manual page

nfs(5) – nfs, nfs – nfs fstab format and options – man 5 nfs

/etc/fstab Mount Process

NFS Client Configuration Files

fstab的格式:

file_system

Is the remote server directory to be mounted.

directory

Is the mount point on the client where the directory is attached.

type

Is the file system type. This can be nfs2 for NFS2 mounting, nfs3 for the NFS3 protocol, and nfs and nfs3pref for mounts that attempt the NFS3 protocol, but fall back to nfs2 if the attempt fails.

options

Is mount options (see Section 4.6.2.1 in this section).

frequency

Is always set to zero (0) for NFS and CacheFS entries.

pass

Is always set to zero (0) for NFS and CacheFS entries.

其中那些参数的含义是:

defaults use default options: rw, suid, dev, exec, auto, nouser, and async.

 

acregmin=n

The minimum time (in seconds) that the NFS client caches attributes of a regular file before it requests fresh attribute information from a server. If this option is not specified, the NFS client uses a 3-second minimum.

acregmax=n

The maximum time (in seconds) that the NFS client caches attributes of a regular file before it requests fresh attribute information from a server. If this option is not specified, the NFS client uses a 60-second maximum.

acdirmin=n

The minimum time (in seconds) that the NFS client caches attributes of a directory before it requests fresh attribute information from a server. If this option is not specified, the NFS client uses a 30-second minimum.

acdirmax=n

The maximum time (in seconds) that the NFS client caches attributes of a directory before it requests fresh attribute information from a server. If this option is not specified, the NFS client uses a 60-second maximum.

actimeo=n

Using actimeo sets all of acregminacregmaxacdirmin, and acdirmax to the same value. If this option is not specified, the NFS client uses the defaults for each of these options listed above.

atime / noatime / relatime / strictatime (Linux-specific)

在Unix中,每个文件都有记录上次访问文件时间(atime的),修改时间(mtime),和changed time(ctime)。atime是指每个文件被读取的时间,这个功能经常受到批评,因为这样会造成性能下降,并增加磨损。然而,一些应用程序和用户会使用到atime,因此需要指定选项,atime?noatime?(in Linux) relatime (update atime if older than mtime)?Linux内核版本是2.6.29,默认是atime;2.6.30则默认是relatime。

      bg              (background) Mounting is performed as a background task if the first attempt fails. The default setting is off.

      nfsvers=n   Use an alternate RPC version number to contact the NFS daemon on the remote host.  This option is useful for hosts that can run multiple 

                        NFS servers.  The default value is version 2.

      intr             If an NFS file operation has a major timeout and it is hard mounted, then allow signals to interupt the file operation and cause it to return

                        EINTR to the calling program.  The default is to not allow file operations to be interrupted.

      tcp              Mount the NFS filesystem using the TCP protocol instead of the default UDP protocol.  Many NFS servers only support UDP.

      actimeo=n   Using  actimeo sets all of acregmin, acregmax, acdirmin, and acdirmax to the same value.  There is no default value.

 

  • hard or soft — Specifies whether the program using a file via an NFS connection should stop and wait (hard) for the server to come back online if the host serving the exported file system is unavailable, or if it should report an error (soft).

    If hard is specified, the user cannot terminate the process waiting for the NFS communication to resume unless the intr option is also specified.

    If soft, is specified, the user can set an additional timeo=<value> option, where<value> specifies the number of seconds to pass before the error is reported.

  • intr — Allows NFS requests to be interrupted if the server goes down or cannot be reached.

  • nfsvers=2 or nfsvers=3 — Specifies which version of the NFS protocol to use.

  • nolock — Disables file locking. This setting is occasionally required when connecting to older NFS servers.

  • noexec — Prevents execution of binaries on mounted file systems. This is useful if the system is mounting a non-Linux file system via NFS containing incompatible binaries.

  • nosuid — Disables set-user-identifier or set-group-identifier bits. This prevents remote users from gaining higher privileges by running a setuid program.

  • rsize=8192 and wsize=8192 — These settings speed up NFS communication for reads (rsize) and writes (wsize) by setting a larger data block size, in bytes, to be transferred at one time. Be careful when changing these values; some older Linux kernels and network cards do not work well with larger block sizes.

  • tcp — Specifies for the NFS mount to use the TCP protocol instead of UDP.

 

【总结】

上述的fstab中的配置:

121.41.120.185:/home/share/image  /root/nfs_client_root/  nfs  auto,noatime,nolock,bg,nfsvers=4,intr,tcp,actimeo=1800  0 0

的含义是:

  • 121.41.120.185:/home/share/image
    • 是目标NFS服务器的IP(或域名)和NFS共享的路径
  • /root/nfs_client_root/
    • 是NFS客户端要mount挂载的路径(自定义挂载点,一般挂载到/mnt下面某个路径,比如光驱常挂载到/mnt/cdrom。这里仅为测试方便)
  • nfs
    • 表示挂载的文件系统类型时NFS
  • 各个参数的含义:
    • auto:自动挂载;
    • noatime:不要添加access time==上次访问文件时间;
    • nolock:禁止文件加锁。有时候访问旧的NFS服务器需要此参数。
    • bg:挂载作为后台服务去运行,如果第一次挂载失败了。默认是off的。
    • nfsvers=4:指定NFS协议的版本。
    • intr:允许NFS请求被中断,如果服务器挂了或连不上
    • tcp:指定NFS(不适用默认的UDP而改用)TCP
    • actimeo=1800:acregmin==acregmax==acdirmin====acdirmax,都设置为1800s=30分钟,即文件缓存时间为30分钟
  • 0
    • 不需要NFS的CacheFS?
posted on 2020-09-14 16:05  51core  阅读(225)  评论(0)    收藏  举报