Linux中nfs网络文件共享

简介:

nfs:网络文件共享
[root@server ~]# yum repolist 
Loaded plugins: langpacks
rhel_dvd | 4.1 kB 00:00 
(1/2): rhel_dvd/group_gz | 134 kB 00:00 
(2/2): rhel_dvd/primary_db | 3.4 MB 00:00 
repo id repo name status
rhel_dvd Remote classroom copy of dvd 4,305
repolist: 4,305

服务端:

[root@server ~]# yum install nfs-utils -y #安装服务
Linux中nfs网络文件共享
[root@server ~]# systemctl start nfs #开启服务
Linux中nfs网络文件共享
[root@server ~]# netstat -antlupe
tcp6 0 0 :::111 :::* LISTEN 0 20201 1280/rpcbind 
Linux中nfs网络文件共享

客户端:

[root@client ~]# showmount -e 172.25.254.125 #发现172.25.254.125上的信息
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host) #No route to host 网络不可到达,有两种情况1.网络不通;2.火墙
Linux中nfs网络文件共享
[root@client ~]# ping 172.25.254.125
PING 172.25.254.125 (172.25.254.125) 56(84) bytes of data.
64 bytes from 172.25.254.125: icmp_seq=1 ttl=64 time=0.207 ms
64 bytes from 172.25.254.125: icmp_seq=2 ttl=64 time=0.218 ms 
#网络通畅,所以可以确定是火墙影响

Linux中nfs网络文件共享

服务端:

[root@server ~]# firewall-cmd --list-all #列出所有火墙策略
public (default, active)
interfaces: eth0
sources: 
services: dhcpv6-client ssh
Linux中nfs网络文件共享
[root@server ~]# firewall-cmd --get-services #列出所有预设服务
nfs rpc-bind mountd 
Linux中nfs网络文件共享
[root@server ~]# firewall-cmd --permanent --add-service=nfs
success

[root@server ~]# firewall-cmd --reload 
Linux中nfs网络文件共享
客户端:

[root@client ~]# showmount -e 172.25.254.125
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host) #端口没开
Linux中nfs网络文件共享
服务端:

[root@server ~]# firewall-cmd --permanent --add-service=rpc-bind #rpc-bind分配此接口
success

[root@server ~]# firewall-cmd --reload #重新加载
success
Linux中nfs网络文件共享
客户端:

[root@client ~]# showmount -e 172.25.254.125 
rpc mount export: RPC: Unable to receive; errno = No route to host #Unable to receive端口能访问但不能使用
Linux中nfs网络文件共享
服务端:

nfs需要挂载才能使用

[root@server ~]# firewall-cmd --permanent --add-service=mountd 
success
[root@server ~]# firewall-cmd --reload 
success
Linux中nfs网络文件共享
客户端:

[root@client ~]# showmount -e 172.25.254.125
Export list for 172.25.254.125:
Linux中nfs网络文件共享
服务端:

建立共享目录:
[root@server ~]# mkdir -p /westos/nfs 
Linux中nfs网络文件共享
[root@server ~]# vim /etc/exports
/westos/nfs (sync) #:目录共享给所有人,(sync)服务端与客户端时时同步
Linux中nfs网络文件共享
Linux中nfs网络文件共享

[root@server ~]# exportfs -rv #刷新,注意不能重启服务,会卡
exporting *:/westos/nfs
Linux中nfs网络文件共享
客户端:

[root@client ~]# showmount -e 172.25.254.125 #发现172.25.254.125信息,目录共享成功
Export list for 172.25.254.125:
/westos/nfs *
Linux中nfs网络文件共享
[root@client ~]# mount 172.25.254.125:/westos/nfs /mnt #执行挂载动作,使用绝对路径172.25.254.125:/westos/nfs,/westos/nfs是172.25.254.125主机上共享的目录
Linux中nfs网络文件共享
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3153212 7320688 31% /
devtmpfs 481120 0 481120 0% /dev
tmpfs 496708 472 496236 1% /dev/shm
tmpfs 496708 13104 483604 3% /run
tmpfs 496708 0 496708 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2339 451840 1% /home
172.25.254.125:/westos/nfs 10473984 3149184 7324800 31% /mnt

