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 ,如需转载请自行联系原作者


相关文章
|
1天前
|
人工智能 Linux
Linux查找大文件的方法
Linux查找大文件的方法
|
3天前
|
网络协议 Linux Shell
【linux网络(一)】初识网络, 理解四层网络模型
【linux网络(一)】初识网络, 理解四层网络模型
|
3天前
|
安全 Ubuntu Linux
Linux 网络操作命令Telnet
Linux 网络操作命令Telnet
16 0
Linux 网络操作命令Telnet
|
4天前
|
固态存储 Ubuntu Linux
Linux(29) 多线程快速解压缩|删除|监视大型文件
Linux(29) 多线程快速解压缩|删除|监视大型文件
11 1
|
4天前
|
Ubuntu Linux 数据安全/隐私保护
Linux(24) 如何在Ubuntu中操作rootfs.img文件
Linux(24) 如何在Ubuntu中操作rootfs.img文件
9 0
|
4天前
|
Ubuntu Linux
Linux(22) Linux设置网络优先级顺序
Linux(22) Linux设置网络优先级顺序
6 0
|
4天前
|
Ubuntu 网络协议 Linux
Linux(20) Ubuntu 20.04 网络接口自动切换路由配置
Linux(20) Ubuntu 20.04 网络接口自动切换路由配置
28 0
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
3月前
|
存储 监控 网络协议
【Linux】文件服务NFS(Network File System)
【Linux】文件服务NFS(Network File System)
35 0
|
3月前
|
存储 Linux 网络安全
Linux系统安装NFS服务器
NFS是一种网络文件系统,英文全称Network File System,通过NFS可以让不同的主机系统之间共享文件或目录。通过NFS,用户可以直接在本地NFS客户端读写NFS服务端上的文件,是非常好的共享存储工具。本篇文章将介绍如何在CentOS7上安装NFS服务器,包括服务端和客户端安装两部分。
74 0