Difference between revisions of "NFS (简体中文)"

From ArchWiki
Jump to: navigation, search
(5 intermediate revisions by the same user not shown)
Line 12: Line 12:
 
{{translateme (简体中文)}}
 
{{translateme (简体中文)}}
  
[[Wikipedia: Network File System|Wikipedia]]: NFS 网络文件系统(Network File System) 是由Sun公司1984年发布的分散式文件系统协议。允许用户如同访问本地文件一样,去访问网络上共享的文件。
+
来源 [[Wikipedia: Network File System|Wikipedia]]: NFS 网络文件系统(Network File System) 是由Sun公司1984年发布的分散式文件系统协议。允许用户像访问本地文件一样,去访问网络上共享的文件。NFS 是一個成功的文件共享方法,但它最大的问题是它不太适合大型的分散式系統。
  
 
本文介绍 NFSv4 的安装.
 
本文介绍 NFSv4 的安装.
 
==安装==
 
==安装==
 
客户端和服务端都需要 [[Pacman|installation]] {{Pkg|nfs-utils}} 包。
 
客户端和服务端都需要 [[Pacman|installation]] {{Pkg|nfs-utils}} 包。
Both client and server only require the  of the  package.
 
  
{{Note|It is HIGHLY recommended to use a time sync daemon on ALL nodes of your network to keep client/server clocks in sync.  Without accurate clocks on all nodes, NFS can introduce unwanted delays!}}
+
{{Note|建议在所有客户机和服务器上使用时间同步的守护进程(daemon),如果各个节点上没有精确的时钟,NFS 可能产生延时。}}
  
The [[NTP]] system is recommended to sync both the server and the clients to the highly accurate NTP servers available on the Internet.
+
建议通过互联网同步服务端和客户端的时钟。
  
==Configuration==
+
==配置==
  
===Server===
+
===服务端===
 
==== ID mapping ====
 
==== ID mapping ====
Edit {{ic|/etc/idmapd.conf}} and set the {{ic|Domain}} field to your domain name.
+
编辑 {{ic|/etc/idmapd.conf}} 设置 {{ic|Domain}} 字段为你的域名。
  
 
{{hc|/etc/idmapd.conf|<nowiki>
 
{{hc|/etc/idmapd.conf|<nowiki>
Line 41: Line 40:
 
Nobody-Group = nobody
 
Nobody-Group = nobody
 
</nowiki>}}
 
</nowiki>}}
==== File system ====
+
==== 文件系统 ====
{{Note|For security reasons, it is recommended to use an NFS export root which will keep users limited to that mount point only.  The following example illustrates this concept.}}
+
{{Note|基于安全原因,建议指定一个 NFS 输出的根(目录),来限制用户的可用挂载点。下面的例子践行此原则。}}
  