Linux中nfs网络文件共享
以上挂载不能解决占用问题,不能实现用的时候自动挂载,不用的时候自动卸载

客户端:

[root@client ~]# yum install autofs #安装此软件实现自动挂载自动卸载
Linux中nfs网络文件共享
[root@client ~]# ls -ld /net #默认无此目录
ls: cannot access /net: No such file or directory
Linux中nfs网络文件共享
[root@client ~]# systemctl start autofs #重启服务
Linux中nfs网络文件共享
[root@client ~]# ls -ld /net #重启服务后,/net自动建立
drwxr-xr-x 2 root root 0 Dec 9 07:57 /net
Linux中nfs网络文件共享
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3179800 7294100 31% /
devtmpfs 481120 0 481120 0% /dev
tmpfs 496708 472 496236 1% /dev/shm
tmpfs 496708 13108 483600 3% /run
tmpfs 496708 0 496708 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2339 451840 1% /home
Linux中nfs网络文件共享
[root@client ~]# cd /net/
实现自动挂载:
[root@client net]# cd 172.25.254.125
[root@client 172.25.254.125]# cd westos/ #切换到共享目录中
[root@client westos]# ls
nfs
[root@client westos]# cd nfs/
Linux中nfs网络文件共享
[root@client nfs]# df #默认挂载点/net/172.25.254.125/westos/nfs ,共享目录(绝对路径):172.25.254.125:/westos/nfs
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3180052 7293848 31% /
devtmpfs 481120 0 481120 0% /dev
tmpfs 496708 472 496236 1% /dev/shm
tmpfs 496708 13108 483600 3% /run
tmpfs 496708 0 496708 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2339 451840 1% /home
172.25.254.125:/westos/nfs 10473984 3150336 7323648 31% /net/172.25.254.125/westos/nfs
Linux中nfs网络文件共享
查看服务配置文件:
[root@client nfs]# rpm -qc autofs
/etc/sysconfig/autofs
Linux中nfs网络文件共享
[root@client nfs]# vim /etc/sysconfig/autofs #此服务配置文件
TIMEOUT=300 #默认300s实现自动卸载和挂载,该参数可以进行修改
Linux中nfs网络文件共享
Linux中nfs网络文件共享
[root@client nfs]# vim /etc/sysconfig/autofs
TIMEOUT=3 #自动卸载3s
Linux中nfs网络文件共享
Linux中nfs网络文件共享
[root@client ~]# systemctl restart autofs.service 
Linux中nfs网络文件共享
[root@client ~]# df #检测设备挂载
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3179540 7294360 31% /
devtmpfs 481120 0 481120 0% /dev
tmpfs 496708 472 496236 1% /dev/shm
tmpfs 496708 13108 483600 3% /run
tmpfs 496708 0 496708 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2339 451840 1% /home
Linux中nfs网络文件共享
Linux中nfs网络文件共享
Linux中nfs网络文件共享
客户端自行指定挂载点:

