一,概述
网络文件系统 (NFS) 是 Unix 系统和网络附加存储文件管理器常用的网络文件系统 , 允许多个客户端通过网络共享文件访问。它
可用于提供对共享二进制目录的访问 , 也可用于允许用户在同一工作组中从不同客户端访问其文件
NFS 协议有多个版本 :Linux 支持版本 4 、版本 3 和版本 2, 而大多数系统管理员熟悉的是 NFSv3 。默认情况下 , 该协议并不
安全 , 但是更新的版本 ( 如 NFSv4) 提供了对更安全的身份验证的支持 , 甚至可以通过 kerberos 进行加密
二,nfs安装与基本操作
1,安装,共享
[root@server ~]# yum install nfs-utils -y >>>>>服务端客户端安装nfs-utils
[root@client ~]# yum install nfs-utils -y
[root@client ~]# showmount -e 172.25.254.200 >>>>>客户端查看服务端的共享文件,为空
Export list for 172.25.254.200:
[root@server ~]# systemctl start nfs >>>>>服务端启动nfs
[root@server ~]# systemctl enable nfs-server
[root@server ~]# vim /etc/exports >>>>>>写共享文件
[root@server ~]# cat /etc/exports >>>>>> *表示所有人
/mnt *(sync,ro) >>>>>>共享/mnt目录,允许进行数据传输,只读挂载
[root@server ~]# exportfs -rv >>>>>>服务端刷新共享文件
exporting *:/mnt >>>>>> *表示所有人
[root@client mnt]# showmount -e 172.25.254.200 >>>>>>显示服务端的共享文件
Export list for 172.25.254.200:
/mnt *
[root@server ~]# cd /mnt/ >>>>>>查看服务端共享目录的文件
[root@server mnt]# ls
file1 file2 file3 file4 file5
[root@client ~]# mount 172.25.254.200:/mnt/ /mnt/ >>>>>>客户端把服务端的/mnt目录挂载在/mnt下
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3152376 7321524 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17036 925624 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
172.25.254.200:/mnt 10473984 3159552 7314432 31% /mnt
[root@client ~]# cd /mnt/
[root@client mnt]# ls >>>>>>客户端看到了服务端共享的文件
file1 file2 file3 file4 file5
[root@client mnt]# rm -rf * >>>>>>由于是只读挂载,删除不了
rm: cannot remove ‘file1’: Read-only file system
rm: cannot remove ‘file2’: Read-only file system
rm: cannot remove ‘file3’: Read-only file system
rm: cannot remove ‘file4’: Read-only file system
rm: cannot remove ‘file5’: Read-only file system
[root@server mnt]# vim /etc/exports
[root@server mnt]# cat /etc/exports >>>>>>修改为可读可写挂载
/mnt *(sync,rw)
[root@server mnt]# systemctl restart nfs >>>>>>
[root@server mnt]# ls
file1 file2 file3 file4 file5
[root@client mnt]# rm -rf * >>>>>>还是无法删除,是文件系统的权限不够
rm: cannot remove ‘file1’: Permission denied
rm: cannot remove ‘file2’: Permission denied
rm: cannot remove ‘file3’: Permission denied
rm: cannot remove ‘file4’: Permission denied
rm: cannot remove ‘file5’: Permission denied
[root@server mnt]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 3 08:14 file1
-rw-r--r--. 1 root root 0 Jun 3 08:14 file2
-rw-r--r--. 1 root root 0 Jun 3 08:14 file3
-rw-r--r--. 1 root root 0 Jun 3 08:14 file4
-rw-r--r--. 1 root root 0 Jun 3 08:14 file5
[root@server mnt]# chmod -R 777 /mnt/ >>>>>>服务端给满权限
[root@client mnt]# rm -rf * >>>>>>成功删除
[root@client mnt]# ls
[root@client mnt]# touch file6 >>>>>>客户端可以创建文件
[root@client mnt]# ls
file6
[root@server mnt]# ls >>>>>>服务端也可以查看到
file6
[root@client mnt]# ll
total 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 Jun 5 03:30 file6 >>>>>>而且这个文件的所有者和组都是nfsbody
2,no_root_squash登陆nfs时获得root的身份
[root@server mnt]# vim /etc/exports
[root@server mnt]# cat /etc/exports
/mnt *(sync,rw,no_root_squash) >>>>>>>对于改目录,具有root权限
[root@server mnt]# systemctl restart nfs >>>>>>>可以不用重启,但是要刷新
[root@server mnt]# exportfs -rv
[root@client ~]# umount /mnt/
[root@client ~]# mount 172.25.254.200:/mnt /mnt/ >>>>>>>客户端重新挂载
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3152732 7321168 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17008 925652 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
172.25.254.200:/mnt 10473984 3159296 7314688 31% /mnt
[root@client ~]# showmount -e 172.25.254.200
Export list for 172.25.254.200:
/mnt *
[root@client ~]# cd /mnt/
[root@client mnt]# touch file7 >>>>>>>以root身份创建文件
[root@client mnt]# ll
total 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 Jun 5 03:30 file6
-rw-r--r--. 1 root root 0 Jun 5 03:45 file7 >>>>>>>所有者和组都是root
3,用户登陆时获得指定的身份
[root@server mnt]# vim /etc/exports
[root@server mnt]# cat /etc/exports
/mnt *(sync,rw,anonuid=1000,anongid=1000) >>>>>>>用户登陆时身份uid=1000,gid=10000
[root@server mnt]# exportfs -rv >>>>>>>刷新
exporting *:/mnt
[root@client mnt]# touch file8
[root@client mnt]# ll
total 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 Jun 5 03:30 file6
-rw-r--r--. 1 root root 0 Jun 5 03:45 file7
-rw-r--r--. 1 student student 0 Jun 5 03:47 file8 >>>>>>>客户端uid=1000,gid=1000的时student用户
[root@client mnt]# id student >>>>>>>查看
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
5,特定ip访问权限
[root@server westos]# vim /etc/exports
[root@server westos]# cat /etc/exports
/mnt *(sync,rw,anonuid=1000,anongid=1000)
/westos 172.25.254.156(sync,rw) >>>>>>>只有172.25.254.156这台主机才可以访问
[root@server westos]# exportfs -rv
exporting 172.25.254.156:/westos
exporting *:/mnt
[root@client ~]# showmount -e 172.25.254.200
Export list for 172.25.254.200:
/mnt *
/westos 172.25.254.156
[root@client ~]# mount 172.25.254.200:/westos /mnt/ >>>>>>>文件生效,100无法挂载
mount.nfs: access denied by server while mounting 172.25.254.200:/westos
[root@156 ~]# mount 172.25.254.200:/westos /mnt/ >>>>>>>156可以挂载,可以查看
[root@156 ~]# cd /mnt/
[root@156 mnt]# ls
file filetest
6,特定网段访问
[root@server ~]# vim /etc/exports
[root@server ~]# cat /etc/exports
/mnt *(sync,rw,anonuid=1000,anongid=1000)
/westos 172.25.254.0/24(sync) >>>>>>>该目录只能由这个网段的ip访问
[root@server ~]# exportfs -rv
exporting 172.25.254.0/24:/westos
exporting *:/mnt
注意:man 5 exports有很多特定条件访问的例子
nfs的读写布尔值是默认打开的
[root@server ~]# getenforce
Enforcing
[root@server ~]# getsebool -a | grep nfs
cobbler_use_nfs --> off
ftpd_use_nfs --> off
git_cgi_use_nfs --> off
git_system_use_nfs --> off
httpd_use_nfs --> off
ksmtuned_use_nfs --> off
logrotate_use_nfs --> off
mpd_use_nfs --> off
nfs_export_all_ro --> on >>>>>>
nfs_export_all_rw --> on >>>>>>
nfsd_anon_write --> off
openshift_use_nfs --> off
polipo_use_nfs --> off
samba_share_nfs --> off
sanlock_use_nfs --> off
sge_use_nfs --> off
use_nfs_home_dirs --> off
virt_sandbox_use_nfs --> off
virt_use_nfs --> off
xen_use_nfs --> off
二,用时自动挂载,闲置时自动取消挂载
[root@server ~]# vim /etc/exports
[root@server ~]# cat /etc/exports
/mnt *(sync,rw,anonuid=1000,anongid=1000)
/westos *(sync,rw
[root@server ~]# exportfs -rv >>>>>>服务端建立两个分享目录
exporting *:/westos
exporting *:/mnt
[root@client ~]# showmount -e 172.25.254.200 >>>>>>客户端可以查看到
Export list for 172.25.254.200:
/westos *
/mnt *
[root@client ~]# yum install autofs -y >>>>>>>客户端安装自动挂载软件autofs
[root@client ~]# systemctl start autofs >>>>>>>启动
[root@client ~]# systemctl enable autofs
ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'
[root@client ~]# ls / >>>>>>>启动之后在根目录下就会出现一个net目录
bin dev home lib64 misc net proc run srv tmp var
boot etc lib media mnt opt root sbin sys usr
[root@client ~]# cd /net/ >>>>>>>net目录什么都没有
[root@client net]# ls
[root@client net]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3158460 7315440 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17012 925648 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup >>>>>>>因为此时还没有进入共享文件,所以还没有挂载
[root@client net]# cd 172.25.254.200 >>>>>>>可以切进去!!!!!
[root@client 172.25.254.200]# pwd
/net/172.25.254.200
[root@client 172.25.254.200]# ls >>>>>>>172.25.254.200这个目录下面有共享服务端共享目录
mnt westos
[root@client 172.25.254.200]# df
Filesystem 1K-blocks Used Available Use% Mounted on >>>>>>>依然没有挂载
/dev/vda1 10473900 3158460 7315440 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17012 925648 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
[root@client 172.25.254.200]# cd westos/
[root@client westos]# ls >>>>>>>>访问westos共享文件
file filetest
[root@client westos]# df >>>>>>>>挂载上,和手动挂载是,只不过挂载点不一样
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3158000 7315900 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17012 925648 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
172.25.254.200:/westos 10473984 3159808 7314176 31% /net/172.25.254.200/westos
[root@client westos]# vim /etc/sysconfig/autofs >>>>>>设置闲置时间为5s,在不用共享文件5s之后自动卸载
TIMEOUT=5 >>>>>>>默认是300s
[root@client westos]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3158024 7315876 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17012 925648 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
172.25.254.200:/westos 10473984 3159296 7314688 31% /net/172.25.254.200/westos
[root@client westos]# cd >>>>>>>>不要在共享目录里面,否则系统认为还在使用
[root@client ~]# systemctl restart autofs.service >>>>>>>>重启系统
[root@client ~]# df >>>>>>>>等待5s之后,自动取消挂载
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3158004 7315896 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17012 925648 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
注意:就算不在共享目录里面,也不能在timeout时间内df一直查看,这是不算作闲置时间的
三,修改自动挂载的挂载点
1,只读挂载
[root@server ~]# systemctl start nfs
[root@client ~]# showmount -e 172.25.254.200 >>>>>一定要开启服务,确定能看到共享文件
Export list for 172.25.254.200:
/westos *
/mnt *
[root@server ~]# ls -ld /westos
drwxr-xr-x. 2 root root 32 Jun 5 04:14 /westos
[root@server ~]# chmod 777 /westos/ >>>>>给满权限
[root@client ~]# vim /etc/auto.master
/misc /etc/auto.misc
/nfs /etc/auto.westos >>>>>>最终挂载点的上层目录 指定要挂载的文件
#
# NOTE: mounts done from a hosts map will be mounted with the
# "nosuid" and "nodev" options unless the "suid" and "dev"
# options are explicitly given.
[root@client ~]# ls /etc/auto.westos
ls: cannot access /etc/auto.westos: No such file or directory
[root@client ~]# vim /etc/auto.westos >>>>>>建立指定要挂载的文件
[root@client ~]# cat /etc/auto.westos
westos -ro 172.25.254.200:/westos >>>>>>最终挂载点上层目录的剩余部分 只读挂载200主机的/westos
>>>>>>就是挂载/nfs/westos下
[root@client ~]# systemctl restart autofs.service >>>>>>>重启服务
[root@client ~]# cd /nfs/
[root@client nfs]# ls
[root@client nfs]# cd westos
[root@client westos]# ls
file filetest
[root@client westos]# df >>>>>成功挂载
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3157260 7316640 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17012 925648 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
172.25.254.200:/westos 10473984 3159296 7314688 31% /nfs/westos
[root@client westos]# mount >>>>>可以看到挂载的详细信息
172.25.254.200:/westos on /nfs/westos type nfs4 (ro,relatime,vers=4.0,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.25.254.100,local_lock=none,addr=172.25.254.200)
2,可读可写挂载
[root@client ~]# vim /etc/auto.master
/misc /etc/auto.misc
/nfs /etc/auto.westos >>>>>>最终挂载点的上层目录 指定挂载的文件
#
# NOTE: mounts done from a hosts map will be mounted with the
# "nosuid" and "nodev" options unless the "suid" and "dev"
# options are explicitly given.
[root@client ~]# vim /etc/auto.westos
[root@client ~]# cat /etc/auto.westos
westos -rw,vers=3 172.25.254.200:/westos >>>>>可读可写挂载
[root@client ~]# systemctl restart autofs.service
[root@client ~]# cd /nfs/
[root@client nfs]# ls
[root@client nfs]# cd westos
[root@client westos]# ls
file filetest
[root@client westos]# df >>>>>成功挂载
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3156796 7317104 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17012 925648 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
172.25.254.200:/westos 10473984 3159296 7314688 31% /nfs/westos
[root@client westos]# touch file1 >>>>>>可以创建文件
[root@client westos]# ls
file file1 filetest
[root@client westos]# mount >>>>>>查看挂载信息
172.25.254.200:/westos on /nfs/westos type nfs (rw,relatime,vers=3,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=172.25.254.200,mountvers=3,mountport=20048,mountproto=udp,local_lock=none,addr=172.25.254.200)