NFS:网络文件系统
NFS:实现两台linux主机之间文件共享
文件共享:一台主机的指定目录可以挂在至另一台主机的特定目录,实现文件的编辑、查看等
NFS:相对于samba部署简单
NFS服务模式:服务器端/客户端
下面实验中PC1为服务器端,IP为192.168.10.10; PC2为客户机端,IP为192.168.10.20。
1、在PC1服务器端安装NFS
[root@PC1 ~]# yum install nfs-utils -y Loaded plugins: langpacks, product-id, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. rhel7 | 4.1 kB 00:00 Package 1:nfs-utils-1.3.0-0.el7.x86_64 already installed and latest version Nothing to do ## 默认已经安装
2、在PC1服务器端清空防火墙策略
[root@PC1 ~]# iptables -F [root@PC1 ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
3、在PC1服务器端创建共享目录,并给与足够的权限
[root@PC1 ~]# mkdir /pc1share [root@PC1 ~]# chmod -R 777 /pc1share/ [root@PC1 pc1share]# echo 'i am pc1' > pc1.txt
4、 在PC1服务器端修改NFS配置文件
[root@PC1 ~]# vim /etc/exports /pc1share 192.168.10.*(rw,sync,root_squash) ## 定义共享目录,针对的IP范围,读写权限,数据同步,NFS用户映射为匿名用户
5、在PC1服务器端启动RPC服务(远程过程调用?),NFS服务
[root@PC1 ~]# systemctl restart rpcbind.service [root@PC1 ~]# systemctl enable rpcbind [root@PC1 ~]# systemctl restart nfs-server [root@PC1 ~]# systemctl enable nfs-server.service ln -s '/usr/lib/systemd/system/nfs-server.service' '/etc/systemd/system/nfs.target.wants/nfs-server.service' [root@PC1 ~]# systemctl status nfs nfs-server.service - NFS Server Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled) Active: active (exited) since Wed 2020-12-16 01:18:53 CST; 29s ago Main PID: 4156 (code=exited, status=0/SUCCESS) CGroup: /system.slice/nfs-server.service Dec 16 01:18:53 PC1 systemd[1]: Starting NFS Server... Dec 16 01:18:53 PC1 systemd[1]: Started NFS Server.
6、在PC2客户机端测试与PC1主机的连通性
[root@PC2 Desktop]# ifconfig | head -n 3 eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.20 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::20c:29ff:fe25:bb3e prefixlen 64 scopeid 0x20<link> [root@PC2 Desktop]# ping -c 3 192.168.10.10 PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data. 64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=0.207 ms 64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.231 ms 64 bytes from 192.168.10.10: icmp_seq=3 ttl=64 time=0.208 ms --- 192.168.10.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms rtt min/avg/max/mdev = 0.207/0.215/0.231/0.016 ms
7、在PC2客户端查看PC1服务器的共享信息
[root@PC2 Desktop]# showmount -e 192.168.10.10 Export list for 192.168.10.10: /pc1share 192.168.10.*
8、在PC2客户机端创建挂载点
[root@PC2 Desktop]# mkdir /pc2dir
9、在PC2客户机端修改开机自动挂载文件
[root@PC2 Desktop]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Wed Dec 2 16:46:09 2020 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/rhel-root / xfs defaults 1 1 UUID=5cc50fd7-fef7-4902-a0f6-d583b437954d /boot xfs defaults 1 2
/dev/mapper/rhel-swap swap swap defaults 0 0
192.168.10.10:/pc1share /pc2dir nfs defaults 0 0
10、在PC2客户机端挂载
[root@PC2 Desktop]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 18G 2.9G 15G 17% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 80K 994M 1% /dev/shm tmpfs 994M 8.8M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sda1 497M 119M 379M 24% /boot [root@PC2 Desktop]# mount -a [root@PC2 Desktop]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 18G 2.9G 15G 17% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 80K 994M 1% /dev/shm tmpfs 994M 8.8M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sda1 497M 119M 379M 24% /boot 192.168.10.10:/pc1share 18G 3.0G 15G 17% /pc2dir
11、验证
[root@PC2 Desktop]# cd /pc2dir/ [root@PC2 pc2dir]# ls pc1.txt [root@PC2 pc2dir]# cat pc1.txt i am pc1 [root@PC2 pc2dir]# echo 'i am pc2' > pc2.txt
[root@PC1 pc1share]# pwd /pc1share [root@PC1 pc1share]# ls pc1.txt pc2.txt [root@PC1 pc1share]# cat pc2.txt i am pc2
以上实验实现了部署NFS在两台linux主机之间进行文件共享,配置较samba服务简单。