搭建nfs服务器

nfs(网络文件系统)是linux系统之间文件共享的一种方式。

需要安装的软件包:nfs-utils (redhat6 默认是安装的)

服务启动脚本:/etc/init.d/nfs

配置文件:/etc/exports

实验环境:redhat6.2 32bit

ip地址:192.169.1.98

本机即作为nfs服务器,也作为nfs客户端,/file为服务器共享目录,/receive为客户端挂载点。

过程如下:

[root@mail ~]# rpm -qa|grep nfs		#redhat6默认是安装这几个包的
nfs4-acl-tools-0.3.3-5.el6.i686
nfs-utils-lib-1.1.5-1.el6.i686
nfs-utils-1.2.2-7.el6.i686
[root@mail ~]# service nfs start
启动 NFS 服务:                                            [确定]
关掉 NFS 配额:                                            [确定]
启动 NFS 守护进程:                                        [确定]
启动 NFS mountd:                                          [确定]
[root@mail ~]# vim /etc/exports 
[root@mail ~]# cat /etc/exports		#将/file目录以同步只读的方式共享给192.169.1.98客户主机
/file	192.169.1.98(sync,ro)
[root@mail ~]# mkdir /file
[root@mail ~]# service nfs restart
关闭 NFS mountd:                                          [确定]
关闭 NFS 守护进程:                                        [确定]
关闭 NFS quotas:                                          [确定]
启动 NFS 服务:                                            [确定]
关掉 NFS 配额:                                            [确定]
启动 NFS 守护进程:                                        [确定]
启动 NFS mountd:                                          [确定]
[root@mail ~]# showmount -e 192.169.1.98	#查看192.169.1.98这台机的共享目录
Export list for 192.169.1.98:
/file 192.169.1.98
[root@mail ~]# mkdir /receive			#创建本地挂载点
[root@mail ~]# mount -t nfs 192.169.1.98:/file /receive/	#挂载
[root@mail ~]# df -hT
文件系统    类型      容量  已用  可用 已用%% 挂载点
/dev/sda3     ext4    9.9G  2.6G  6.8G  28% /
tmpfs        tmpfs    330M  424K  330M   1% /dev/shm
/dev/sda1     ext4    194M   24M  161M  13% /boot
/dev/sr0   iso9660    2.9G  2.9G     0 100% /media/RHEL_6.2 i386 Disc 1
/dev/sr0   iso9660    2.9G  2.9G     0 100% /mnt/iso
192.169.1.98:/file
               nfs    9.9G  2.6G  6.8G  28% /receive	#可以看到类型为nfs
You have new mail in /var/spool/mail/root
[root@mail ~]# touch /file/a		#服务器端在创建一个文件
[root@mail ~]# ls /receive/		#客户端对应目录下可以看到对应的文件了
a
[root@mail ~]# touch /receive/b		#因为服务器以只读方式方式共享,所以客户端不能写
touch: 无法创建"/receive/b": 只读文件系统

[root@mail ~]# vim /etc/exports		
[root@mail ~]# cat /etc/exports		#修改配置文件,以读写方式共享
/file	192.169.1.98(sync,rw)
[root@mail ~]# exportfs -rv		#服务器端,重新读取配置文件,相当于平滑重启了
exporting 192.169.1.98:/file
[root@mail ~]# ll -ld /file/
drwxr-xr-x. 2 root root 4096  8月 14 11:07 /file/
[root@mail ~]# chmod o+w /file/		#客户端登陆服务器写,默认是以nfsnobody的身份,所以给/file的other写权限
[root@mail ~]# ll -ld /file/
drwxr-xrwx. 2 root root 4096  8月 14 11:07 /file/
[root@mail ~]# ls /receive/
a
[root@mail ~]# touch /receive/b		
[root@mail ~]# ls -l /receive/		#客户端可以写了
总用量 0
-rw-r--r--. 1 root      root      0  8月 14 11:07 a
-rw-r--r--. 1 nfsnobody nfsnobody 0  8月 14 11:17 b
[root@mail ~]# chmod o-w /file/	
[root@mail ~]# touch /receive/c		#对/file去掉other的写权限,是不能写的
touch: 无法创建"/receive/c": 权限不够
[root@mail ~]# vim /etc/exports		
[root@mail ~]# cat /etc/exports		#但是可以改变客户端写的身份为root
/file	192.169.1.98(sync,rw,no_root_squash)	#加上no_root_squash,这样客户端写的身份为root了
[root@mail ~]# exportfs -rv
exporting 192.169.1.98:/file
[root@mail ~]# touch /receive/c
[root@mail ~]# ls -l /receive/		#这下可以写了,因为是以root身份写的
总用量 0
-rw-r--r--. 1 root      root      0  8月 14 11:07 a
-rw-r--r--. 1 nfsnobody nfsnobody 0  8月 14 11:17 b
-rw-r--r--. 1 root      root      0  8月 14 11:19 c
[root@mail ~]# 

/etc/exports中

/file   192.169.1.98(sync,rw)地址的格式可以是以下几种格式。

实例说明
192.168.152.13指定IP地址的主机
nfsclient.test.com指定域名的主机
192.168.1.0/24 指定网段中的所有主机 
*.test.com指定域下的所有主机
*所有主机

括号中的设置选项可以是:

sync:设置NFS服务器同步写磁盘,这样不会轻易丢失数据,建议所有的NFS共享目录都使用该选项
ro:设置输出的共享目录只读,与rw不能共同使用
rw:设置输出的共享目录可读写,与ro不能共同使用
no_root_squash: 设置客户机以root用户身份读写

重新输出共享目录
使nfs服务器重新读取exports文件中的设置
# exportfs -rv
停止输出所有目录
停止当前主机中NFS服务器的所有目录输出
# exportfs -auv
输出(启用)所有目录
输出当前主机中NFS服务器的所有共享目录
# exportfs -av

使用umount命令卸载NFS文件系统
# umount /mnt/

实现开机就挂载可以

1.写入/etc/fstab

192.169.1.98:/file /receive nfs defaults 0 0

2.写入开机后启动脚本/etc/rc.local

mount -t nfs 192.169.1.98:/file /receive

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值