NFS安装配置

安装NFS

yum install -y nfs-utils

NFS配置文件路径

/etc/exports

配置文件格式

共享目录        可访问的主机网段(参数1,参数2....) 

常用参数

rw             读写

ro              只读

wo             只写

sync          同步写入

async        异步写入

#下面三个是一组,如果all_squash单独使用,那所有客户端都会变成匿名用户

all_squash        无视客户端通过什么权限的用户登录,统一修改为anonuid指定的用户写入

anonuid             指定服务端操作数据的uid

anongid             指定服务端操作数据的gid

写入配置

目标文件夹/data

[root@Centos-S-10 /]# ls /data
10.file  1.file  2.file  3.file  4.file  5.file  6.file  7.file  8.file  9.file

配置1

[root@Centos-S-10 /]# cat /etc/exports
/data 192.168.31.0/24(rw,sync)

启动服务

#启动
[root@Centos-S-10 /]# systemctl start nfs-server.service

#查看
[root@Centos-S-10 /]# systemctl status nfs-server.service
● nfs-server.service - NFS server and services
     Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; preset: disabled)
     Active: active (exited) since Thu 2025-03-20 02:45:00 CST; 6s ago
 Invocation: 16a0902f94fd4fc8b07686dcb0c8a44a
       Docs: man:rpc.nfsd(8)
             man:exportfs(8)
    Process: 10832 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
    Process: 10833 ExecStart=/bin/sh -c /usr/sbin/nfsdctl autostart || /usr/sbin/rpc.nfsd (code=exited, status=0/SUCCESS)
    Process: 10861 ExecStart=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
   Main PID: 10861 (code=exited, status=0/SUCCESS)
   Mem peak: 2M
        CPU: 31ms

3月 20 02:45:00 Centos-S-10 systemd[1]: Starting nfs-server.service - NFS server and services...
3月 20 02:45:00 Centos-S-10 systemd[1]: Finished nfs-server.service - NFS server and services.

检查挂载

[root@Centos-S-10 /]# showmount -e 192.168.31.70
Export list for 192.168.31.70:
/data 192.168.31.0/24

 客户端挂载

安装与服务端一样,但不用配置

挂载

(base) [root@Anaconda NFS]# mount -t nfs 192.168.31.70:/data /NFS/data/
(base) [root@Anaconda NFS]# ls data/
10.file  1.file  2.file  3.file  4.file  5.file  6.file  7.file  8.file  9.file

失败的测试

(base) [root@Anaconda data]# cat 1.file
I am server

(base) [root@Anaconda data]# echo "I am client" >> 1.file
-bash: 1.file: Permission denied

测试失败,可读不可写,原因其实就是服务器上1.file的写入权限是归root的,解决方法就是创建一个用户来指定权限

#第一步,创建用户和用户组
[root@Centos-S-10 data]# groupadd -g 1101 nfsdog
[root@Centos-S-10 data]# useradd -u 1101 -g nfsdog -M -s /sbin/nologin dog

#第二步,修改目录权限
[root@Centos-S-10 /]# chown -R dog:nfsdog /data

#第三步,修改nfs配置文件
[root@Centos-S-10 /]# cat /etc/exports
/data 192.168.31.0/24(rw,sync,all_squash,anonuid=1101,anongid=1101)

#第四步,检查配置文件
[root@Centos-S-10 /]# showmount -e
Export list for Centos-S-10:
/data 192.168.31.0/24

这里有个要注意的地方,就是客户端那里,要创建一个相同的uid、gid用户,用来占用掉这个uid和gid,防止客户端访问的时候出现误会,我这里没有创建,可以看到下图的权限那里显示的就是uid和gid,而不是用户名和组名

第二个要注意的,就是如果指定的访问的不是网段,而是单一一个IP那就不要带上掩码或者写  x.x.x.x/32

测试

(base) [root@Anaconda data]# cat 1.file
I am server
(base) [root@Anaconda data]# ll
total 4
-rw-r--r--. 1 1101 1101  0 Mar 20 02:42 10.file
-rw-r--r--. 1 1101 1101 12 Mar 20 03:46 1.file
-rw-r--r--. 1 1101 1101  0 Mar 20 02:42 2.file
-rw-r--r--. 1 1101 1101  0 Mar 20 02:42 3.file
-rw-r--r--. 1 1101 1101  0 Mar 20 02:42 4.file
-rw-r--r--. 1 1101 1101  0 Mar 20 02:42 5.file
-rw-r--r--. 1 1101 1101  0 Mar 20 02:42 6.file
-rw-r--r--. 1 1101 1101  0 Mar 20 02:42 7.file
-rw-r--r--. 1 1101 1101  0 Mar 20 02:42 8.file
-rw-r--r--. 1 1101 1101  0 Mar 20 02:42 9.file
(base) [root@Anaconda data]# echo "I am client" >> 1.file
(base) [root@Anaconda data]# cat 1.file
I am server
I am client

