欢迎来到我的博客,这里是Linux板块,配合之前RHCSA进阶,希望我的博客可以帮助到您,感谢您的阅读,有什么问题还希望一起探讨交流,感谢各位的支持,感谢!
0、RHCSA——使用Linux搭建一个简单的论坛(这里即可跳转Linux基础内容)
=1001.2014.3001.5501
1、RHCE——一、安装部署及例行性工作
=1001.2014.3001.5501
2、RHCE——二、时间服务器
=1001.2014.3001.5501
3、RHCE——三、远程连接服务器
=1001.2014.3001.5501
4、RHCE——四、Web服务器(理论篇)
=1001.2014.3001.5501
5、RHCE——五、Web服务器及内网穿透(实验篇)
=1001.2014.3001.5501
6、RHCE——六、基于https协议的静态网站
=1001.2014.3001.5501
7、RHCE——七、搭建云存储器
=1001.2014.3001.5501
8、RHCE——八、DNS域名解析服务器
=1001.2014.3001.5501
9、RHCE——九、SELinux
=1001.2014.3001.5501
10、RHCE——十、防火墙、iptables、firewalld
=1001.2014.3001.5501
11、RHCE——十一、NFS服务器
=1001.2014.3001.5501
12、RHCE——十二、Mysql服务
=1001.2014.3001.5501
13、RHCE——十三、Shell自动化运维编程基础
=1001.2014.3001.5501
14、RHCE——十四、变量和引用
=1001.2014.3001.5501
15、RHCE——十五、shell条件测试
=1001.2014.3001.5501
16、RHCE——十六、流程控制之条件判断、循环
=1001.2014.3001.5501
17、RHCE——十七、文本搜索工具-grep、正则表达式
=1001.2014.3001.5501
18、RHCE——十八、shell编程之sed
=1001.2014.3001.5501
19、RHCE——十九、shell编程之awk
=1001.2014.3001.5501
20、RHCE——二十、Ansible及安装与配置
=1001.2014.3001.5501
21、RHCE——二十一、Ansible模块
=1001.2014.3001.5501
首先可以参考RHCSA的第一篇文章系统的安装:
红帽虚拟机安装
新手建议先看初级学习(即上链接)
这里单个文件创建和多个文件创建的区别:
单文件便于写入,多文件便于移动。
登录:点击“未列出”输入账户root和密码后登录
打开终端:活动->终端
关闭锁屏:活动->九个点按钮(显示应用程序)->设置->隐私->锁屏->息屏延时改为从不->关闭自动锁屏
[root@server ~]# cd /pos.d
[root@pos.d]# po po.back # 此步骤可选
# 使用xftp上传po 配置文件
# 提示系统未注册,可执行以下2步,将enabled=1修改为0,但也可以不该,不影响使用
[root@pos.d]# vim /etc/yum/pluginconf.f
enabled=0
[root@pos.d]# vim /etc/yum/pluginconf.f
enabled=0
[root@pos.d]# yum makecache # 制作缓存
[root@server ~]# cd ~
[root@server ~]# hostnamectl set-hostname server # server为主机名
[root@server ~]# reboot # 重启
[root@server ~]# vim /etc/selinux/config # 编辑SELinux配置文件修改为关闭
SELINUX=disabled[root@server ~]# systemctl stop firewalld # 关闭防火墙
[root@server ~]# systemctl disable firewalld #v取消开机启动
方式一:可视化配置:nmtui
格式:
[root@server ~]# nmtui
增加一个IP地址
激活并重启查看;
[root@server ~]# nmcli c up ens160
[root@server ~]# nmcli c reload
[root@server ~]# ip addr
方式二:nmcli命令
[root@server ~]# nmcli --help
用法:nmcli [选项] 对象 { 命令 | help }
[root@server ~]# nmcli c show
[root@server ~]# nmcli c show 网卡名 # 查看网卡详细信息,点q退出
[root@server ~]# nmcli dev status # 查看已有设备的状态
[root@server ~]# nmcli dev show # 查看所有硬件设备状态
[root@server ~]# nmcli c up 网卡名
[root@server ~]# nmcli c down 网卡名 # 停用连接
[root@server ~]# nmcli c add type ethernet ifname ens160 con-name ens161 autoconnect yes ip4 192.168.48.135/24 gw4 192.168.48.2
[root@server ~]# nmcli c up ens161 # 激活
[root@server ~]# nmcli c show
命令解释
[root@server ~]# nmcli c mod ens160 hod manual ipv4.addresses '192.168.48.150/24' ipv4.gateway '192.168.48.2' ipv4.dns '114.114.114.114' [root@server ~]# nmcli c up ens160 # 激活
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/7)
[root@server ~]# ip a # 查看结果
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000link/ether 00:0c:29:dc:cf:14 brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 192.168.48.150/24 brd 192.168.48.255 scope global noprefixroute ens160
[root@server ~]# vim /etc/NetworkManager/ection
# 配置:
[ipv4]
address1=192.168.48.150/24,192.168.48.2
dns=114.114.114.114;
method=manual
# 格式
[ipv4]
address1=IP地址/24,网关地址
dns=114.114.114.114;
method=manual
删除网络连接;
[root@server ~]# nmcli c show # 查看网络连接
[root@server ~]# nmcli c del ens161 # 删除
[root@server ~]# nmcli c show # 再次查看
场景:
生活中,我们有太多场景需要使用到闹钟,比如早上 7 点起床,下午 4 点开会,晚上 8 点购物,等等。
在 Linux 系统里,我们同样也有类似的需求。比如我们想在凌晨 1 点将文件上传服务器,或者在晚上 10 点确认系统状态,等等。
但我们不可能一直守在电脑前,毕竟我们也需要下班/睡觉,还要陪女朋友(new一个也行)。而且即使在上班期间,如果到点了还需要人工操作,未免效率太低了。
at
命令就是为这个需求而诞生的。使用 at
命令,你可以在特定时间自动完成你所设定的任务,也可以实现自动化,非常方便快捷!
单一执行的例行性任务–at(一次性)
1、安装
[root@server ~]# at -V
at version 3.1.23
[root@server ~]# systemctl status atd # 查看状态
[root@server ~]# systemctl enable --now atd # 开机启动并当前立刻启动
2、at命令详解
1、格式:
at -参数 日期时间
2、举例
[root@server ~]# at 14:08
warning: commands will be executed using /bin/sh
at> ls /root > at.txt
at> <EOT> # 按下ctrl+d退出输入状态
job 1 at Sat Aug 19 14:08:00 2023
[root@server ~]# at -l # 浏览计划任务列表
[root@server ~]# ls # 查看结果
公共 模板 视频 图片 文档 下载 音乐 桌面 anaconda-ks.cfg at.txt
[root@server ~]# cat at.txt
3、参数详情:
-l:列出系统中所有用户的at任务
atq:等价于at -l-d :删除或取消一个任务
-v :使用时间格式累出at任务
-c :可以列出任务后的命令内容
-f :从文件中读取
4、举例:
[root@server ~]# echo "echo 'hello again' >> at.txt" | at now +1 minute^C
[root@server ~]# cat at.txt
5、时间格式:
参数 | 说明 |
---|---|
HH:MM | 在今天HH小时的MM分钟执行,若时间错过则在明天执行 |
HH:MM YYYY-MM-DD | 规定在某年某月某日的特殊时间执行 |
now +数字 时间单词 | now + 2 mintues now + 3 hours mow +4 days now +5 weeks |
6、注意:
7、at命令执行过程分析
8、举例
[root@server ~]# useradd ce1 # 新建账户
[root@server ~]# passwd ce1 # 设置密码
更改用户 ce1 的密码 。
新的密码:
无效的密码: 密码少于 8 个字符
重新输入新的密码:
passwd:所有的身份验证令牌已经成功更新。
[root@server ~]# id ce1 # 查看账户信息
用户id=1001(ce1) 组id=1001(ce1) 组=1001(ce1)[root@server ~]# tail -1 /etc/passwd # 查看账户信息
ce1:x:1001:1001::/home/ce1:/bin/bash[root@server ~]# vim /etc/at.deny # 编辑黑名单
# 写入ce1[root@server ~]# su ce1 # 切换账户
[ce1@server root]$ cd ~
[ce1@server ~]$ at 14:44 # 由于ce1在黑名单中,则禁止使用at命令
You do not have permission to use at.
[ce1@server ~]$ su root # 切换回root账户
密码:
[root@server ce1]# vim /etc/at.deny # 编辑黑名单
# 删除ce1
[root@server ce1]# su ce1 # 切换账户
[ce1@server ~]$ at 14:46 # 可以使用at命令
warning: commands will be executed using /bin/sh
at>
at> <EOT> # ctrl+d 退出
job 6 at Sat Aug 19 14:46:00 2023
[ce1@server ~]$ su root
9、练习白名单以及黑名单:
[root@server ~]# id ce1
用户id=1001(ce1) 组id=1001(ce1) 组=1001(ce1)
[root@server ~]# tail -1 /etc/passwd
ce1:x:1001:1001::/home/ce1:/bin/bash
[root@server ~]# vim /etc/at.deny
[root@server ~]# cat /etc/at.deny
ce1
[root@server ~]# su ce1
[ce1@server root]$ cd
[ce1@server ~]$ at 14:44
You do not have permission to use at.
[ce1@server ~]$ su root
密码:
[root@server ce1]# vim /etc/at.deny
[root@server ce1]# cat /etc/at.deny [root@server ce1]# su ce1
[ce1@server ~]$ at 14:45
warning: commands will be executed using /bin/sh
at> <EOT>
job 3 at Sun Aug 20 14:45:00 2023
[ce1@server ~]$ su root
密码:
[root@server ce1]# cd
[root@server ~]# vim /etc/at.allow
[root@server ~]# su fox
[fox@server root]$ at
You do not have permission to use at.
[fox@server root]$ su root
密码:
su: 鉴定故障
[fox@server root]$ su root
密码:
[root@server ~]# vim /etc/at.allow
[root@server ~]# cat /etc/at.allow
fox
[root@server ~]# su fox
[fox@server root]$ at
Garbled time
[fox@server root]$ at 14:56
warning: commands will be executed using /bin/sh
at> <EOT>
job 4 at Sat Aug 19 14:56:00 2023
[fox@server root]$ su root
密码:
[root@server ~]# vim /etc/at.deny
[root@server ~]# su fox
[fox@server root]$ at
Garbled time
[fox@server root]$ su root
密码:
su: 鉴定故障
[fox@server root]$ su root
密码:
[root@server ~]# vim /etc/at.allow
[root@server ~]# cat /etc/at.deny
fox
[root@server ~]# su fox
[fox@server root]$ at
You do not have permission to use at.
循环执行的例行性任务–crontab(周期性)
1、crontd服务
at
命令是在指定的时间只能执行一次任务,crontab
命令可以循环重复的执行定时任务,与Windows 中的计划任务有些类似[root@server ~]# yum install crontabs # 安装
[root@server ~]# systemctl status crond # 查看状态
crond 进程每分钟会定期检查是否有要执行的任务,如果有,则会自动执行该任务,crontab 命令需要 crond 服务支持
linux 任务调度的工作主要分为以下两类:
2、crontab工作过程
当系统中有 /etc/cron.allow 文件时,只有写入此文件的用户可以使用 crontab 命令,没有写入的用户不能使用 crontab 命令。同样,如果有此文件,/etc/cron.deny 文件会被忽略,因为 /etc/cron.allow 文件的优先级更高
当系统中只有 /etc/cron.deny 文件时,写入此文件的用户不能使用 crontab 命令,没有写入文件的用户可以使用 crontab 命令
crontab 执行的每一项工作都会被 记录到 /var/log/cron 这个日志文件中
当用户使用 crontab 新建工作之后,该项工作就会被记录到 /var/spool/cron/目录里面
3、crontab命令详解
(1)、格式
crontab [-u user] [-l| -r | -e]
(2)、参数
参数 | 说明 |
---|---|
-u | 只有root才能执行该任务 |
-e | 编辑crontab工作内容 |
-l | 查询crontab任务内容 |
-r | 删除cronatb任务 |
(3)、编辑crontab
crontab -e
表示意义 | 分钟 | 小时 | 日期 | 月份 | 星期 |
---|---|---|---|---|---|
数字范围 | 0-59 | 0-23 | 1-31 | 1-12 | 0-7(0和7都表示星期日) |
(4)、特殊字符
* : 任意时刻
- : 表示范围
,(逗号) :分割时段
*/数字 :指定时间间隔频率,如:每3分钟执行一次:*/3
(5)、时间描述示例:
1 2 * * * : 每天凌晨2点1分执行动作
0 17 * * 1 : 每周一下午17点整执行动作
0 5 1,15 * * :每月1日和15日凌晨5点执行动作
40 4 * * 1-5 :每周一至周五凌晨4点40分执行动作
*、10
*/10 4 * * * : 每天凌晨4点每隔10分钟执行动作
3,15 8-11 */2 * * :每隔2天上午8-11点的第3分钟和第15分钟执行动作
* * * * * :每隔1分钟执行动作
[root@server ~]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root# For details see man 4 crontabs# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
分析
[root@server ~]vim test.sh
#!/bin/bashwhile :
doecho "this is my test sh"$(date) >> /sleep 1
done
[root@server ~]# crontab -e
*/1 * * * * /bin/bash /root/test.sh
注意:crond服务会自动启动,crontab命令只要保存就会生效
[root@server ~]# crontab -l*/1 * * * * /bin/bash /root/test.sh
[root@server ~]# cat /
[root@server ~]# crontab -r # 删除
[root@server ~]# crontab -l # 查看
no crontab for root
[root@server ~]# ps -aux | grep test # 查看已存在的test进程
[root@server ~]# kill -9 进程号 # 强制终止
示例:
[root@server ~]# ps -aux | grep test
root 2514 0.1 0.1 222516 3460 ? Ss 15:31 0:00 /bin/bash /root/test.sh
root 2664 0.1 0.1 222516 3404 ? Ss 15:32 0:00 /bin/bash /root/test.sh
root 2918 0.1 0.1 222516 3476 ? Ss 15:33 0:00 /bin/bash /root/test.sh
root 3445 0.0 0.1 221680 2360 pts/0 S+ 15:34 0:00 grep --color=auto test
[root@server ~]# kill -9 2514
[root@server ~]# kill -9 2664
[root@server ~]# kill -9 2918
[root@server ~]# ps -aux | grep test
root 3753 0.0 0.1 221680 2344 pts/0 S+ 15:35 0:00 grep --color=auto test
[root@server ~]# yum install s-nail -y
上次元数据过期检查:0:51:48 前,执行于 2023年08月19日 星期六 14时56分45秒。
依赖关系解决。
=========================================================================================================================================================软件包 架构 版本 仓库 大小
=========================================================================================================================================================
安装:s-nail x86_64 14.9.22-8.el9 AppStream 622 k事务概要
=========================================================================================================================================================
安装 1 软件包总下载:622 k
安装大小:1.1 M
下载软件包:
s-nail-14.9.22-8.el9.x86_64.rpm 851 kB/s | 622 kB 00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------
总计 847 kB/s | 622 kB 00:00
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务准备中 : 1/1 运行脚本: s-nail-14.9.22-8.el9.x86_64 1/1 安装 : s-nail-14.9.22-8.el9.x86_64 1/1 运行脚本: s-nail-14.9.22-8.el9.x86_64 1/1 验证 : s-nail-14.9.22-8.el9.x86_64 1/1 已安装:s-nail-14.9.22-8.el9.x86_64 完毕!
[root@server ~]# vim /
这里在最后加下面这几条命令。
set from=17674044@qq
set smtp=smtp.qq
set smtp-auth-user=qq邮箱
set smtp-auth-password=授权码
set smtp-auth=login
注意:文件配置完毕需要强制保存并退出,末行模式下输入wq!(由于这个文件为只读文件)
查看:
[root@server ~]# vim /
[root@server ~]# tail -5 /
set smtp=smtp.qq
set smtp-auth-user=3103646008@qq
set smtp-auth-password=rxjivbmohdytddjh
set smtp-auth=login[root@server ~]# ll /
-r--r--r-- 1 root root 9771 8月 19 15:57 /
[root@server ~]# echo "testmail" | mail -s "testmail" 1927875806@qq
s-nail: Warning: $LOGNAME (fox) not identical to user (root)!
s-nail: Warning: $USER (fox) not identical to user (root)!
s-nail: Warning: variable superseded or obsoleted: smtp
s-nail: Warning: variable superseded or obsoleted: smtp-auth-user
s-nail: Warning: variable superseded or obsoleted: smtp-auth-password
s-nail: Obsoletion warning: please do not use *smtp*, instead assign a smtp:// URL to *mta*!
s-nail: Obsoletion warning: Use of old-style credentials, which will vanish in v15!
s-nail: Please read the manual section "On URL syntax and credential lookup"
[root@server ~]# echo "testmail" | mail -s "testmail" 3103646008@qq
s-nail: Warning: $LOGNAME (fox) not identical to user (root)!
s-nail: Warning: $USER (fox) not identical to user (root)!
s-nail: Warning: variable superseded or obsoleted: smtp
s-nail: Warning: variable superseded or obsoleted: smtp-auth-user
s-nail: Warning: variable superseded or obsoleted: smtp-auth-password
s-nail: Obsoletion warning: please do not use *smtp*, instead assign a smtp:// URL to *mta*!
s-nail: Obsoletion warning: Use of old-style credentials, which will vanish in v15!
s-nail: Please read the manual section "On URL syntax and credential lookup"
这里我们发送了两封测试邮件,发送成功则为上一步配置成功。
[root@server ~]# crontab -e
MAILTO=qq邮箱* * * * * echo "警告,server服务器内存较低,请速处理." | mail -v -s "告警邮件" qq邮箱
这里配置即为每分钟发送一次。
每天6:00将日志文件/var/log/messages 文件进行备份,备份到/backup目录中,备份时需要将文件名修改为logfileYYYY-MM-DD-HH:MM:SS
[root@server ~]# mkdir /backup[root@server ~]# vim /etc/crontab
0 6 * * * root /usr/bin/cp /var/log/messages /backup/logfile`date +%Y-%m-%d-%H:%M:%S` # 注意# 反引号``:表示将引起的内容识别为命令# 在crontab中%表示换行,需要增加转义符进行转义
[root@server ~]# mkdir /backup
[root@server ~]# vim /etc/crontab
[root@server ~]# vim /etc/crontab
[root@server ~]# cd /backup/
[root@server backup]# ls
logfile2023-08-19-16:37-01 logfile2023-08-19-16:38-01
[root@server backup]# ll
总用量 2344
-rw------- 1 root root 1196788 8月 19 16:37 logfile2023-08-19-16:37-01
-rw------- 1 root root 1196788 8月 19 16:38 logfile2023-08-19-16:38-01
[root@server backup]# cd
[root@server ~]# vim /etc/crontab
[root@server ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root# For details see man 4 crontabs# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
本文发布于:2024-02-02 00:19:18,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170680996640140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |