一、NFS(network file system)
网络文件系统:在互联网中共享服务器中的文件资源(用于Linux主机共享文件的协议)。
使用nfs服务需要安装:nfs-utils 以及 rpcbind
nfs-utils : 提供nfs服务的程序
rpcbind :管理nfs所有进程端口号的程序
二、实验1:服务器与客户端共享文件
(1)
root@Server \~# dnf install nfs-utils #在服务器安装nfs服务
root@Server \~# dnf install rpcbind #在服务器安装rpc服务
root@Server \~# systemctl enable --now nfs-server.service #立即永久开机自启动nfs服务
root@Server \~# systemctl enable --now rpcbind #立即永久开机自启动rpc服务
root@Client \~# dnf install nfs-utils #在客户端安装nfs服务
root@Client \~# dnf install rpcbind #在客户端安装rpc服务
root@Client \~# systemctl enable --now rpcbind #客户端只需要开启rpc服务
(2)
root@Server \~# mkdir /nfs_share #在服务器的家目录下创建共享目录
root@Server nfs_share# rpm -ql nfs-utils #查看nfs服务产生那些文件
/etc/exports.d #nfs服务共享的配置文件(可能不会自动创建)
······
root@Server \~# man 5 exports #查看配置文件(配置文件都在5区,所以一般要加个5)
在命令行打/EXAMPLE #可以查到exports配置文件的编辑方法
sample /etc/exports file
/ master(rw) trusty(rw,no_root_squash) #编辑方法
/projects proj*.local.domain(rw)
/usr *.local.domain(ro) @trusted(rw)
/home/joe pc001(rw,all_squash,anonuid=150,anongid=100)
/pub *(ro,insecure,all_squash)
/srv/www -sync,rw server @trusted @external(ro)
/foo 2001:db8:9:e54::/64(rw) 192.0.2.0/24(rw)
/build buildhost0-9.local.domain(rw)
root@Server \~# vim /etc/exports #编辑nfs服务共享的配置文件
/nfs_share *(rw,sync,all_squash)
要共享的文件夹 IP地址(拥有的权限)
ro:只读
rw:读写
sync:同步,每进行一步操作都会共享给对方。性能弱
async:异步,在共享文件编辑好保存完后,立即发送给对方。性能强
root_squash:当客户端以root用户去访问时,隐匿root用户为nobody
no_root_squash:当客户端以root用户去访问时,不隐匿root用户(不安全)
all_squash:当客户端以任何账户去访问时,都隐匿账户为nobody(安全,推荐)
no_all_squash:当客户端以任何账户去访问时,都不隐匿账户
anonuid= anongid= :将文件的用户和工作组映射成指定的UID和GID,不指定则默认为65534(nobody)
root@Server \~# exportfs -rv #更新nfs服务
exporting *:/nfs_share
root@Client \~# showmount -e 192.168.153.100 #查看服务器的共享文件有没有共享出来
Export list for 192.168.153.100:
/nfs_share *
root@Client \~# mount 192.168.153.100:/nfs_share /mnt #将服务器下的共享文件挂载到/mnt
root@Server \~# echo "hello nfs" > /nfs_share/hellonfs #在服务器共享文件夹下创建共享文件(如:hellonfs)并在"hellp nfs"编辑到共享文件中
root@Client \~# ls /mnt #查看有没有共享到文件
hellonfs
root@Client \~# cat /mnt/hellonfs #查看共享文件中的信息
hello nfs
三、实验2:是否隐匿账户
(1)
root@Server \~# vim /etc/exports #编辑共享配置文件
/nfs_share *(rw,sync,root_squash) #隐匿root用户
root@Server \~# exportfs -rv #更新nfs服务
exporting *:/nfs_share
root@Client mnt# ll #查看/mnt下的文件详细信息
总用量 4
-rw-r--r--. 1 root root 10 4月 14 20:40 hellonfs
root@Server \~# chmod 777 /nfs_share #在/nfs_share目录时,给其他用户可读可写的权限
root@Client mnt# touch 123 #创建文件123
root@Client mnt# ll #再次查看文件信息
总用量 4
-rw-r--r--. 1 nobody nobody 0 4月 14 20:43 123 #客户端root用户创建文件的root用户被隐匿
-rw-r--r--. 1 root root 10 4月 14 20:40 hellonfs
(2)
root@Server \~# vim /etc/exports
/nfs_share *(rw,sync,no_root_squash) #不隐匿root用户
root@Server \~# exportfs -rv
exporting *:/nfs_share
root@Client mnt# touch abc #创建文件abc
root@Client mnt# ll
总用量 4
-rw-r--r--. 1 nobody nobody 0 4月 14 20:43 123
-rw-r--r--. 1 root root 0 4月 14 20:49 abc #客户端root用户创建文件的root用户不被隐匿
-rw-r--r--. 1 root root 10 4月 14 20:40 hellonfs
(3)
root@Client mnt# su xing #切换xing用户
xing@Client mnt$ touch aaaaa #创建文件aaaaa
xing@Client mnt$ ll
总用量 4
-rw-r--r--. 1 nobody nobody 0 4月 14 20:43 123
-rw-r--r--. 1 xing xing 0 4月 14 20:52 aaaaa #创建文件的xing用户不被隐匿
-rw-r--r--. 1 root root 0 4月 14 20:49 abc
-rw-r--r--. 1 root root 10 4月 14 20:40 hellonfs
root@Server \~# vim /etc/exports
/nfs_share *(rw,sync,all_squash) #隐匿所有用户
root@Server \~# exportfs -rv
exporting *:/nfs_share
xing@Client mnt$ touch bbbbb #xing用户创建文件bbbbb
xing@Client mnt$ ll
总用量 4
-rw-r--r--. 1 nobody nobody 0 4月 14 20:43 123
-rw-r--r--. 1 xing xing 0 4月 14 20:52 aaaaa
-rw-r--r--. 1 root root 0 4月 14 20:49 abc
-rw-r--r--. 1 nobody nobody 0 4月 14 20:56 bbbbb #创建文件的xing用户被隐匿
-rw-r--r--. 1 root root 10 4月 14 20:40 hellonfs
四、实验3:自动挂载
(1)
root@Client mnt# df #在客户端上查看挂载信息
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 4096 0 4096 0% /dev
······
192.168.153.100:/nfs_share 26578944 6055424 20523520 23% /mnt #被挂载共享目录信息
root@Client mnt# cd #返回家目录(如果一直处在被挂载的目录,是取消不了挂载的)
root@Client \~# umount /mnt #取消挂载
root@Client \~# df 再次查看挂载信息
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 4096 0 4096 0% /dev
······
(2)
root@Client \~# dnf install autofs -y #安装自动挂载服务
root@Client \~# cd / #进到根目录
root@Client /# ls #查看根目录下的文件
afs boot etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
root@Client /# systemctl start autofs.service #激活autofs服务
root@Client /# ls #再次查看根目录下的文件
afs boot etc lib media mnt opt root sbin sys usr
bin dev home lib64 misc net proc run srv tmp var #autofs服务会产生两个文件'misc'和'net',/net就是自动挂载的目录
(3)
root@Client /# cd /net/ #进入自动挂载目录
root@Client net# ls #查看文件时什么都没有
root@Client net# cd 192.168.153.100 #进入要被挂载的服务器,此时客户端会立刻自动挂载
root@Client 192.168.153.100# ls #再次查看文件
nfs_share
root@Client 192.168.153.100# cd nfs_share/ #进入共享目录
root@Client nfs_share# pwd #查看当前文件所处位置
/net/192.168.153.100/nfs_share
root@Client nfs_share# df #查看挂载信息
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 4096 0 4096 0% /dev
·····
192.168.153.100:/nfs_share 26578944 6055424 20523520 23% /net/192.168.153.100/nfs_share #共享目录被自动挂载
(4)
root@Client nfs_share# cd #退出共享目录
root@Client \~# #300秒内不操作与共享目录有关的事,自动挂载的目录会自动删除
root@Client \~# vim /etc/autofs.conf #编辑autofs服务的配置文件
26 timeout = 5 #默认超时时间为300秒,修改为5秒
root@Client \~# systemctl restart autofs.service #重启autofs服务
root@Client \~# cd /net/192.168.153.100/nfs_share/ #进入自动挂载服务器下的共享目录
root@Client nfs_share# df
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 4096 0 4096 0% /dev
·····
192.168.153.100:/nfs_share 26578944 6055424 20523520 23% /net/192.168.153.100/nfs_share #共享目录被自动挂载上
root@Client nfs_share# cd #退出共享目录5
root@Client \~# df #5秒后查看挂载信息
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 4096 0 4096 0% /dev
······ #共享目录自动删除(超时时间一般不修改)
五、实验4:指定自动挂载目录
root@Client \~# vim /etc/auto.master #修改指定挂载目录要进入auto.master配置文件
13 /net -hosts #将访问的主机(虚拟器)挂载到/net目录下
14 /nfs /etc/autofs.nfs_share #创建指定挂载的主目录/nfs,/etc/autofs.nfs_share是子目录的配置文件
root@Client \~# vim /etc/autofs.nfs_share #编辑子目录的配置文件
nfs_share -rw 192.168.153.100:/nfs_share
创建在主目录/nfs下的文件 可读可写 服务器的共享文件
root@Client \~# systemctl restart autofs.service #重启autofs服务
root@Client \~# ls /nfs #查看指定挂载目录/nfs