RHCE Day2 时间管理服务器 NFS服务器

一、时间服务器

1、简介

重要性:订单,日志,

RTC:硬件时间、系统时间:操作系统时间

设置时间的两个命令 :date timedatectl

2、命令

shell 复制代码
#date 设置时间   用来设置命令的时候 还能影响系统时间 无法修改 硬件时间
  # date -s "%Y-%m-%d %H:%M:%S"
  # date -s "%Y:%m:%d"
  # date -s "%H:%M"
  [root@server ~]# date -s "2011-11-11 11:11:11"
  2011年 11月 11日 星期五 11:11:11 CST
  [root@server ~]# timedatectl
                 Local time: 五 2011-11-11 11:11:18 CST
             Universal time: 五 2011-11-11 03:11:18 UTC
                   RTC time: 六 2025-11-01 02:13:59
                  Time zone: Asia/Shanghai (CST, +0800)
  System clock synchronized: no
                NTP service: active
            RTC in local TZ: no
  
  # timedatectl 设置时间
  # 如果直接set-time 会失败 原因是系统中的时间同步开启中
  [root@server ~]# timedatectl set-time "2022-2-22 2:2:2"
  Failed to set time: Automatic time synchronization is enabled
  [root@server ~]# timedatectl
                 Local time: 五 2011-11-11 11:15:59 CST
             Universal time: 五 2011-11-11 03:15:59 UTC
                   RTC time: 六 2025-11-01 02:18:29
                  Time zone: Asia/Shanghai (CST, +0800)
  System clock synchronized: no
                NTP service: active
            RTC in local TZ: no
  # 所以首先关闭系统同步
  [root@server ~]# timedatectl set-ntp 0/timedatectl set-ntp false/timedatectl set-ntp no
  [root@server ~]# timedatectl
                 Local time: 五 2011-11-11 11:17:03 CST
             Universal time: 五 2011-11-11 03:17:03 UTC
                   RTC time: 六 2025-11-01 02:19:29
                  Time zone: Asia/Shanghai (CST, +0800)
  System clock synchronized: no
                NTP service: inactive #系统同步服务已关闭
            RTC in local TZ: no
  # 关闭后再去设置系统时间,他会修改系统时间及硬件时间
  [root@server ~]# timedatectl set-time "2022-2-22 2:2:2"
  [root@server ~]# timedatectl
                 Local time: 二 2022-02-22 02:02:08 CST
             Universal time: 一 2022-02-21 18:02:08 UTC
                   RTC time: 一 2022-02-21 18:02:08
                  Time zone: Asia/Shanghai (CST, +0800)
                  
  # 时间同步 当系统时间date命令或其他命令修改了,系统认为你要使用自己设定时间
  # 所以他不会再去同步授时中心下发的时间,即使ntp service : active
  # 如果你不想使用自定义时间而是要授时中心下发的时间那么你需要先关闭同步服务在开启同步服务
  [root@server ~]# timedatectl set-ntp 0
  [root@server ~]# timedatectl set-ntp 1

3、 hwclock :可以用系统时间设置硬件时间,用硬件时间设置系统时间

shell 复制代码
  hwclcok -s :使用硬件时间设置系统时间
  hwclock -w :使用系统时间设置硬件时间

4、 NTP及chrony

NTP:首先是一个理论,它是关于时间同步的通信协议。

chrony:就是将该ntp通信协议具体实现出来的一套软件系统。

( 1 )安装

shell 复制代码
[root@server ~]# dnf install chrony -y

( 2 )配置文件分析

shell 复制代码
 [root@server ~]# /etc/chrony.conf
    1 # Use public servers from the pool.ntp.org project.
    2 # Please consider joining the pool (https://www.pool.ntp.org/jo    in.html).
    3 pool 2.centos.pool.ntp.org iburst # 这里就是配置授时服务器的地址 iburst 表示chronyd启动时会立即进行一次同步
    4
    5 # Use NTP servers from DHCP. # 通过dhcp服务获取时间同步服务(前提是支持该功能的dhcp)
    6 sourcedir /run/chrony-dhcp
    7
    8 # Record the rate at which the system clock gains/losses time.
    9 driftfile /var/lib/chrony/drift # 该文件记录系统时钟的误差
   10
   11 # Allow the system clock to be stepped in the first three updat    es
   12 # if its offset is larger than 1 second.
   13 makestep 1.0 3  # 当跟授时服务器的误差超过1秒时,会进行3次快速同步
   14
   15 # Enable kernel synchronization of the real-time clock (RTC).
   16 rtcsync
   17
   18 # Enable hardware timestamping on all interfaces that support i    t.
   19 #hwtimestamp *
   20
   21 # Increase the minimum number of selectable sources required to     adjust
   22 # the system clock.
   23 #minsources 2
   24
   # 当本机作为授时服务器时 以下字段生效
   25 # Allow NTP client access from local network.
   26 allow 0.0.0.0/0
   27
   28 # Serve time even if not synchronized to a time source.
   29 local stratum 10
   30

