NFS服务配置(用于在网络上共享存储)
服务端配置NFS
CentOS上使用NFS服务需要安装两个包(nfs-utils和rpcbind),不过使用yum工具安装nfs-utils时会一并安装rpcbind
[root@ns1 ~]# mount /dev/sr0 /mnt/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@ns1 ~]# cd /etc/yum.repos.d/
[root@ns1 yum.repos.d]# ls
CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-Debuginfo.repo CentOS-Media.repo
[root@ns1 yum.repos.d]# vim CentOS-Media.repo
[root@ns1 yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak
[root@ns1 yum.repos.d]# yum repolist
已加载插件:fastestmirror, security
Loading mirror speeds from cached hostfile
仓库标识 仓库名称 状态
c6-media CentOS-6 - Media 6,706
repolist: 6,706
[root@ns1 yum.repos.d]# yum install -y nfs-utils
已加载插件:fastestmirror, security
设置安装进程
Loading mirror speeds from cached hostfile
包 1:nfs-utils-1.2.3-75.el6.x86_64 已安装并且是最新版本
无须任何处理
创建简单的NFS服务器
[root@ns1 ~]# vim /etc/exports //写入以下内容
/home/nfstestdir 172.16.1.0/16(rw,sync,all_squash,anonuid=1000,anongid=1000)
分为三部分,第一部分是本地要共享出去的目录,第二部分为允许访问的主机(可以是一个IP,也可以是一个IP段)
第三部分就是小括号里面的一些权限选项。
选项:rw:表示读/写
ro:表示只读
sync:同步模式,表示内存中的数据实时写入磁盘
async:非同步模式,表示把内存中的数据定期写入磁盘
no_root_squash:加上这个选项后,root用户会对共享的目录拥有至高的权限控制,就像是对本机目录操作一样。安全性降低
root_squash:与 no_root_squash相对应,表示root用户对共享目录的权限不高,只有普通用户的权限
all_squash:表示不管使用NFS的用户是谁,其身份都会被限定为一个指定的普通用户身份
anonuid/anongid:要和 root_squash及 all_squash一同使用,用于指定使用NFS的用户被限定后的uid和gid,但前提是本机的/etc/password中存在相对应的uid和gid
[root@ns1 ~]# /etc/init.d/nfs start
启动 NFS 服务: [确定]
关掉 NFS 配额: [确定]
启动 NFS mountd: [确定]
启动 NFS 守护进程: [确定]
正在启动 RPC idmapd: [确定]
客户端挂载NFS
[root@ns2 ~]# mount -t nfs 172.16.1.143:/home/nfstestdir /mnt
[root@ns2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 2.1G 15G 13% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 488M 33M 430M 8% /boot
172.16.1.143:/home/nfstestdir
18G 2.1G 15G 13% /mnt
[root@ns2 ~]# ls -l /mnt/
总用量 0
-rw-r--r-- 1 root root 0 4月 24 16:29 test1
创建测试文件
[root@ns2 ~]# cd /mnt/
[root@ns2 mnt]# touch test2
touch: 无法创建"test2": 权限不够
这是因为在服务端(1.143)上创建的/home/nfstestdir目录权限不合适,挂载后相当于被限制为uid为1000的用户,解决该问题需要在服务端(1.143)
上修改/home/nfstestdir目录权限
[root@ns2 ~]# useradd -u1000 user1
[root@ns2 ~]# mount -t nfs -onolock,nfsvers=3 172.16.1.143:/home/nfstestdir /mnt
[root@ns2 ~]# cd /mnt
[root@ns2 mnt]# ls
test1 test1.txt
[root@ns2 mnt]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 4月 24 16:29 test1
-rw-r--r-- 1 user1 user1 0 4月 24 17:26 test1.txt
[root@ns2 mnt]# id user1
uid=1000(user1) gid=1000(user1) 组=1000(user1)
mount -t nfs -onolock,nfsvers=3 172.16.1.143:/home/nfstestdir /mnt
如果不加-onolock,nfsvers=3 则挂载目录下文件属主和属组都是nobody,如果指定nfsvers=3则显示root
-a:表示全部挂载或者卸载
-r:表示重新挂载
-u:表示卸载某一个目录
-v:表示显示共享的目录
当改变/etc/exports配置文件后,使用exportfs命令挂载不需要重启NFS服务
例:修改服务端(1.143)的配置文件
[root@ns1 ~]# vim /etc/exports //增加一行
/tmp/ 172.16.1.0/16(rw,sync,no_root_squash)
然后在服务端上(1.1443)执行:
[root@ns1 ~]# exportfs -arv
exporting 172.16.1.0/16:/tmp
exporting 172.16.1.0/16:/home/nfstestdir
在客户端:
[root@ns2 ~]# showmount -e 172.16.1.143
Export list for 172.16.1.143:
/tmp 172.16.1.0/16
/home/nfstestdir 172.16.1.0/16
服务端配置NFS
CentOS上使用NFS服务需要安装两个包(nfs-utils和rpcbind),不过使用yum工具安装nfs-utils时会一并安装rpcbind
[root@ns1 ~]# mount /dev/sr0 /mnt/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@ns1 ~]# cd /etc/yum.repos.d/
[root@ns1 yum.repos.d]# ls
CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-Debuginfo.repo CentOS-Media.repo
[root@ns1 yum.repos.d]# vim CentOS-Media.repo
[root@ns1 yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak
[root@ns1 yum.repos.d]# yum repolist
已加载插件:fastestmirror, security
Loading mirror speeds from cached hostfile
仓库标识 仓库名称 状态
c6-media CentOS-6 - Media 6,706
repolist: 6,706
[root@ns1 yum.repos.d]# yum install -y nfs-utils
已加载插件:fastestmirror, security
设置安装进程
Loading mirror speeds from cached hostfile
包 1:nfs-utils-1.2.3-75.el6.x86_64 已安装并且是最新版本
无须任何处理
创建简单的NFS服务器
[root@ns1 ~]# vim /etc/exports //写入以下内容
/home/nfstestdir 172.16.1.0/16(rw,sync,all_squash,anonuid=1000,anongid=1000)
分为三部分,第一部分是本地要共享出去的目录,第二部分为允许访问的主机(可以是一个IP,也可以是一个IP段)
第三部分就是小括号里面的一些权限选项。
选项:rw:表示读/写
ro:表示只读
sync:同步模式,表示内存中的数据实时写入磁盘
async:非同步模式,表示把内存中的数据定期写入磁盘
no_root_squash:加上这个选项后,root用户会对共享的目录拥有至高的权限控制,就像是对本机目录操作一样。安全性降低
root_squash:与 no_root_squash相对应,表示root用户对共享目录的权限不高,只有普通用户的权限
all_squash:表示不管使用NFS的用户是谁,其身份都会被限定为一个指定的普通用户身份
anonuid/anongid:要和 root_squash及 all_squash一同使用,用于指定使用NFS的用户被限定后的uid和gid,但前提是本机的/etc/password中存在相对应的uid和gid
启动服务
[root@ns1 ~]# /etc/init.d/rpcbind start[root@ns1 ~]# /etc/init.d/nfs start
启动 NFS 服务: [确定]
关掉 NFS 配额: [确定]
启动 NFS mountd: [确定]
启动 NFS 守护进程: [确定]
正在启动 RPC idmapd: [确定]
客户端挂载NFS
[root@ns2 ~]# mount -t nfs 172.16.1.143:/home/nfstestdir /mnt
[root@ns2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 2.1G 15G 13% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 488M 33M 430M 8% /boot
172.16.1.143:/home/nfstestdir
18G 2.1G 15G 13% /mnt
[root@ns2 ~]# ls -l /mnt/
总用量 0
-rw-r--r-- 1 root root 0 4月 24 16:29 test1
创建测试文件
[root@ns2 ~]# cd /mnt/
[root@ns2 mnt]# touch test2
touch: 无法创建"test2": 权限不够
这是因为在服务端(1.143)上创建的/home/nfstestdir目录权限不合适,挂载后相当于被限制为uid为1000的用户,解决该问题需要在服务端(1.143)
上修改/home/nfstestdir目录权限
[root@ns2 ~]# useradd -u1000 user1
[root@ns2 ~]# mount -t nfs -onolock,nfsvers=3 172.16.1.143:/home/nfstestdir /mnt
[root@ns2 ~]# cd /mnt
[root@ns2 mnt]# ls
test1 test1.txt
[root@ns2 mnt]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 4月 24 16:29 test1
-rw-r--r-- 1 user1 user1 0 4月 24 17:26 test1.txt
[root@ns2 mnt]# id user1
uid=1000(user1) gid=1000(user1) 组=1000(user1)
mount -t nfs -onolock,nfsvers=3 172.16.1.143:/home/nfstestdir /mnt
如果不加-onolock,nfsvers=3 则挂载目录下文件属主和属组都是nobody,如果指定nfsvers=3则显示root
命令exports
常用选项-a:表示全部挂载或者卸载
-r:表示重新挂载
-u:表示卸载某一个目录
-v:表示显示共享的目录
当改变/etc/exports配置文件后,使用exportfs命令挂载不需要重启NFS服务
例:修改服务端(1.143)的配置文件
[root@ns1 ~]# vim /etc/exports //增加一行
/tmp/ 172.16.1.0/16(rw,sync,no_root_squash)
然后在服务端上(1.1443)执行:
[root@ns1 ~]# exportfs -arv
exporting 172.16.1.0/16:/tmp
exporting 172.16.1.0/16:/home/nfstestdir
在客户端:
[root@ns2 ~]# showmount -e 172.16.1.143
Export list for 172.16.1.143:
/tmp 172.16.1.0/16
/home/nfstestdir 172.16.1.0/16