NFS 概念
网络文件系统 (NFS) 是 Unix 系统和网络附加存储文件管理器常用的网络文件系统 , 允许多个客户端通过网络共享文件访问。它可用于提供对共享二进制目录的访问 , 也可用于允许用户在同一工作组中从不同客户端访问其文件NFS 协议有多个版本 :Linux 支持版本 4 、版本 3 和版本 2, 而大多数系统管理员熟悉的是 NFSv3 。默认情况下 , 该协议并不安全 , 但是更新的版本 ( 如 NFSv4) 提供了对更安全的身份验证的支持 , 甚至可以通过 kerberos 进行加密NFS 服务器配置
1:安装启动服务
[root@localhost ~]# yum install nfs-utils -y
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl status nfs
nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled)
Active: active (exited) since Mon 2017-08-21 07:48:42 EDT; 4s ago
Process: 7286 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
Process: 7285 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Main PID: 7286 (code=exited, status=0/SUCCESS)
Aug 21 07:48:41 localhost systemd[1]: Starting NFS server and services...
Aug 21 07:48:42 localhost systemd[1]: Started NFS server and services.
[root@localhost ~]# systemctl enable nfs-server
ln -s '/usr/lib/systemd/system/nfs-server.service' '/etc/systemd/system/multi-user.target.wants/nfs-server.service'
[root@localhost ~]# vim /etc/exports
[root@localhost ~]# showmount -e 172.25.254.111
Export list for 172.25.254.111:
[root@localhost ~]# exportfs -rv
exporting *:/westosshare
[root@localhost ~]# showmount -e 172.25.254.111
Export list for 172.25.254.111:
/westosshare *
在另一个电脑上挂载并查看
[root@localhost ~]# showmount -e 172.25.254.111
Export list for 172.25.254.111:
/westosshare *
[root@localhost ~]# mount 172.25.254.111:/westosshare /mnt
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 7620588 2853312 73% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 84 942576 1% /dev/shm
tmpfs 942660 17028 925632 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2399 451780 1% /home
/dev/loop0 3947824 3947824 0 100% /var/www/html/rhel7.2
172.25.254.111:/westosshare 10473984 3207680 7266304 31% /mnt
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
westosfile1 westosfile2 westosfile3
[root@localhost mnt]# touch file
touch: cannot touch ‘file’: Read-only file system
2:设置其他用户可以建立文件
[root@localhost ~]# vim /etc/exports
[root@localhost ~]# exportfs -rv
exporting 172.25.254.211:/westosshare
exporting *:/westosshare
用另一台电脑测试
[root@localhost ~]# mount 172.25.254.111:/westosshare /mnt
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
westosfile1 westosfile2 westosfile3
[root@localhost mnt]# touch file
[root@localhost mnt]# ls
file westosfile1 westosfile2 westosfile3
3:文件共享的自动挂载设置
[root@localhost ~]# yum install autofs -y
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package autofs.x86_64 1:5.0.7-54.el7 will be installed
--> Processing Dependency: libhesiod.so.0()(64bit) for package: 1:autofs-5.0.7-54.el7.x86_64
--> Running transaction check
---> Package hesiod.x86_64 0:3.2.1-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
============================================================================
Package Arch Version Repository Size
============================================================================
Installing:
autofs x86_64 1:5.0.7-54.el7 search 793 k
Installing for dependencies:
hesiod x86_64 3.2.1-3.el7 search 30 k
Transaction Summary
============================================================================
Install 1 Package (+1 Dependent package)
Total download size: 823 k
Installed size: 5.1 M
Downloading packages:
(1/2): autofs-5.0.7-54.el7.x86_64.rpm | 793 kB 00:00
(2/2): hesiod-3.2.1-3.el7.x86_64.rpm | 30 kB 00:00
----------------------------------------------------------------------------
Total 1.7 MB/s | 823 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : hesiod-3.2.1-3.el7.x86_64 1/2
Installing : 1:autofs-5.0.7-54.el7.x86_64 2/2
Verifying : 1:autofs-5.0.7-54.el7.x86_64 1/2
Verifying : hesiod-3.2.1-3.el7.x86_64 2/2
Installed:
autofs.x86_64 1:5.0.7-54.el7
Dependency Installed:
hesiod.x86_64 0:3.2.1-3.el7
Complete!
[root@localhost ~]# systemctl start autofs.service
[root@localhost ~]# systemctl status autofs.service
autofs.service - Automounts filesystems on demand
Loaded: loaded (/usr/lib/systemd/system/autofs.service; disabled)
Active: active (running) since Mon 2017-08-21 08:29:24 EDT; 10s ago
Process: 4744 ExecStart=/usr/sbin/automount $OPTIONS --pid-file /run/autofs.pid (code=exited, status=0/SUCCESS)
Main PID: 4746 (automount)
CGroup: /system.slice/autofs.service
└─4746 /usr/sbin/automount --pid-file /run/autofs.pid
Aug 21 08:29:24 localhost systemd[1]: Started Automounts filesystems on ....
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# ls -ld /net
drwxr-xr-x 2 root root 0 Aug 21 08:29 /net
[root@localhost ~]# cd /net
[root@localhost net]# ls
[root@localhost net]# ls -a
. ..
[root@localhost net]# cd 172.25.254.111
[root@localhost 172.25.254.111]# pwd
/net/172.25.254.111
[root@localhost 172.25.254.111]# ls
westosshare
[root@localhost 172.25.254.111]# cd westosshare/
[root@localhost westosshare]# ls
file ggg hhh westosfile1 westosfile2 westosfile3
[root@localhost westosshare]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 7626132 2847768 73% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 84 942576 1% /dev/shm
tmpfs 942660 17032 925628 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2399 451780 1% /home
/dev/loop0 3947824 3947824 0 100% /var/www/html/rhel7.2
172.25.254.111:/westosshare 10473984 3207936 7266048 31% /net/172.25.254.111/westosshare
设置推出 net 后的自动取消挂载时间
[root@localhost ~]# vim /etc/autofs.conf
[root@localhost ~]# systemctl restart autofs.service
[root@localhost ~]# cd /net
[root@localhost net]# cd 172.25.254.111
[root@localhost 172.25.254.111]# ls
westosshare
[root@localhost 172.25.254.111]# cd westosshare/
[root@localhost westosshare]# ls
file ggg hhh westosfile1 westosfile2 westosfile3
[root@localhost westosshare]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 7626132 2847768 73% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 84 942576 1% /dev/shm
tmpfs 942660 17032 925628 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2399 451780 1% /home
/dev/loop0 3947824 3947824 0 100% /var/www/html/rhel7.2
172.25.254.111:/westosshare 10473984 3207936 7266048 31% /net/172.25.254.111/westosshare
[root@localhost westosshare]# cd
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 7626132 2847768 73% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 84 942576 1% /dev/shm
tmpfs 942660 17032 925628 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2399 451780 1% /home
/dev/loop0 3947824 3947824 0 100% /var/www/html/rhel7.2
172.25.254.111:/westosshare 10473984 3207936 7266048 31% /net/172.25.254.111/westosshare
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 7626132 2847768 73% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 84 942576 1% /dev/shm
tmpfs 942660 17032 925628 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2399 451780 1% /home
/dev/loop0 3947824 3947824 0 100% /var/www/html/rhel7.2
autofs
在客户端可以自动挂载卸载的服务
vim /etc/auto.master
vim /etc/auto.nfs