> 文档中心 > Linux 基础入门 10

Linux 基础入门 10


视频讲解在微信公众号 问渠清源 回复关键词 视频 即可观看

一、NTP 时钟同步协议

网络时间协议 NTP(Network Time Protocol)是 TCP/IP 协议族里面的一个应用层协议。 NTP 用于在一系列分布式时间服务器与客户端之间同步时钟。NTP 的实现基于 IP 和 UDP。 NTP 报文通过 UDP 传输,端口号是 123 。

1.1 NTP 主要应用场合

网络管理:对从不同路由器采集来的日志信息、调试信息进行分析时,需要以时 间作为参照依据。

计费系统:要求所有设备的时钟保持一致。

多个系统协同处理同一个复杂事件:为保证正确的执行顺序,多个系统必须参考 同一时钟。

备份服务器和客户机之间进行增量备份:要求备份服务器和所有客户机之间的时 钟同步。

用户登录时间:某些应用程序需要知道用户登录系统的时间以及文件修改的时间。

1.2 基本原理

1.3 系统时钟同步流程

(1)RouterA 发送一个 NTP 报文给 RouterB,该报文中带有它离开 RouterA 时的时间戳 10:00:00 a.m.(T1)。

(2)此 NTP 报文到达 RouterB 时,RouterB 加上到达时间戳 11:00:01a.m.(T2)

(3)此 NTP 报文离开 RouterB 时,RouterB 再加上离开时间戳 11:00:02 a.m.(T3)

(4)RouterA 接收到该响应报文时,加上新的时间戳 10:00:03 a.m.(T4)

至此,RouterA 获得了足够信息来计算以下两个重要参数:

  • NTP 报文来回一个周期的时延:Delay= ( T4 - T1 ) - ( T3 – T2 )

  • RouterA 相对 RouterB 的时间差:Offset= ( ( T2- T1 ) + ( T3 – T4 ) ) / 2

(5)RouterA 根据计算得到 Delay 为 2 秒,Offset 为 1 小时。RouterA 根据这些信息来设定自己的时钟,实现与 RouterB 的时钟同步。

二、RHEL8 服务器配置时钟同步

配置文件 /etc/chrony.confsystemctl restart chronydsystemctl status chronydchronyc  sources注意:*就是我们目前的同步源修改配置文件 vim /etc/chrony.conf删除上面的pool开头添加为 server   ntp.aliyun.com  iburstsystemctl restart chronydchronyc sourcesdate 查看目前时间是否修改成功

实例

[root@wentan ~]# systemctl start chronyd.service[root@wentan ~]# chronyc sources210 Number of sources = 4MS Name/IP address  Stratum Poll Reach LastRx Last sample ===============================================================================^* 119.28.183.184  2   6   377     0  -4486us[  +20ms] +/-   48ms^- ntp6.flashdance.cx     2   6   317    58    -24ms[-6875ns] +/-  204ms^+ 139.199.214.202 2   6   373     1    -50us[  -50us] +/-   36ms^? time.cloudflare.com    0   6     0     -     +0ns[   +0ns] +/-    0ns[root@wentan ~]# vim /etc/chrony.conf#删除第一行port 并修改为server ntp.aliyun.com iburst #阿里云[root@wentan ~]# systemctl restart chronyd.service [root@wentan ~]# chronyc sources210 Number of sources = 1MS Name/IP address  Stratum Poll Reach LastRx Last sample ===============================================================================^* 203.107.6.88    2   6    17    10    -23ms[  -24ms] +/-   48ms

三、调整系统性能

3.1 系统调优

tuned 守护进程来进行系统调优
系统调优:系统管理员可以基于多种用例工作负载来调整各种设备配置,以此来优化系统性能。目前有两种系统调优的模式:

  • 静态调优:
    tuned守护进程会在服务启动时或者选择新的调优配置文件时来对系统进行设置。静态调优会对配置文件中的tuned在运行时预定义kernel参数进程配置。对于静态调优而言,内核参数是对整体性能设置的,不会随着活跃度的变化而调整。

    #安装并启用tuned守护进程yum install -y tunedsystemctl enable --now tunedsystemctl status tuned

    选择调优配置文件:

    tuned应用提供配置文件分别为以下几个类别:

    • 节能型配置文件

    • 性能提升型配置文件

    性能提升型配置文件中包括了以下几个方面的配置文件

    • 存储和网络低延时

    • 存储和网络高吞吐

    • 虚拟机性能

    • 虚拟化主机性能

3.2 调优配置文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OH4kkLnX-1642750070567)(C:\Users\tan-1210\AppData\Roaming\Typora\typora-user-images\image-20220121145050708.png)]

实例1

#安装并启用tuned守护进程[root@wentan ~]# yum install -y tuned[root@wentan ~]# systemctl enable tuned[root@wentan ~]# systemctl start tuned[root@wentan ~]# systemctl status tuned#查看所有系统调优配置文件[root@wentan ~]# tuned-adm list Available profiles:- accelerator-performance     - Throughput performance based tuning with disabled higher latency STOP states- balanced      - General non-specialized tuned profile- desktop- Optimize for the desktop use-case- hpc-compute   - Optimize for HPC compute workloads- intel-sst     - Configure for Intel Speed Select Base Frequency- latency-performance  - Optimize for deterministic performance at the cost of increased power consumption- network-latency      - Optimize for deterministic performance at the cost of increased power consumption, focused on low latency network performance- network-throughput   - Optimize for streaming network throughput, generally only necessary on older CPUs or 40G+ networks- optimize-serial-console     - Optimize for serial console use.- powersave     - Optimize for low power consumption- throughput-performance      - Broadly applicable tuning that provides excellent performance across a variety of common server workloads- virtual-guest - Optimize for running inside a virtual guest- virtual-host  - Optimize for running KVM guestsCurrent active profile: virtual-guest[root@wentan ~]# tuned-adm profile accelerator-performance  latency-performance      recommend.dbalanced   network-latency   throughput-performancedesktop    network-throughputvirtual-guesthpc-computeoptimize-serial-console  virtual-hostintel-sst  powersave #调整当前系统调优配置文件为高吞吐[root@wentan ~]# tuned-adm profile throughput-performance#查看当前系统推荐得系统调优配置文件[root@wentan ~]# tuned-adm recommend virtual-guest[root@wentan ~]# tuned-adm profile virtual-guest [root@wentan ~]# tuned-adm off#关闭系统调优[root@wentan ~]# tuned-adm off[root@wentan ~]# tuned-adm profile virtual-guest #打开
  • 动态调优

    tuned守护进程会监视系统活动,根据系统事实行为的变化来调整设置,从所选的配置文件中,声明初始位置开始,动态调优会不断进行调整,以适应当前的工作负载。

    例如: 存储设备在启动和登录期间,使用率会较高。但是当用户工作负载是用web浏览器,或者使用电子邮件客户端期间,活动量会减少。

    cpu和网络在整个工作日高峰期会出现大量活动增加。tuned守护进程会监视这些组件的活动并且调整参数设置,以最大的限度来提高活动期间的性能,并降低低活动的设置值。

    默认情况下,动态调整是禁用的。如果需要启动动态调优,要编辑配置文件

    配置文件位置: /etc/tuned/tuned-main.conf

    [root@wentan ~]# vim /etc/tuned/tuned-main.conf 
    dynamic_tuning = 1 #开启动态调优   然后定期分析熊统计信息并使用他们来更新系统调整设置# Update interval for dynamic tunings (in seconds).# It must be multiply of the sleep_interval.update_interval = 10  #配置的是两次更新之间的时间间隔
3.3 动态调优如何工作

办公室的workstation上,以太网负载很低,只有电子邮件或者加载网页才会用到以太网,对于这种情况,网络接口不必要始终保持全速运行。tuned具有用于网络设备监视和调整硬件功耗的功能,可以检测这种低活动性,然后自动降低该接口的速度,从而降低功耗。

3.4 安装并使用一个profile
#从yum源安装一个新的profile配置[root@wentan ~]# yum install -y tuned-profiles-cpu-partitioning#安装的配置默认保存在了/usr/lib/tuned[root@wentan ~]# cd /lib/tuned/#找到这个新下载的配置文件,打开并查看[root@wentan tuned]# cd cpu-partitioning/[root@wentan cpu-partitioning]# lltotal 12-rwxr-xr-x. 1 root root  478 Jul 22 02:41 00-tuned-pre-udev.sh-rwxr-xr-x. 1 root root 1313 Jul 22 02:41 script.sh-rw-r--r--. 1 root root 2652 Jul 22 02:41 tuned.conf[root@wentan cpu-partitioning]# vim tuned.conf[root@wentan cpu-partitioning]# tuned-adm profile cpu-partitioning #如果系统支持该配置文件,可以启用该文件。目前本系统不支持需要修改配置文件

四、计划任务 crontab

  1. 系统内置的服务,无需安装,服务进程文件 /usr/sbin/crond 服务默认是启动的

  2. crontab 的默认运行环境变量记录在 /etc/crontab

  3. 可以设置拒绝用户使用crontab 配置文件 /etc/cron.deny

  4. crontab的日志记录在 /var/log/cron

  5. 编辑完crontab 不需要重启 直接生效

4.1 crontab 常用参数
-u #指定运行用户-l #列出自动任务-r #删除全部自动任务-e #编辑自动任务
4.2 crontab 编辑格式

* * * * * command

第一个 * 代表分钟,取值0-59

第二个 * 代表小时,取值0-23

第三个 * 代表几号,取值1-31

第四个 * 代表月份,取值1-12

第五个 * 代表星期,取值1-7

4.3 特殊符号

默认 * 代表任何时刻。

逗号 代表分隔时间。* 3,6 * * * 代表3点和6点执行一次

横岗 - 代表时间段。* 3-6 * * * 代表3到6点每小时执行一次

/n 代表每n单位时间间隔。* */1 * * * 代表每1个小时执行一次

管道与重定向

注意:只有在 分钟 小时 日期 月份 星期

都满足且匹配的条件下 才会执行command命令

计划任务当中的脚本建议一定要写绝对路径

分钟建议不要给*,这样每分钟都会执行一次。

分 时 日 月 星期 命令

# * 表示任何数字都符合0 2 * * * /run.sh# 每天的2点0 2 14 * * /run.sh# 每月14号2点0 2 14 2 * /run.sh# 每年2月14号2点0 2 * * 5 /run.sh# 每个星期5的2点0 2 * 6 5 /run.sh# 每年6月份的星期5的2点0 2 2 * 5 /run.sh# 每月2号或者星期5的2点   星期和日同时存在,那么就是或的关系0 2 2 6 5 /run.sh# 每年6月2号或者星期5的2点* /5 * * * * /run.sh# 每隔5分钟执行一次0 2 1,4,6 * * /run.sh# 每月1号,4号,6号的2点0 2 5-9 * * /run.sh# 每月5-9号的2点* * * * * /run.sh# 每分钟0 * * * * /run.sh# 每整点* * 2 * * /run.sh# 每月2号的每分钟

五、管道与重定向

5.1 标准输入,标准输出,标准错误

在这里插入图片描述

File Descriptors(FD,文件描述符 或 Process I/O channels)

进程使用文件描述符来管理打开的文件

[root@wentan ~]#  ls -l  /proc/8427/fdtotal 0lrwx------. 1 root root 64 Jan 20 13:45 0 -> /dev/pts/1lrwx------. 1 root root 64 Jan 20 13:46 1 -> /dev/pts/1lrwx------. 1 root root 64 Jan 20 13:46 2 -> /dev/pts/1lrwx------. 1 root root 64 Jan 20 13:46 3 -> 'socket:[74832]'lr-x------. 1 root root 64 Jan 20 13:46 4 -> /var/lib/sss/mc/passwdlrwx------. 1 root root 64 Jan 20 13:46 5 -> 'socket:[74835]'lrwx------. 1 root root 64 Jan 20 13:46 6 -> 'socket:[74003]'
Number Channel name Description Default connection
0 stdin 标准输入 键盘
1 stdout 标准输出 命令行
2 stderr 标准错误 命令行
3+ filename 其他文件 none
5.2 输出重定向

> 覆盖

>> 追加

  • 正确输出 1> 1>> 等同于 > >>

  • 错误输出 2> 2>>

  • 正确输出 1> 1>>

5.2.1 案例1 输出重定向覆盖

Linux 基础入门 10

[root@wentan ~]# cd /dwt/[root@wentan dwt]# date 1> /dwt/date.txt[root@wentan dwt]# cat date.txt Thu 20 Jan 13:55:26 CST 2022
5.2.2 案例2 输出重定向追加

Linux 基础入门 10

[root@wentan dwt]# cal >> /dwt/date.txt[root@wentan dwt]# cat date.txt Thu 20 Jan 13:55:26 CST 2022    January 2022    Mo Tu We Th Fr Sa Su  1  2 3  4  5  6  7  8  910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 3031    
5.2.3 案例3 输出错误重定向

Linux 基础入门 10

