NFS日志管理和文件共享以及永久挂载文件
  3fZ8Ei5tA2Xa 2023年11月02日 18 0

今天分享的是NFS的网络共享,其中包括主机之间的文件公布共享和挂载其相关配置文件

网络文件实时同步和共享

实战案例:将所有服务器里的存储内容传到共享服务器里
1.硬盘插入首先要分区  2.创建文件系统 3.挂载
NFS软件介绍

软件包:

  • 红帽系统: nfs-utils: 包括服务器和客户端相关工具,CentOS8 最小化安装时默认没有安装)
  • Ubuntu: nfs-kernel-server 服各器包名,nfs-common 客户端包名

相关软件包:rpcbind(必须),tcp_wrappers

端口:2049(nfsd), 其它端口由portmap(111)分配

NFS服务主要进程:

  • rpc.nfsd 最主要的NFS进程,管理客户端是否可登录
  • rpc.mountd 挂载和卸载NFS文件系统,包括权限管理
  • rpc.lockd 非必要,管理文件锁,避免同时写出错
  • rpc.statd 非必要,检查文件一致性,可修复文件

说明:CentOS 6 开始portmap进程由rpcbind代替

日志:/var/lib/nfs/

NFS配置文件:

/etc/exports
/etc/exports.d/*.exports

范例:红帽系统安装NFS软件包

[root@centos8 ~]#yum -y install nfs-utils
[root@centos8 ~]#systemctl enable --now nfs-server.service

范例: Ubuntu 安装NFS软件包

#服务器
[root@ubuntu2004 ~]#apt install nfs-kernel-server
[root@ubuntu2004 ~]#systemctl status nfs-server

#客户端
[root@ubuntu2004 ~]#apt -y install nfs-common
服务端
1.#NFS服务器,共享/data/nfsdir文件夹
[root@ubuntu2004 ~]#mkdir /data/nfsdir{1,2} -pv
mkdir: 已创建目录 '/data'
mkdir: 已创建目录 '/data/nfsdir1'
mkdir: 已创建目录 '/data/nfsdir2'
[root@ubuntu2004 ~]#tree /data
/data
├── nfsdir1
└── nfsdir2

2directories, 0 files
2.想要共享首先要安装NFS软件
[root@ubuntu2004 ~]#apt update;apt install nfs-kernel或者安装
[root@ubuntu2004 ~]#apt update;apt install nfs-server

[root@ubuntu2004 ~]#systemctl status nfs-server
客户端
[root@ubuntu2204 ~]#apt update;apt install nfs-common -y
[root@rocky8 ~]#yum install nfs-utils -y
#查看端口号
[root@ubuntu2004 ~]#ss -ntl
[root@ubuntu2004 ~]#ss -ntlp #可以查看更详细的
[root@ubuntu2004 ~]#rpcinfo -p 端口nfs适合公司内网使用不适合互联网
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100005 1 udp 47417 mountd
100005 1 tcp 59645 mountd
100005 2 udp 56450 mountd
100005 2 tcp 58285 mountd
100005 3 udp 32818 mountd
100005 3 tcp 55179 mountd
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 3 tcp 2049
100003 3 udp 2049 nfs
100227 3 udp 2049
100021 1 udp 39663 nlockmgr
100021 3 udp 39663 nlockmgr
100021 4 udp 39663 nlockmgr
100021 1 tcp 38387 nlockmgr
100021 3 tcp 38387 nlockmgr
100021 4 tcp 38387 nlockmgr
[root@ubuntu2004 ~]#ll /etc/exports
-rw-r--r-- 1 root root 389 513 2021 /etc/exports
[root@ubuntu2004 ~]#cat /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3: #传统用这个
# /srv/homes 权限hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check) 共享的对象
#
# Example for NFSv4: #这个比较鸡肋,不好
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
3.#参考文件,设置一下
[root@ubuntu2004 ~]#vim /etc/exports
加入共享的文件
/data/nfsdir1 *

#可以查看共享的资源
[root@ubuntu2004 ~]#exportfs -v
#生效不断掉的方法
[root@ubuntu2004 ~]#exportfs -r
exportfs: No options for /data/nfsdir1 *: suggest *(sync) to avoid warning
exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/nfsdir1".
Assuming default behaviour ('no_subtree_check').
NOTE: this default has changed since nfs-utils version 1.0.x
[root@ubuntu2004 ~]#exportfs -v
/data/nfsdir1 <world>(ro,wdelay,root_squash,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)

客户端访问的命令 能看到共享列表则共享成功,没装客户端也不会成功
[root@rocky8 ~]#showmount -e 10.0.0.100
Export list for 10.0.0.100:
/data/nfsdir1 *

因为没有东西,所以拷一个文件进去
[root@ubuntu2004 ~]#cp /etc/os-release /data/nfsdir1/a.txt
想使用需要进行文件挂载到本机 建个网站
[root@rocky8 ~]#yum -y install httpd
把远程共享的文件夹挂载到下面的文件夹,格式就是IP加路径跟上要挂载的文件,/var/www/html/需要提前创建
[root@rocky8 ~]#ls /var/www/html/
[root@rocky8 ~]#mount 10.0.0.100:/data/nfsdir1 /var/www/html/
[root@rocky8 ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 894044 0 894044 0% /dev
tmpfs 914116 0 914116 0% /dev/shm
tmpfs 914116 8908 905208 1% /run
tmpfs 914116 0 914116 0% /sys/fs/cgroup
/dev/mapper/rl-root 73364480 3135108 70229372 5% /
/dev/sda1 1038336 214868 823468 21% /boot
/dev/mapper/rl-home 133071376 960828 132110548 1% /home
tmpfs 182820 0 182820 0% /run/user/0
10.0.0.100:/data/nfsdir1 101848064 6872832 89755648 8% /var/www/html
现在web服务是httpd,现在可以访问页面了
[root@rocky8 ~]#systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@rocky8 ~]#ls /var/www/html/
a.txt
现在可以去网站上查看了
http://10.0.0.161/a.txt

NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
续例:让两个服务共享一个页面
换一台机子继续操作
[root@ubuntu2204 ~]#apt install apache2 -y
然后挂载一下
[root@ubuntu2204 ~]#mount 10.0.0.100:/data/nfsdir1 /var/www/html/
[root@ubuntu2204 ~]#df
文件系统 1K-块 已用 可用 已用% 挂载点
tmpfs 198828 1020 197808 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 101590008 6286172 90097208 7% /
tmpfs 994120 0 994120 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
/dev/sda2 1992552 251960 1619352 14% /boot
tmpfs 198824 0 198824 0% /run/user/0
10.0.0.100:/data/nfsdir1 101848064 6872832 89755648 8% /var/www/html
现在可以去换另一个从网站上查看了
http://10.0.0.200/a.txt

NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

更改源主机数据
[root@ubuntu2004 ~]#vim /data/nfsdir1/a.txt
welcome to nfs server
网站刷新
http://10.0.0.200/a.txt

welcome to nfs server
#该文件内容为只读的 其中root_squash为压榨,剥削,使原本root用户变成普通用户
如果想不被压榨就更改文件
[root@ubuntu2004 ~]#vim /etc/exports
/data/nfsdir1 *(rw,no_root_squash)#不压榨
#生效不断掉的方法
[root@ubuntu2004 ~]#exportfs -r
exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/nfsdir1".
Assuming default behaviour ('no_subtree_check').
NOTE: this default has changed since nfs-utils version 1.0.x
#可以查看共享的资源
[root@ubuntu2004 ~]#exportfs -v
/data/nfsdir1 <world>(rw,wdelay,no_root_squash,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
在另一个文件输入文件可以成功
[root@rocky8 ~]#cd /var/www/html/
[root@rocky8 html]#touch b.txt
[root@rocky8 html]#ll
total 4
-rw-r--r-- 1 root root 22 Nov 20 12:39 a.txt
-rw-r--r-- 1 root root 0 Nov 20 12:57 b.txt
之前挂载都是临时性的

永久挂载配置文件

[root@rocky8 ~]#cd /var/www/html/
[root@rocky8 html]#vim /etc/fstab
10.0.0.100:/data/nfsdir1 /var/www/html nfs _netdev 0 0
第一个0是设备备份周期 第二个0表示检查次序
先取消临时挂载
[root@ubuntu2204 ~]#df
文件系统 1K-块 已用 可用 已用% 挂载点
tmpfs 198828 1020 197808 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 101590008 6286176 90097204 7% /
tmpfs 994120 0 994120 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
/dev/sda2 1992552 251960 1619352 14% /boot
tmpfs 198824 0 198824 0% /run/user/0
10.0.0.100:/data/nfsdir1 101848064 6872832 89755648 8% /var/www/html

#挂载取消不成功是应为占用根目录,直接全部返回源目录就好
[root@ubuntu2204 ~]#umount /var/www/html/

#查看
[root@ubuntu2204 ~]#cat /etc/fstab
/dev/disk/by-uuid/fd29b9ab-4943-44c7-894f-7db7a966f8e9 /boot ext4 defaults 0 1
/swap.img none swap sw 0 0
10.0.0.100:/data/nfsdir1 /var/www/html nfs _netdev 0 0
#使挂载文件生效
[root@ubuntu2204 ~]#mount -a
[root@ubuntu2204 ~]#df
文件系统 1K-块 已用 可用 已用% 挂载点
tmpfs 198828 1020 197808 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 101590008 6286180 90097200 7% /
tmpfs 994120 0 994120 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
/dev/sda2 1992552 251960 1619352 14% /boot
tmpfs 198824 0 198824 0% /run/user/0
10.0.0.100:/data/nfsdir1 101848064 6872832 89755648 8% /var/www/html

今天的分享就到这里,谢谢大家的观看!

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
3fZ8Ei5tA2Xa
最新推荐 更多