实验

1、修改时间,换源,同步

shell 复制代码
# 1. 修改时间
[root@server ~]#date -s "2011-11-11 1:1:1"
# 2. 修改配置文件中授时服务器地址
[root@server ~]#vim /etc/chrony.conf
----------------------------------------------------------
3 pool ntp.aliyun.com iburst # 修改授时源
----------------------------------------------------------
# 3. 重启chronyd
[root@server ~]#systemctl restart chronyd.service
# 4.稍等片刻去观察时间是否进行了同步,如果同步成功说明 阿里云的收拾服务器设置成功

2、一台server 一台client 用client去同步错误时间的server

shell 复制代码
#服务端配置
# 1.创建时间服务器修改/etc/chrony.conf的26和29行
[root@server ~]# vim /etc/chrony.conf
----------------------------------------------------------
26 allow xxx.xxx.xxx.0/24 # 允许指定网段中主机访问
29 stratum 10 #表示当前时时钟源的第十层
----------------------------------------------------------
# 2. 重启服务
[root@server ~]#systemctl restart chronyd
# 3.将自己的时间修改为一个错误的时间
[root@server ~]#date -s "xxxx-xx-xx xx:xx:xx"
############################################################################
#客户端
# 1.修改配置文件将授时服务器指定为xxx.xxx.xxx.xxx
[root@clinet ~]#vim /etc/chrony.conf
----------------------------------------------------------
3 pool xxx.xxx.xxx.xxx iburst
----------------------------------------------------------
#2.先关闭系统的 NTP 自动时间同步再开启
[root@clinet ~]#timedatectl set-ntp 0
[root@clinet ~]#timedatectl set-ntp 1
#3.检测系统时间变化
[root@clinet ~]#watch -n 1 timedatectl
# 4.重启服务
[root@clinet ~]#systemctl restart chronyd

3、chronyc sources -v 查看授时服务器的状态

shell 复制代码
  [root@client ~]# chronyc sources -v
  
    .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
   / .- Source state '*' = current best, '+' = combined, '-' = not combined,
  | /             'x' = may be in error, '~' = too variable, '?' = unusable.
  ||                                                 .- xxxx [ yyyy ] +/- zzzz
  ||      Reachability register (octal) -.           |  xxxx = adjusted offset,
  ||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
  ||                                \     |          |  zzzz = estimated error.
  ||                                 |    |           \
  MS Name/IP address         Stratum Poll Reach LastRx Last sample
  ===============================================================================
  ^~ 172.25.254.100                2   6   377     7   -4859s[ -4859s] +/-   35ms
  # ^~ :表示波动大的授时服务器
  # ^* :当前最好的源,主授时源
  # ^= : 同级别的授时源
  # ^+ : 备用授时源
  # ^- : 不适合授时源
  # ^x : 有错误的授时源
  # ^? : 不可用的授时源
  # ^# : 本地授时源
  # poll 6 : 同步周期为2^6=64
  
  # 在2进制转为16进制中 4为二进制对应1为十六进制
  # 在2进制转为8进制中 3为二进制对应1为八进制
  # Reach 377: 377==>011111111 表示最近8次的同步结果如果同步结果为1,如果同步失败结果0
  
  # LastRx :上一次同步实在多少秒前

二、 NFS服务器

1、简介

NFS:network filesystem 网络文件系统,服务端给客户端共享一个硬盘上的目录。

NFS的使用场景:将数据中心的数据共享给其他服务器,其他服务器在使用共享目录时,感受跟本地没有区别

NFS涉及到的服务 :

  • rpcbind: 服务中介 固定端口111。因为nfs-server端口为多个,且不固定,客户端如果想要使用nfs-server,但是没有办法知道nfs-server的具体端口信息, 所以rpcbind就会提供服务将nfs-server具体端口信息给到客户端。

  • nfs-server :真正提供数据资源的服务器

2、nfs的使用

(1)安装

shell 复制代码
# 客户端和服务端都安装 nfs-utils
yum install nfs-utils -y

(2)配置文件