[root@wentan dwt]# ls /home/admin  test  wentan[root@wentan dwt]# ls /home/ /asojknls: cannot access '/asojkn': No such file or directory/home/:admin  test  wentan[root@wentan dwt]# ls /home/ /asojkn > /dwt/list.txtls: cannot access '/asojkn': No such file or directory[root@wentan dwt]# ls /home/ /asojkn 2> /dwt/error.txt/home/:admin  test  wentan
5.2.4 案例4 正确和错误都输入到相同位置

Linux 基础入门 10

[root@wentan dwt]# ls /home/ /sgdvkjs &> /dwt/test.txt[root@wentan dwt]# cat test.txt ls: cannot access '/sgdvkjs': No such file or directory/home/:admintestwentan
5.2.5 案例5 正确和错误都输入到相同位置

Linux 基础入门 10

[root@wentan dwt]# ls /home/ /sgdvkjs > /dwt/test1.txt 2>&1[root@wentan dwt]# cat test1.txt ls: cannot access '/sgdvkjs': No such file or directory/home/:admintestwentan
5.2.6 案例6 重定向到空设备 /dev/null
[root@wentan dwt]# ls /home/ /sgdvkjs > /dwt/test2.txt 2>/dev/null [root@wentan dwt]# cat test2.txt /home/:admintestwentan[root@wentan dwt]# ls /home/ /sgdvkjs &>/dev/null 
5.3 输入重定向

标准输入 < 等价 0 <

5.3.1 案例1 通过重定向方式发送文本内容作为邮件

实验开始前得有mail环境那么我们先来部署

yum install -y postfix   systemctl start postfixyum install -y mailx
[root@wentan ~]# mail  -s  "hello"  wentanhello  helloi meet  youEOT[root@wentan ~]# su - wentan[redhat@wentan ~]$ mailHeirloom Mail version 12.5 7/5/10.  Type ? for help."/var/spool/mail/redhat": 6 messages 1 new 6 unread U  1 root    Thu Jan 20 14:51  20/779   "test01" U  2 root    Thu Jan 20 14:54  22/636   "hello" U  3 root    Thu Jan 20 14:54  20/640   "hello" U  4 root    Thu Jan 20 15:02  20/708   "hello" U  5 root    Thu Jan 20 15:06  19/633   "luxingjun">N  6 root    Thu Jan 20 15:11  19/635   "hello"[root@wentan ~]# vim /dwt/test.txt[root@wentan ~]# mail  -s   "new"   wentan    <  /dwt/test.txt 
5.3.2 案例2
[root@wentan ~]# grep  'root'qweqweqweqweasdasdasdqweqwerootrootrootroot[root@wentanr ~]# grep 'root'   <  /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin
5.4 进程管道

Linux 基础入门 10

5.4.1 案例1 将/etc/passwd中的用户按照UID由大到小排序
[root@wentanr ~]# sort -t ":"   -k  3    -n    /etc/passwd[root@wentanr ~]# sort -t ":"   -k  3    -n    /etc/passwd    -r[root@wentanr ~]# sort -t ":"   -k  3    -n    /etc/passwd    -r  |head  -n  5

参数

-t  #指定字段分隔符-k  #指定列-n  #按数值排-r  #倒序
5.4.2 案例2 统计当前 /etc/passwd中用户使用的shell类型个数
[root@wentanr ~]# awk -F:   '{print $7}'   /etc/passwd |sort  |uniq  -c  uniq#去重
5.4.3 案例3 打印当前所有的ip
[root@wentanr ~]# ip addr |grep 'inet' | awk '{print $2}' |awk  -F '/'  '{print $1}' 
5.4.4 打印根分区已用空间的百分比 (仅打印数字)
[root@wentanr ~]# df  |grep '/$' |awk '{print $5}' | awk -F "%" '{print $1}' /etc/passwd    -r  |head  -n  5

参数

-t  #指定字段分隔符-k  #指定列-n  #按数值排-r  #倒序
5.4.2 案例2 统计当前 /etc/passwd中用户使用的shell类型个数
[root@wentanr ~]# awk -F:   '{print $7}'   /etc/passwd |sort  |uniq  -c  uniq#去重
5.4.3 案例3 打印当前所有的ip
[root@wentanr ~]# ip addr |grep 'inet' | awk '{print $2}' |awk  -F '/'  '{print $1}' 
5.4.4 打印根分区已用空间的百分比 (仅打印数字)
[root@wentanr ~]# df  |grep '/$' |awk '{print $5}' | awk -F "%" '{print $1}'

视频讲解在微信公众号 问渠清源 回复关键词 视频 即可观看
Linux 基础入门 10