测试成功,然后开机自动挂载

#开机挂载配置文件
(base) [root@Anaconda data]# cat /etc/fstab | egrep -v "^#|^$"
UUID=6d7e7cad-e4d2-4695-8c9c-1ac8bb9278a2 /                       xfs     defaults        0 0
UUID=ba3494a3-5402-4d23-959a-b480c2cc3ff1 /boot                   ext4    defaults        1 2
UUID=f49777f6-d306-4771-b7a5-48cd54cb3e40 swap                    swap    defaults        0 0
192.168.31.70:/data                       /NFS/data               nfs     defaults        0 0

#刷新mount配置,立即生效
(base) [root@Anaconda data]# mount -a

故障

  • 当NFS服务端崩掉的时候,客户端只要切设和服务器上挂载目录有关的所有操作都会卡住,开关机都会。只能通过umount -f 强制卸载掉那个目录,或者等服务端修好。

  • 需要对NFS服务的依赖包也开放防火墙端口

lsyncd(rsync+inotify)NFS的同步与备份

环境

主NFS:192.168.31.70

备NFS:192.168.31.71

主目录:/data

备目录:/NFS/data

rsync

安装
(base) [root@Anaconda data]# yum install -y rsync
(base) [root@Anaconda NFS]# yum install -y rsync-daemon.noarch

命令

rsync        [选项]        源文件        目标IP:目标路径                传输(push)

rsync        [选项]        目标IP:目标路径        源文件                拉取(pull)

参数

-a        保留所有属性,这个参数是好几个参数的集合,加上就行

-v        显示传输详情

-z        压缩传输

--delete        删除目标文件夹下与源目录不同的内容,完全同步两个文件夹

增量传输
# PC1:192.168.31.70
[root@Centos-S-10 data]# rsync -avz /data/* 192.168.31.71:/NFS/data
root@192.168.31.71's password:
sending incremental file list
./
1.file
10.file
2.file
3.file
4.file
5.file
6.file
7.file
8.file
9.file

sent 632 bytes  received 209 bytes  240.29 bytes/sec
total size is 24  speedup is 0.03


# PC2:192.168.31.71
(base) [root@Anaconda data]# ls
10.file  1.file  2.file  3.file  4.file  5.file  6.file  7.file  8.file  9.file
服务模式
  • 服务模式分为客户端和服务端
  • 需要备份的设备是客户端,备份服务器是服务端
  • 配置文件路径/etc/rsyncd.conf
服务端

传输使用的用户,我使用的就是NFS那个账号dog,我在两端都创建了相同uid和gid的账号。

第一步,修改配置文件

我里面标注的注释的项,都记得要根据情况改,没注释的,照抄就行

注意:这个配置文件千万别写错,一个字也别写错,记得去掉我的注释

# 配置文件路径
[root@Centos-S-10 data]# rpm -qc rsync
/etc/rsyncd.conf

# 配置文件
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = dog    #操作文件的用户
gid = nfsdog    #操作文件的用户组
use chroot = no
fake super = yes
port = 873    #端口号
max connections = 200
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_user    #rsync的虚拟账号
secrets file = /etc/rsync.passwd    #rsync虚拟账号的密码
log file = /var/log/rsyncd.log
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area

# 模块,这只是用来定义需要的变量,里面的路径可以根据实际情况更爱
[data]    
path = /NFS/data

[backup]
path = /tmp/backup

第二步,创建用户,以及调整目录权限

# 创建用户和组
groupadd -g 1101 nfsdog
useradd -u 1101 -g 1101 -M -s /sbin/nologin dog

# 调整权限
(base) [root@Anaconda tmp]# chown -R dog:nfsdog /NFS/data/ /tmp/backup/

第三步,创建密码文件并修改权限

格式:账号:密码

[root@Anaconda NFS]# echo "rsync_user:123" > /etc/rsync.passwd
(base) [root@Anaconda NFS]# chmod 600 /etc/rsync.passwd
(base) [root@Anaconda NFS]# ll /etc/rsync.passwd
-rw-------. 1 root root 15 Mar 23 07:20 /etc/rsync.passwd

第四部,启动服务