①主配置文件 /etc/exports ,在该配置文件中,

  • 1、写入要共享目录

  • 2、要共享给谁

  • 3、被共享人所拥有权限 : ro,rw

  • 4、账户映射设定

    • root_squash(只将root映射为nobody),
    • no_root_squash(直接将客户端的root在服务器也映射为root),
    • all_squash(所有账户在服务器上都被映射为nobody)(推荐)
    • no_all_squash(除了root其他账户都不映射)
  • 5、同步/异步读写:

    • sync:同步方式,一端写入内容,另一端同步在内存和硬盘中写入同样内容,然后回执。(推荐)
    • async:异步方式,一端写入内容,另一端会将内容暂存在内存中,然后回执,只有当内存中的数据积累到一定量时再写入到硬盘。
  • 6、指定用户的映射身份

②共享控制/usr/sbin/exportfs :常用于刷新共享目录的配置信息

  • 热更新:exportfs - rv(无需重启 nfs-server 服务,即可让新配置生效的操作。)

实验:使用nfs实现一个共享目录

1、服务端建立nfs服务 客户端挂载该服务

shell 复制代码
  #服务端
  #1.安装nfs-utils
  [root@clinet ~]#dnf install nfs-utils -y
  #2.创建要共享的目录并在其中写入文件
  [root@clinet ~]#mkdir /nfs_server
  [root@clinet ~]#echo "hello nfs" > /nfs_server/hello.txt
  #3.在/etc/exports添加要共享的目录 并指定接受共享的主机ip,该主机的权限,该主机映射为nobody,同步方式读写
  [root@clinet ~]#vim /etc/exports
  ----------------------------------------------------------
  /nfs_server xxx.xxx.xxx.xxx(rw,all_squash,sync)
  ----------------------------------------------------------
  # 4.启动服务
  [root@server ~]#systemctl enable --now nfs-server
##############################################################################
  #客户端
  #1.安装nfs-utils
  [root@clinet ~]#dnf install nfs-utils -y
  #2.创建接受共享的目录
  [root@clinet ~]#mkdir /nfs_client
  #3.将服务端的目录挂在到nfs_client下
  [root@clinet ~]#showmount -e xxx.xxx.xxx
  [root@clinet ~]#mount xxx.xxx.xxx:/nfs_server /nfs_client

2、尝试写文件,如果不行为什么

shell 复制代码
  #因为此时客户端的用户身份被映射为了服务器上的nobody,而nobody没有写文件的权限,所以无法创建。
  [root@server nfs_server]# chmod 777 .

3、将客户端的账户映射为redhat

shell 复制代码
  # 以下操作是建立在实验1的基础之上
  #服务器
  # 1.修改/etc/exports文件
  [root@server ~]#vim /etc/exports
 -----------------------------------------------------------------------------
  /nfs_server xxx.xxx.xxx.xxx/xx(rw,all_squash,sync,anonuid=1000,anongid=1000)
 -----------------------------------------------------------------------------
  # 2.使用exportfs命令更新共享目录的设置(热更新)
  [root@server ~]#exportfs -rv
  ############################################################################
  # 客户端
  # 此时在共享目录中创建新的文件 观察该文件属于谁
  [root@client nfs_client]# touch 333
  [root@client nfs_client]# ll
  总用量 8
  -rw-r--r-- 1 redhat redhat  0 10月 28 16:51 333
  -rw-r--r-- 1 root   root   10 10月 28 15:32 hello.txt
  -rw-r--r-- 1 nobody nobody 22 10月 28 15:55 thanks.txt

3、 自动挂载

本质:惰性挂载

( 1 )autofs

默认情况下autofs会将共享目录挂在到net目录下,当你进入到指定ip的目录那一瞬间时autofs会自动将该ip中的共享目录挂载到指定ip的目录下。

❗autofs还支持自动卸载

  • 配置文件/etc/autofs.conf
    • 在这个文件中 我们可以修改自动卸载的时长
  • 配置文件/etc/auto.master
    • 在这个文件中我们可以指定自动挂载的目录

实验

1、实现自动挂载

shell 复制代码
  # 客户端
  # 1.将之前做的实验取消挂载
 [root@clinet ~]# umount /nfs_client
  # 2.安装autofs
  [root@clinet ~]#dnf install autofs -y
  #3.开启
  [root@clinet /]# systemctl start autofs
  # 4. 进入/net/xxx.xxx.xxx.xxx/nfs_server,观察是否报错
  [root@clinet /]# vim /net/192.168.10.100/nfs_server
  # 5. 如果没有报错并且可以查看到 服务端的共享目录中文件 那么说明自动挂载出发成功
  
  # 6.修改自动卸载的时长
  [root@clinet ~]#vim /etc/autofs.conf
----------------------------------------
  timeout = 5