[root@client ~]# vim /etc/auto.master
8 /westos/linux /etc/auto.nfs #/westos/linux指定挂载点的上层目录,最终目录写在/etc/auto.nfs文件中
Linux中nfs网络文件共享
Linux中nfs网络文件共享
[root@client ~]# vim /etc/auto.nfs
nfs -ro 172.25.254.125:/westos/nfs #最终挂载点是nfs,所以172.25.254.125:/westos/nfs 只读挂载在/westos/linux/nfs ,ro:只读挂载
Linux中nfs网络文件共享
Linux中nfs网络文件共享
[root@client ~]# systemctl restart autofs.service 
Linux中nfs网络文件共享
[root@client ~]# cd /westos/
[root@client westos]# ls
linux
[root@client westos]# cd linux/
[root@client linux]# cd nfs
Linux中nfs网络文件共享
[root@client nfs]# df #指定挂载点/westos/linux/nfs
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3179544 7294356 31% /
devtmpfs 481120 0 481120 0% /dev
tmpfs 496708 472 496236 1% /dev/shm
tmpfs 496708 13108 483600 3% /run
tmpfs 496708 0 496708 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2339 451840 1% /home
172.25.254.125:/westos/nfs 10473984 3149312 7324672 31% /westos/linux/nfs
Linux中nfs网络文件共享
[root@client ~]# vim /etc/auto.nfs 
nfs -rw,noatime 172.25.254.125:/westos/nfs #noatime:在文件被浏览时忽略时间更新
Linux中nfs网络文件共享
Linux中nfs网络文件共享
[root@client ~]# systemctl restart autofs.service 
Linux中nfs网络文件共享
[root@client ~]# cd /net/ 
[root@client net]# cd 172.25.254.125
[root@client 172.25.254.125]# ls
westos
[root@client 172.25.254.125]# cd westos/
[root@client westos]# cd nfs/
[root@client nfs]# touch file 
touch: cannot touch ‘file’: Read-only file system #不能建立文件,文件系统对我不可写
Linux中nfs网络文件共享
服务端:

[root@server ~]# vim /etc/exports

/westos/nfs *(sync,rw) #所有人对/westos/nfs目录可写
Linux中nfs网络文件共享
Linux中nfs网络文件共享

[root@server ~]# exportfs -rv #刷新
exporting *:/westos/nfs
Linux中nfs网络文件共享
客户端

[root@client nfs]# touch file
touch: cannot touch ‘file’: Permission denied #建立文件权力被禁止(权限不够)
Linux中nfs网络文件共享
服务端:

[root@server ~]# ls -ld /westos/nfs/ #列出目录的属性
drwxr-xr-x. 2 root root 6 Dec 9 07:42 /westos/nfs/
[root@server ~]# chmod 777 /westos/nfs/ #更改权限可读可写
[root@server ~]# ls -ld /westos/nfs/
drwxrwxrwx. 2 root root 6 Dec 9 07:42 /westos/nfs/
Linux中nfs网络文件共享
客户端:

[root@client nfs]# touch file #建立文件
[root@client nfs]# ls
file
Linux中nfs网络文件共享
[root@client nfs]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 9 08:47 file #在客户端建立的文件用户身份是服务端的nfsnobody

Linux中nfs网络文件共享
客户端:

[root@server ~]# useradd westos
[root@server ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos)
Linux中nfs网络文件共享
[root@server ~]# vim /etc/exports
/westos/nfs (sync,rw,anonuid=1001,anongid=1001) #所有人使用该目录的用户身份的uid是1001,用户组gid是1001
Linux中nfs网络文件共享
Linux中nfs网络文件共享
[root@server ~]# exportfs -rv
exporting 
:/westos/nfs
Linux中nfs网络文件共享

客户端:

[root@client nfs]# touch file1
Linux中nfs网络文件共享
[root@client nfs]# ls -l
Linux中nfs网络文件共享

-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 9 08:47 file
-rw-r--r-- 1 1001 1001 0 Dec 9 08:56 file1 #建立文件的用户在客户端是1001

[root@client nfs]# useradd haha
[root@client nfs]# id haha
uid=1001(haha) gid=1001(haha) groups=1001(haha)
Linux中nfs网络文件共享
[root@client nfs]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 9 08:47 file
-rw-r--r-- 1 haha haha 0 Dec 9 08:56 file1 #建立文件时用户身份uid是1001的是客户端的haha
Linux中nfs网络文件共享
服务端:

[root@server ~]# vim /etc/exports
/westos/nfs (sync,rw,no_root_squash) #当客户端用超级用户身份使用目录时保持超级用户身份
Linux中nfs网络文件共享
Linux中nfs网络文件共享
[root@server ~]# exportfs -rv
exporting 
:/westos/nfs
Linux中nfs网络文件共享
客户端:

[root@client nfs]# touch file2
[root@client nfs]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 9 08:47 file
-rw-r--r-- 1 haha haha 0 Dec 9 08:56 file1
-rw-r--r-- 1 root root 0 Dec 9 09:06 file2 #建立文件保持root身份
Linux中nfs网络文件共享
服务端:

[root@server ~]# vim /etc/exports
/westos/nfs 172.25.254.225(sync,rw,no_root_squash) (sync,ro) #只有172.25.254.225使用该目录有读写权力,并且以超级身份使用目录保持超级用户身份,其他所有用户只能只读挂载
Linux中nfs网络文件共享
Linux中nfs网络文件共享
[root@server ~]# exportfs -rv
exporting 172.25.254.225:/westos/nfs
exporting 
:/westos/nfs
Linux中nfs网络文件共享
客户端:

[root@client nfs]# touch file3
[root@client nfs]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 9 08:47 file
-rw-r--r-- 1 haha haha 0 Dec 9 08:56 file1
-rw-r--r-- 1 root root 0 Dec 9 09:06 file2
-rw-r--r-- 1 root root 0 Dec 9 09:13 file3 #172.25.254.225建立文件是以超级用户身份
Linux中nfs网络文件共享







本文转自Uniqueh51CTO博客,原文链接:http://blog.51cto.com/13363488/2050017 ,如需转载请自行联系原作者


相关文章
|
19天前
|
安全 Linux 网络安全
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
58 0
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
|
5月前
|
安全 网络协议 Linux
Linux网络应用层协议展示:HTTP与HTTPS
此外,必须注意,从HTTP迁移到HTTPS是一项重要且必要的任务,因为这不仅关乎用户信息的安全,也有利于你的网站评级和粉丝的信心。在网络世界中,信息的安全就是一切,选择HTTPS,让您的网站更加安全,使您的用户满意,也使您感到满意。
156 18
|
5月前
|
Linux 数据安全/隐私保护
使用Linux命令行接入无线网络Wi-Fi的示例。
现在,你已经使用命令行成功地连接到 Wi-Fi 网络了。这两个示例涵盖了用 `nmcli` 和 `wpa_supplicant` 连接无线网络的常见场景,让你能够不依赖图形化界面来完成这个任务。在日常使用中熟练掌握这些基本操作能增强你对 Linux 系统的理解,帮助你更有效地处理各种问题。
273 12
|
5月前
|
安全 Ubuntu Linux
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
163 0
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
|
12月前
|
存储 安全 Unix
网络文件系统 (NFS)
【10月更文挑战第12天】
446 5
|
12月前
|
存储 缓存 安全
网络文件系统 (NFS)
【10月更文挑战第11天】
376 1
|
7月前
|
Ubuntu Linux
Linux系统管理:服务器时间与网络时间同步技巧。
以上就是在Linux服务器上设置时间同步的方式。然而,要正确运用这些知识,需要理解其背后的工作原理:服务器根据网络中的其他机器的时间进行校对,逐步地精确自己的系统时间,就像一只犹豫不决的啮齿动物,通过观察其他啮齿动物的行为,逐渐确定自己的行为逻辑,既简单,又有趣。最后希望这个过程既能给你带来乐趣,也能提高你作为系统管理员的专业素养。
1111 20
|
7月前
|
JSON 运维 Ubuntu
Linux下如何使用Curl进行网络请求
希望这篇文章能帮助您在Linux下更好地使用Curl进行网络请求。如有疑问,请随时提问!
364 10
|
9月前
|
Linux 网络性能优化 网络安全
Linux(openwrt)下iptables+tc工具实现网络流量限速控制(QoS)
通过以上步骤,您可以在Linux(OpenWrt)系统中使用iptables和tc工具实现网络流量限速控制(QoS)。这种方法灵活且功能强大,可以帮助管理员有效管理网络带宽,确保关键业务的网络性能。希望本文能够为您提供有价值的参考。
1318 28
|
9月前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
352 11