Nfs服务器简单搭建
创建共享目录,并更改权限
[root@localhost ~]# mkdir /www
[root@localhost ~]# chown -R 777 /www/
更改selinux机制为禁止
[root@localhost ~]# vim /etc/sysconfig/selinux
SELINUX=disabled
写防火墙规则或直接关掉
[root@localhost ~]# service iptables stop
[root@localhost ~]# /etc/init.d/iptables restart #最后重启防火墙使配置生效
防火墙端口说明:
portmap或者rpcbind(CentOS 6.x)使用:tcp/udp 111
nfs使用:tcp/udp 2049
mountd使用: TCP/UDP 892
rquotad使用:tcp/udp 875
status使用: TCP/UDP 1001-1004共四个端口
nlockmgr使用:TCP/32803端口 UDP/32769端口
安装nfs所需软件
nfs-utils-1.0.9-44.el5
portmap-4.0-65.2.2.1 #在CentOS 6.x中portmap的名称变为rpcbind
[root@localhost ~]# yum install nfs-utils* portmap
配置nfs共享目录
[root@localhost ~]# vim /etc/exports
/www 192.168.1.0/24(rw,sync,no_root_squash)
#或 /www *(rw,sync,no_root_squash) *代表允许所有主机通过
:wq! #保存退出
相关参数说明:
/www#NFS共享目录
192.168.1.0/24 #允许这个网段内的IP地址访问共享目录
rw #读取写入权限
sync #数据实时同步
no_subtree_check #不检查目录权限,提高数据读取效率
anonuid=501 #501代表nfs用户 打开cat /etc/passwd 查找nfs对应的id
anongid=501 #501代表nfs组 打开cat /etc/passwd 查找nfs对应的id
重启自动生效启动服务
[root@localhost ~]# /etc/init.d/portmap start
[root@localhost ~]# /etc/init.d/nfslock start
[root@localhost ~]# /etc/init.d/nfs start
[root@localhost ~]# chkconfig portmap on
[root@localhost ~]# chkconfig nfslock on
[root@localhost ~]# chkconfig nfs on
在客户端执行以下指令:
[root@localhost ~]# showmount -e 192.168.1.1 #显示server上面共享的目录,出现下面的提示,说明配置正确
Export list for 192.168.1.1:
/www 192.168.1.0/24
NFS客户端
创建nfs运行账号
[root@localhost ~]# useradd nfs
创建共享目录,并更改权限
[root@localhost ~]# chown -R 777 /opt/
更改selinux机制为禁止
[root@localhost ~]# vim /etc/sysconfig/selinux
SELINUX=disabled
挂载共享目录到本地/opt下
[root@localhost ~]# mount -t nfs 192.168.1.1:/www /opt/
[root@localhost ~]# df -hT #查看挂载
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/sda5 ext3 17G 4.8G 11G 31% /
/dev/sda2 ext3 2.0G 36M 1.9G 2% /home
/dev/sda1 ext3 99M 12M 83M 13% /boot
tmpfs tmpfs 252M 0 252M 0% /dev/shm
/dev/hdc iso9660 3.9G 3.9G 0 100% /media/CentOS_5.5_Final
192.168.1.1:/www
nfs 17G 4.9G 11G 32% /opt
测试,分别向两端添加文件,看是否同步
nfs服务器
[root@localhost ~]# touch /www/1.txt
nfs客户端
[root@localhost ~]# ls /opt/
1.txt
设置开机自动挂载(nfs客户端上)
[root@localhost ~]# vim /etc/fstab
192.168.1.1:/www /opt nfs defaults 0 0
[root@localhost ~]# mount #查看挂载
/dev/sda5 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda2 on /home type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
none on /proc/fs/vmblock/mountPoint type vmblock (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hdc on /media/CentOS_5.5_Final type iso9660 (ro,noexec,nosuid,nodev,uid=0)
192.168.1.1:/www on /opt type nfs (rw,addr=192.168.1.1)
如何卸载nfs的mount
[root@cucrz-7 mount]# fuser /home/recv/mount/
/home/recv/mount/: 4381c
[root@cucrz-7 mount]# kill -9 4381
[root@cucrz-7 mount]# umount /home/recv/mount/
#service nfs restart
提示:
rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
解决办法:
1 #service rpcbind restart
2 #service nfs start