使用yum前先进行挂载搭建本地yum mount /dev/cdrom /mnt/ cd/etc/yum.repos.d/ ls 修改网络yum名
1.使用NFS需要安装两个包(nfs-utils和rpcbind),安装前者会自动安装后者:yum install -y nfs-utils
[root@zjz ~]# yum install -y nfs-utils
已加载插件:fastestmirror, security
设置安装进程
Loading mirror speeds from cached hostfile
包 1:nfs-utils-1.2.3-75.el6.x86_64 已安装并且是最新版本
无须任何处理
[root@zjz ~]# rpm -q nfs-utils
nfs-utils-1.2.3-75.el6.x86_64
[root@zjz ~]# rpm -q rpcbind
rpcbind-0.2.0-13.el6.x86_64
2.修改配置文件etc/exports //将一下内容写入
/home/nfstestdir IP(rw,sync,all_squash,anonuid=1000,anongid=1000)
第一部分是要共享的文件目录,第二部分是允许访问的主机,可以是IP地址,也可以是IP段,第三部分是小括号内一些权限
rw:读和写
ro:只读
sync:同步模式,表示内存中的数据同时写入磁盘
async:非同步模式,把内存中的数据定期写入磁盘
no_root_squash:加上这个选项后root用户就会对共享目录用于志高的权限控制就像本机的目录操作一样
root_squash:表示root用户对共享目录的权限不高,只有普通用户的权限
all_squash:不管使用nfs的用户是谁,其身份都会被限定为一个指定的普通用户身份
anonuid/anongid:指定nfs的uid和gid 前提是本机的etc/passwd中存在相应的uid和gid
3.编辑好配置文件后创建共享目录(/home/nfstestdir)并启动NFS
(7.4)mkdir home/nfstestdir
systemctl start rpcbind
systemctl start nfs
systemctl enable rpcbind
systemctl enable nfs
(6.9)mkdir /home/nfstestdir
service rpcbind start
service nfs start
[root@zjz ~]# service rpcbind start
[root@zjz ~]# service nfs start
启动 NFS 服务: exportfs: No options for /home/nfstestdir 172.16.1.15: suggest 172.16.1.15(sync) to avoid warning
exportfs: No host name given with /home/nfstestdir (rw,sync,all_squash,anonuid=1000,anongid=1000), suggest *(rw,sync,all_squash,anonuid=1000,anongid=1000) to avoid warning
[确定]
关掉 NFS 配额: [确定]
启动 NFS mountd: [确定]
启动 NFS 守护进程: [确定]
正在启动 RPC idmapd: [确定]
4.客户端挂载NFS,客户端也要安装nfs-utils,使用showmount -e 命令查看服务端共享文件,showmount -e 服务端IP地址
然后在客户端挂载NFS,mount -t nfs 服务端IP地址:/home/nfstestdir /mnt/
df -h 会看到新增了一个/mnt分区,就是nfs的共享目录进入目录,创建文件测试(touch),失败,因为权限不够,需要在服务端更改权限
然后重启再次创建
[root@zjz ~]# showmount -e 172.16.1.14
Export list for 172.16.1.14:
/home/nfstestdir (everyone)
[root@zjz ~]# 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
/dev/sr0 3.7G 3.7G 0 100% /mnt
[root@zjz ~]# cd /mnt
[root@zjz mnt]# touch zjz.txt
touch: 无法创建"zjz.txt": 只读文件系统
[root@zjz ~]# chmod 777 /home/nfstestdir/
[root@zjz mnt]# touch zjz.txt
[root@zjz mnt]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 4月 24 18:12 zjz.txt
5.命令exportfs 常用选项:-a -r -u -v
-a 表示全部挂载或者卸载
-r 表示重新挂载
-u 表示卸载一个目录
-v 显示共享的目录
首先在服务器端更改/etc/exports 配置文件
增加一行
/tmp/ 172.16.1.15 (rw,sync,no_root_squash)
执行命令
[root@zjz ~]# exportfs -arv
exportfs: No options for /home/nfstestdir 172.16.1.15: suggest 172.16.1.15(sync) to avoid warning
exportfs: No host name given with /home/nfstestdir (rw,sync,all_squash,anonuid=1000,anongid=1000), suggest *(rw,sync,all_squash,anonuid=1000,anongid=1000) to avoid warning
exportfs: No options for /tmp/ 172.16.1.15: suggest 172.16.1.15(sync) to avoid warning
exportfs: No host name given with /tmp/ (rw,sync,no_root_squash), suggest *(rw,sync,no_root_squash) to avoid warning
exporting 172.16.1.15:/tmp
exporting 172.16.1.15:/home/nfstestdir
exporting *:/tmp
exporting *:/home/nfstestdir
exporting 172.16.1.15:/tmp to kernel
exportfs: internal: no supported addresses in nfs_client
exportfs: 172.16.1.15:/tmp: No such file or directory
exporting 172.16.1.15:/home/nfstestdir to kernel
exportfs: internal: no supported addresses in nfs_client
exportfs: 172.16.1.15:/home/nfstestdir: No such file or directory
在客户端挂载nfs时,常用-o nolock选项(不加锁的意思)
在客户端:
[root@zjz ~]# mkdir /zhangjinzhao
[root@zjz ~]# mount -t nfs -o nolock 172.16.1.14:/tmp/ /zhangjinzhao/
还可以把要挂载的nfs目录写到客户端上的etc/fstab上 挂载时只需执行mount -a命令即可
在/etc/fstab文件中
192.168.1.14:/tmp/ /zhangjinzhao nfs defaults,nolock 0 0
这样操作的好处是以后开机会自动挂载nfs