(base) [root@Anaconda NFS]# systemctl start rsyncd.service
(base) [root@Anaconda NFS]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
   Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2025-03-23 07:29:25 CST; 2s ago
 Main PID: 8154 (rsync)
    Tasks: 1 (limit: 49645)
   Memory: 280.0K
   CGroup: /system.slice/rsyncd.service
           └─8154 /usr/bin/rsync --daemon --no-detach

Mar 23 07:29:25 Anaconda systemd[1]: Started fast remote file copy program daemon.
Mar 23 07:29:25 Anaconda rsyncd[8154]: params.c:Parameter() - Ignoring badly formed line in config file: ignore errors
Mar 23 07:29:25 Anaconda rsyncd[8154]: Unknown Parameter encountered: "auth user"
Mar 23 07:29:25 Anaconda rsyncd[8154]: IGNORING unknown parameter "auth user"
Mar 23 07:29:25 Anaconda rsyncd[8154]: rsyncd version 3.1.3 starting, listening on port 873

这里面最最关键的就两个,路径权限,一定要注意!!!!

客户端 -----NFS本体

第一种传输方式

rsync        [选项]        源文件        rsync账户@目标IP::目标路径                传输(push)

[root@Centos-S-10 data]# rsync -avz /data/ rsync_user@192.168.31.71::data
Password:
sending incremental file list

sent 243 bytes  received 20 bytes  105.20 bytes/sec
total size is 24  speedup is 0.09
[root@Centos-S-10 data]# rsync -avz /data/ rsync_user@192.168.31.71::data
Password:
sending incremental file list

sent 243 bytes  received 20 bytes  58.44 bytes/sec
total size is 24  speedup is 0.09

这里有几个我遇到的问题,遇到的就先都关了

  • 防火墙,iptable或者firewalld
  • selinux安全补丁:setenforce 0或者/etc/sysconfig/selinux 修改 SELINUX=disabled来永久禁用
  • 注意密码文件的权限,给多了他就弹不出输入密码的提示了
客户端免密

第一种:环境变量

[root@Centos-S-10 data]# echo "export RSYNC_PASSWORD=123" >> /etc/profile
[root@Centos-S-10 data]# source /etc/profile

第二种:密码文件并添加--password-file参数

[root@Centos-S-10 data]# echo 123 >> /etc/rsync_client.passwd
[root@Centos-S-10 data]# chmod 600 /etc/rsync_client.passwd
[root@Centos-S-10 data]# ll /etc/rsync_client.passwd

[root@Centos-S-10 data]# rsync -avz --password-file=/etc/rsync_client.passwd /data/ rsync_user@192.168.31.71::data

这都是在客户端的操作!!!

第二种方法,文件密码的权限别弄错了,不能添加额外的权限

inotify 实时同步

安装

# 这个工具需要epel源

yum install inotify-tools

使用

 因为后面会用lsyncd去结合,所以这个我直接给命令

(base) [root@Anaconda inotify-tools]# inotifywait -mrq --timefmt '%d-%m-%y %H:%M' --format '%T %w%f' -e delete,create /NFS/data &

#我这里创建了个文件,就会有提示
(base) [root@Anaconda inotify-tools]# touch 222.txt
23-03-25 09:59 /NFS/data/inotify-tools/222.txt

这里面有很多参数可以选择,我这里在放一个脚本,这个脚本是用来配合rsync的,如果文件发生变化,自动同步,删除没写,看着玩。

#!/bin/bash

inotifywait -mrq --format '%w%f' -e create,modify /data | while read line
do
    rsync -avz --password-file=/etc/rsync_client.passwd $line rsync_user@192.168.31.71::data
done

lsyncd (前两个的集合)

安装

yum install -y lsyncd

 修改配置文件 /etc/lsyncd.conf

settings {
        logfile         = "/var/log/lsyncd/lsyncd.log",
        statusFile      = "/var/log/lsyncd/lsyncd.status",
        inotifyMode     = "CloseWrite",
        maxProcesses    = 8,
}

sync{
        default.rsync,
        source="/data",
        target="rsync_user@192.168.31.71::data",
        delete=true,
        exclude={"*.*"},
        delay=1,
        rsync={
                binary="/usr/bin/rsync",
                archive=true,
                compress=true,
                verbose=true,
                password_file="/etc/rsync_client.passwd",
                _extra={"--bwlimit=200"},
        }
}

连的内容按需修改,这是运行在NFS服务器上的,配合备份服务器上的rsync服务端

启动

systemctl start lsyncd.service

然后配合NFS就可以了,非常简单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值