----------------------------------------
  # 7.重启autofs
  [root@clinet /]#systemctl restart autofs
  # 8.进入/net/xxx.xxx.xxx.xxx/nfs_server,再回到根目录
  [root@clinet /]# cd /net/192.168.10.100/nfs_server
  [root@clinet nfs_server]# df
  文件系统                      1K-块    已用     可用 已用% 挂载点
  devtmpfs                       4096       0     4096    0% /dev
  tmpfs                        886588       0   886588    0% /dev/shm
  tmpfs                        354636    5240   349396    2% /run
  /dev/mapper/rhel-root      17141760 1763564 15378196   11% /
  /dev/nvme0n1p2               983040  221248   761792   23% /boot
  /dev/nvme0n1p1               613184    7140   606044    2% /boot/efi
  tmpfs                        177316       0   177316    0% /run/user/0
  192.168.10.100:/nfs_server 17141760 1763328 15378432   11% /net/192.168.10.100/nfs_server
  [root@clinet nfs_server]# cd /
  # 9.第一时间df 查看挂载信息
  [root@client /]# df
  文件系统                      1K-块    已用     可用 已用% 挂载点
  。。。。
  172.25.254.100:/nfs_server 42467328 1988352 40478976    5% /net/172.25.254.100/nfs_server
  # 10.过5秒钟 再使用df命令 发现工项目 已经卸载了
  [root@client /]# df
  文件系统                 1K-块    已用     可用 已用% 挂载点
  devtmpfs                  4096       0     4096    0% /dev
  tmpfs                   872836       0   872836    0% /dev/shm
  tmpfs                   349136    5284   343852    2% /run
  /dev/mapper/rhel-root 42467328 1990920 40476408    5% /
  /dev/sda2               983040  200180   782860   21% /boot
  /dev/mapper/rhel-home 20701184  177448 20523736    1% /home
  /dev/sda1               613160    7140   606020    2% /boot/efi
  tmpfs                   174564       0   174564    0% /run/user/0

2、实现指定目录的自动挂载

shell 复制代码
  # 1.修改/etc/auto.master 新增自动挂载的目录 并指定该目录的子配置文件
  [root@clinet net]# vim /etc/auto.master   在此文件里添加以下内容
---------------------------------------------
  /nfs_auto    /etc/auto.nfs_auto
---------------------------------------------
  
  # 2.实现一下子配置文件/etc/auto.nfs_auto
  [root@clinet net]# vim /etc/auto.nfs_auto
---------------------------------------------
  fromserver 192.168.10.100:/nfs_server
----------------------------------------------
  # 3.重启autofs
  [root@clinet net]# systemctl restart autofs
  
  # 4.重启过会发现 根目录中自动创建了 /nfs_auto目录 我们进入其中的fromserver 那就是服务端共享目录
  [root@client /]# cd /nfs_auto/fromserver
  [root@client fromserver]# ls
  333  444  hello.txt  thanks.txt

3、实现光盘挂载

shell 复制代码
#1、编辑/etc/auto.master,添加光盘挂载规则
  [root@client fromserver]# vim /etc/auto.master    在此文件里添加以下内容
----------------------------------------------
/media       /etc/auto.media
----------------------------------------------
#2、编辑/etc/auto.media,定义光盘挂载规则
  [root@client fromserver]#systemctl restart autofs vim /etc/auto.media
----------------------------------------------
cdrom -fstype=iso9660 :/dev/sr0
----------------------------------------------
#3、重启autofs服务
[root@client fromserver]#systemctl restart autofs
#4、触发自动挂载并验证
[root@client fromserver]#cd /media/cdrom
[root@clinet cdrom]# ls
相关推荐
热爱编程的小白白11 小时前
【Playwright自动化】录制生成脚本
运维·自动化
ROCKY_81711 小时前
计算机网络考试考点——应用层
服务器·网络·计算机网络
e***749511 小时前
Modbus报文详解
服务器·开发语言·php
java_logo11 小时前
MySQL Server Docker 容器化部署指南
linux·运维·数据库·docker·容器
运维管理11 小时前
anolis openeuler 文件复制上用命令-学习篇
服务器
I***t71611 小时前
自己编译RustDesk,并将自建ID服务器和key信息写入客户端
运维·服务器
誰能久伴不乏11 小时前
Linux文件套接字AF_UNIX
linux·服务器·c语言·c++·unix
BJ_Bonree11 小时前
数智先锋 | 核心应用响应时常<1s、多终端崩溃率低至 0.1%!Bonree ONE 赋能蓝月亮应用性能与终端体验双重升级!
运维
张鱼小丸子11 小时前
电脑刷机教程:轻松重装系统指南
运维