Define any NFS shares in {{ic|/etc/exports}} which are relative to the NFS root.  In this example, the NFS root will be {{ic|/srv/nfs4}} and we will be sharing {{ic|/mnt/music}}.
+
{{ic|/etc/exports}} 里定义相对于 NFS 根目录的任意 NFS 共享。 在这个例子中,NFS 根目录为 {{ic|/srv/nfs4}} 并且共享 {{ic|/mnt/music}} 目录。
  
 
{{bc|# mkdir -p /srv/nfs4/music}}
 
{{bc|# mkdir -p /srv/nfs4/music}}
  
Read/Write permissions must be set on the music directory so clients may write to it.
+
要想让客户端可以写入这个目录,确保 music 目录有读写权限。
  
Now mount the actual target share, {{ic|/mnt/music}} to the NFS share via the mount command:
+
挂载 {{ic|/mnt/music}} NFS 共享。 使用 mount 命令:
  
 
{{bc|# mount --bind /mnt/music /srv/nfs4/music}}
 
{{bc|# mount --bind /mnt/music /srv/nfs4/music}}
  
To make it stick across server reboots, add the bind mount to {{ic|fstab}}:
+
为使服务器重启后仍然有效, 增加绑定到 {{ic|fstab}} 文件:
 +
 
 
{{hc|/etc/fstab|
 
{{hc|/etc/fstab|
 
/mnt/music /srv/nfs4/music  none  bind  0  0
 
/mnt/music /srv/nfs4/music  none  bind  0  0
Line 60: Line 60:
  
 
==== Exports ====
 
==== Exports ====
Add directories to be shared and an ip address or hostname(s) of client machines that will be allowed to mount them in {{ic|exports}}:
+
增加允许被挂载的目录和主机到{{ic|exports}}:
 
{{hc|/etc/exports|<nowiki>
 
{{hc|/etc/exports|<nowiki>
 
/srv/nfs4/ 192.168.0.1/24(rw,fsid=0,no_subtree_check)
 
/srv/nfs4/ 192.168.0.1/24(rw,fsid=0,no_subtree_check)
Line 66: Line 66:
 
</nowiki>}}
 
</nowiki>}}
  
Users need-not open the share to the entire subnet; one can specify a single IP address or hostname as well.
+
不必共享给整个子网; 设置一个指定的IP地址也不错。
  
See {{ic|man exports}} for detailed settings information.
+
具体设置查看 {{ic|man exports}}
  
 
{{Note|The {{ic|1=fsid=0}} is required for the root file system being exported. {{ic|/srv/nfs4}} is the NFS root here (due to the {{ic|1=fsid=0}} entry). Everything else that you want to be shared over NFS must be accessible under {{ic|/srv/nfs4}}. Setting an NFS root is required. For exporting directories outside the NFS root, see below.}}
 
{{Note|The {{ic|1=fsid=0}} is required for the root file system being exported. {{ic|/srv/nfs4}} is the NFS root here (due to the {{ic|1=fsid=0}} entry). Everything else that you want to be shared over NFS must be accessible under {{ic|/srv/nfs4}}. Setting an NFS root is required. For exporting directories outside the NFS root, see below.}}
For more information about all available options see {{ic|man 5 exports}}.
 
  
If you modify {{ic|/etc/exports}} while the server is running, you must reexport them for changes to take effect:
+
更多可用选项 {{ic|man 5 exports}}.
 +
 
 +
如果服务运行时修改了 {{ic|/etc/exports}} 文件, 你需要重新导出使其生效。
 +
 
 
{{bc|# exportfs -ra}}
 
{{bc|# exportfs -ra}}
  
====Starting the server====
+
====开始运行服务====
  
The services for the NFS server are {{ic|rpc-idmapd.service}} and {{ic|rpc-mountd.service}}.
+
NFS 服务包括 {{ic|rpc-idmapd.service}} {{ic|rpc-mountd.service}}
  
Start them and configure them to start at boot. Read [[Daemons]] for more details.
+
注意这些 units 会请求其它服务, 这些服务会被 [[systemd]] 自动开启。
  
Note that these units require others, which are launched automatically by [[systemd]].
+
===客户端===
 +
客户端需要 {{Pkg|nfs-utils}},但在连接 NFS4 服务器时无需其它步骤;
  
===Client===
+
====Linux 上挂载====
Clients need {{Pkg|nfs-utils}} to connect, but no special setup is required when connecting to NFS 4 servers.
+
显示指定服务器的共享:
  
====Mounting from Linux====
+
{{bc|$ showmount -e 服务器名或IP}}
Show the server's exported filesystems:
+
{{bc|$ showmount -e servername}}
+
  
Then just mount as normal:
+
然后按照普通方式挂载:
 
{{bc|# mount -t nfs4 servername:/music /mountpoint/on/client}}
 
{{bc|# mount -t nfs4 servername:/music /mountpoint/on/client}}
  
=====/etc/fstab Settings=====
+
=====/etc/fstab 设置=====
Using [[fstab]] is useful for a server which is always on, and the NFS shares are available whenever the client boots up. Edit {{ic|/etc/fstab}} file, and add an appropriate line reflecting the setup.
+
在启动时自动挂载。编辑 {{ic|/etc/fstab}} 文件,增加一行。
 +
 
 
{{hc|/etc/fstab|<nowiki>
 
{{hc|/etc/fstab|<nowiki>
 
servername:/music  /mountpoint/on/client  nfs4  rsize=8192,wsize=8192,timeo=14,intr 0 0
 
servername:/music  /mountpoint/on/client  nfs4  rsize=8192,wsize=8192,timeo=14,intr 0 0
 
</nowiki>}}
 
</nowiki>}}
  
{{Note|Additional mount options can be specified here. Consult the NFS man page for further information.}}
+
一些实用的附加挂载选项:
Some additional mount options to consider are include:
+
  
* {{ic|1=rsize=8192}} and {{ic|1=wsize=8192}}
+
* {{ic|1=rsize=8192}} {{ic|1=wsize=8192}}
 
* {{ic|1=timeo=14}}
 
* {{ic|1=timeo=14}}
 
* {{ic|1=intr}}
 
* {{ic|1=intr}}
  
The {{ic|rsize}} value is the number of bytes used when reading from the server. The {{ic|wsize}} value is the number of bytes used when writing to the server. The default for both is 1024, but using higher values such as 8192 can improve throughput.  This is not universal.  It is recommended to test after making this change.
+
{{ic|rsize}} 的值是从服务器读取的字节数。{{ic|wsize}} 是写入到服务器的字节数。默认都是1024, 如果使用比较高的值,如8192,可以提高传输速度。 到底设到多少合适,还是自己测试吧。
  
 
The {{ic|timeo}} value is the amount of time, in tenths of a second, to wait before resending a transmission after an RPC timeout. After the first timeout, the timeout value is doubled for each retry for a maximum of 60 seconds or until a major timeout occurs. If connecting to a slow server or over a busy network, better performance can be achieved by increasing this timeout value.  
 
The {{ic|timeo}} value is the amount of time, in tenths of a second, to wait before resending a transmission after an RPC timeout. After the first timeout, the timeout value is doubled for each retry for a maximum of 60 seconds or until a major timeout occurs. If connecting to a slow server or over a busy network, better performance can be achieved by increasing this timeout value.  

Revision as of 11:01, 17 January 2013

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki - Dedicated article for common problems and solutions. Template:Article summary wiki - Deprecated v3 format. Template:Article summary end

Tango-preferences-desktop-locale.png本页面需要更新翻译,内容可能已经与英文脱节。要贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: please use the first argument of the template to provide more detailed indications.

来源 Wikipedia: NFS 网络文件系统(Network File System) 是由Sun公司1984年发布的分散式文件系统协议。允许用户像访问本地文件一样,去访问网络上共享的文件。NFS 是一個成功的文件共享方法,但它最大的问题是它不太适合大型的分散式系統。

本文介绍 NFSv4 的安装.

安装

客户端和服务端都需要 installation nfs-utils 包。

Note: 建议在所有客户机和服务器上使用时间同步的守护进程(daemon),如果各个节点上没有精确的时钟,NFS 可能产生延时。

建议通过互联网同步服务端和客户端的时钟。

配置

服务端

ID mapping

编辑 /etc/idmapd.conf 设置 Domain 字段为你的域名。

/etc/idmapd.conf
[General]
 
Verbosity = 1
Pipefs-Directory = /var/lib/nfs/rpc_pipefs
Domain = atomic

[Mapping]

Nobody-User = nobody
Nobody-Group = nobody

文件系统

Note: 基于安全原因,建议指定一个 NFS 输出的根(目录),来限制用户的可用挂载点。下面的例子践行此原则。

/etc/exports 里定义相对于 NFS 根目录的任意 NFS 共享。 在这个例子中,NFS 根目录为 /srv/nfs4 并且共享 /mnt/music 目录。

# mkdir -p /srv/nfs4/music

要想让客户端可以写入这个目录,确保 music 目录有读写权限。

挂载 /mnt/music 到 NFS 共享。 使用 mount 命令:

# mount --bind /mnt/music /srv/nfs4/music

为使服务器重启后仍然有效, 增加绑定到 fstab 文件:

/etc/fstab
/mnt/music /srv/nfs4/music  none   bind   0   0

Exports

增加允许被挂载的目录和主机到exports:

/etc/exports
/srv/nfs4/ 192.168.0.1/24(rw,fsid=0,no_subtree_check)
/srv/nfs4/music 192.168.0.1/24(rw,no_subtree_check,nohide) # note the nohide option which is applied to mounted directories on the file system.

不必共享给整个子网; 设置一个指定的IP地址也不错。

具体设置查看 man exports

Note: The fsid=0 is required for the root file system being exported. /srv/nfs4 is the NFS root here (due to the fsid=0 entry). Everything else that you want to be shared over NFS must be accessible under /srv/nfs4. Setting an NFS root is required. For exporting directories outside the NFS root, see below.

更多可用选项 man 5 exports.

如果服务运行时修改了 /etc/exports 文件, 你需要重新导出使其生效。

# exportfs -ra

开始运行服务

NFS 服务包括 rpc-idmapd.servicerpc-mountd.service

注意这些 units 会请求其它服务, 这些服务会被 systemd 自动开启。

客户端

客户端需要 nfs-utils,但在连接 NFS4 服务器时无需其它步骤;

Linux 上挂载

显示指定服务器的共享:

$ showmount -e 服务器名或IP

然后按照普通方式挂载:

# mount -t nfs4 servername:/music /mountpoint/on/client
/etc/fstab 设置

在启动时自动挂载。编辑 /etc/fstab 文件,增加一行。

/etc/fstab
servername:/music   /mountpoint/on/client   nfs4   rsize=8192,wsize=8192,timeo=14,intr	0 0

一些实用的附加挂载选项:

  • rsize=8192wsize=8192
  • timeo=14
  • intr

rsize 的值是从服务器读取的字节数。wsize 是写入到服务器的字节数。默认都是1024, 如果使用比较高的值,如8192,可以提高传输速度。 到底设到多少合适,还是自己测试吧。

The timeo value is the amount of time, in tenths of a second, to wait before resending a transmission after an RPC timeout. After the first timeout, the timeout value is doubled for each retry for a maximum of 60 seconds or until a major timeout occurs. If connecting to a slow server or over a busy network, better performance can be achieved by increasing this timeout value.

The intr option allows signals to interrupt the file operation if a major timeout occurs for a hard-mounted share.

Using autofs

Using autofs is useful when multiple machines want to connect via NFS; they could both be clients as well as servers. The reason this method is preferable over the earlier one is that if the server is switched off, the client will not throw errors about being unable to find NFS shares. See autofs#NFS Network mounts for details.

Mounting from Windows

Warning: Serious performance issues may occur (it randomly takes 30-60 seconds to display a folder, 2 MB/s file copy speed on gigabit LAN, ...) to which Microsoft does not have a solution yet.[1]
Note: Only the Ultimate and Enterprise editions of Windows 7 and the Enterprise edition of Windows 8 include "Client for NFS"

NFS shares can be mounted from windows if the "Client for NFS" service is actived (which it is not by default). To install the service go to "Programs and features" either through the control panel or by typing it in the search box from the start menu and click on "Turn Windows features on or off". Locate the "Services for NFS" and activate it as well as both subservices ("Administrative tools" and "Client for NFS").

Some global options can be set by opening the "Services for Network File System" (locate it with the search box) and right clicking on the client->properties.

Warning: Under Windows the share is addressed by its full path on the server, not just the path relative to the nfsroot! If in doubt run showmount -e servername from cmd.exe

Mounting from OS X

Note: OS X by default uses an insecure (>1024) port to mount a share.

Either export the share with the insecure flag, and mount using Finder:

Go > Connect to Server > nfs://servername/

Or, mount the share using a secure port using the terminal:

# sudo mount -t nfs -o resvport servername:/ /Volumes/servername/
Warning: Under OS X the share is addressed by its full path on the server, not just the path relative to the nfsroot! If in doubt run showmount -e servername from the terminal

Troubleshooting

There is a dedicated article NFS Troubleshooting.