在本教程中,让我们看看如何安装和配置NFS服务器和如何在NFS服务器和客户端之间共享文件。虽然我已经在CentOS 6.5 32位版本上测试这些步骤,它也可以在RHEL /Scientific Linux 6.x上工作。
场景
NFS Server Operating System : CentOS 6.5 32 bit (Minimal server install)
NFS Client Operating System : CentOS 6.5 32 bit (Minimal Desktop install)
NFS Server IP Address : 192.168.1.200/24
NFS Client IP Address : 192.168.1.29/24
1. 在服务器上安装NFS
[root@server ~]# yum install nfs* -y
2. 启动NFS服务
[root@server ~]# /etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS mountd: [ OK ]
Stopping RPC idmapd: [ OK ]
Starting RPC idmapd: [ OK ]
Starting NFS daemon: [ OK ]
[root@server ~]# chkconfig nfs on
3. 在客户机上安装NFS
[root@vpn client]# yum install nfs* -y
4. 在客户机上启动NFS服务
[root@vpn client]# /etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Stopping RPC idmapd: [ OK ]
Starting RPC idmapd: [ OK ]
Starting NFS daemon: [ OK ]
[root@vpn client]# chkconfig nfs on
5. 在服务器上创建共享目录
让我们在服务器上创建一个名为“/home/ostechnix”的共享目录,让客户端用户读取和写入文件到 '/home/ostechnix“目录中。
[root@server ~]# mkdir /home/ostechnix
[root@server ~]# chmod 755 /home/ostechnix/
6. 在服务器上导出共享目录
打开/etc/exports文件,如下图所示添加条目:
[root@server ~]# vi /etc/exports
/home/ostechnix 192.168.1.0/24(rw,sync,no_root_squash,no_all_squash)
其中,
/home/ostechnix –
共享目录
192.168.1.0/24 – 客户端访问共享文件夹的IP地址范围
rw – 设定共享文件夹可写
sync – 创建新的文件/文件夹时同步共享目录
no_root_squash – 启用root权限(用户可以读取,写入和删除共享目录中的文件)
no_all_squash – 允许用户权限
现在重新启动NFS服务。(译者注:Ubuntu 的话使用的是 /etc/init.d/nfs-kernel-server)
[root@server ~]# /etc/init.d/nfs restart
Shutting down NFS daemon: [ OK ]
Shutting down NFS mountd: [ OK ]
Shutting down NFS services: [ OK ]
Starting NFS services: [ OK ]
Starting NFS mountd: [ OK ]
Stopping RPC idmapd: [ OK ]
Starting RPC idmapd: [ OK ]
Starting NFS daemon: [ OK ] -
7. 在客户端映射共享目录
创建一个挂载点来挂载服务器的共享目录。
要做到这一点创建一个名为“/nfs/shared”的目录(您可以创建自己的挂载点)
[root@vpn client]# mkdir -p /nfs/shared
现在映射服务器上的共享目录,如下图所示
[root@vpn client]# mount -t nfs 192.168.1.200:/home/ostechnix/ /nfs/shared/
这需要花一些时间,并显示连接超时的错误。好的,不要惊慌,防火墙可能会限制客户端从服务器挂载共享。简单地关闭iptables来纠正问题,也可以通过设定iptables允许NFS服务的端口。
要做到这一点打开/etc/sysconfig/nfs文件,并取消以黑体标出的部分的注释。
[root@server ~]# vi /etc/sysconfig/nfs
#
# Define which protocol versions mountd
# will advertise. The values are "no" or "yes"
# with yes being the default
#MOUNTD_NFS_V2="no"
#MOUNTD_NFS_V3="no"
#
#
# Path to remote quota server. See rquotad(8)
#RQUOTAD="/usr/sbin/rpc.rquotad"
# Port rquotad should listen on.
RQUOTAD_PORT=875
# Optinal options passed to rquotad
#RPCRQUOTADOPTS=""
#
#
# Optional arguments passed to in-kernel lockd
#LOCKDARG=
# TCP port rpc.lockd should listen on.
LOCKD_TCPPORT=32803
# UDP port rpc.lockd should listen on.
LOCKD_UDPPORT=32769
#
#
# Optional arguments passed to rpc.nfsd. See rpc.nfsd(8)
# Turn off v2 and v3 protocol support
#RPCNFSDARGS="-N 2 -N 3"
# Turn off v4 protocol support
#RPCNFSDARGS="-N 4"
# Number of nfs server processes to be started.
# The default is 8.
#RPCNFSDCOUNT=8
# Stop the nfsd module from being pre-loaded
#NFSD_MODULE="noload"
# Set V4 grace period in seconds
#NFSD_V4_GRACE=90
#
#
#
# Optional arguments passed to rpc.mountd. See rpc.mountd(8)
#RPCMOUNTDOPTS=""
# Port rpc.mountd should listen on.
MOUNTD_PORT=892
#
#
# Optional arguments passed to rpc.statd. See rpc.statd(8)
#STATDARG=""
# Port rpc.statd should listen on.
STATD_PORT=662
# Outgoing port statd should used. The default is port
# is random
STATD_OUTGOING_PORT=2020
# Specify callout program
#STATD_HA_CALLOUT="/usr/local/bin/foo"
#
#
# Optional arguments passed to rpc.idmapd. See rpc.idmapd(8)
#RPCIDMAPDARGS=""
#
# Set to turn on Secure NFS mounts.
#SECURE_NFS="yes"
# Optional arguments passed to rpc.gssd. See rpc.gssd(8)
#RPCGSSDARGS=""
# Optional arguments passed to rpc.svcgssd. See rpc.svcgssd(8)
#RPCSVCGSSDARGS=""
#
# To enable RDMA support on the server by setting this to
# the port the server should listen on
#RDMA_PORT=20049
现在重新启动NFS服务
[root@server ~]# /etc/init.d/nfs restart
Shutting down NFS daemon: [ OK ]
Shutting down NFS mountd: [ OK ]
Shutting down NFS services: [ OK ]
Starting NFS services: [ OK ]
Starting NFS mountd: [ OK ]
Stopping RPC idmapd: [ OK ]
Starting RPC idmapd: [ OK ]
Starting NFS daemon: [ OK ]
在‘/etc/sysconfig/iptables’ 文件中添加在以粗体显示的行。
[root@server ~]# vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
-A INPUT -m state --state NEW -m udp -p udp --dport 2049 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2049 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 111 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 111 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 32769 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 32803 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 892 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 892 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 875 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 875 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 662 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 662 -j ACCEPT
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
现在重新启动iptables服务。
[root@server ~]# service iptables restart
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
再次从客户端映射共享
[root@vpn client]# mount -t nfs 192.168.1.200:/home/ostechnix/ /nfs/shared/
最后,NFS共享被映射,没有任何连接超时的错误。
为了验证共享目录是否被安装,请在客户端执行mount命令。
[root@vpn client]# mount
/dev/mapper/vg_vpn-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
192.168.1.200:/home/ostechnix/ on /nfs/shared type nfs (rw,vers=4,addr=192.168.1.200,clientaddr=192.168.1.29)
8. 测试NFS
现在,在‘/nfs/shared’目录下创建一些文件或文件夹。
[root@vpn shared]# mkdir test
[root@vpn shared]# touch file1 file2 file3
现在到服务器上,更换到“/home/ostechnix”目录。
[root@server ~]# cd /home/ostechnix/
[root@server ostechnix]# ls
file1 file2 file3 test
[root@server ostechnix]#
现在列出由客户端创建的文件和目录。你也可以从服务器到客户端共享文件,反之亦然。
9. 自动挂载共享
如果你想自动挂载共享,而不是在每次重新启动手动挂载它们,使用如下客户端‘/etc/fstab’中加粗的行。
[root@vpn client]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Feb 27 15:35:14 2013
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_vpn-lv_root / ext4 defaults 1 1
UUID=59411b1a-d116-4e52-9382-51ff6e252cfb /boot ext4 defaults 1 2
/dev/mapper/vg_vpn-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
192.168.1.200:/home/ostechnix/nfs/sharednfsrw,sync,hard,intr0 0
10. 验证共享
重新启动你的客户端并验证共享是否自动挂载。
[root@vpn client]# mount
/dev/mapper/vg_vpn-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.1.200:/home/ostechnix on /nfs/shared type nfs (rw,sync,hard,intr,vers=4,addr=192.168.1.200,clientaddr=192.168.1.29)
nfsd on /proc/fs/nfsd type nfsd (rw)
这就是所有的了,祝你有美好的一天!