RHCE笔记1

一、单一例行服务

1.At命令

        at命令可以用来在某一时段执行一次任务,只会执行一次,不会重复执行。

        at的守护进程atd会以后台模式运行,检查作业队列来运行。

        默认情况下,atd守护进程每60秒检查作业队列,有作业时,会检查队列中的所有作业运行时间,如果时间与当前时间匹配,则运行此作业。(也就是说,at守护进程的最小时间单位是分钟)

注意:这里的队列并不是说第一个执行了后面的才能执行,每次都会检查所有的任务的保证atd进程的启动。

可以使用相关指令来查看 ps -ef | grep atd

2.at指令详解

at [选项] [时间]

        atq---查看队列

        atrm 编号 ---删除人物

3..at指定时间的方法

        1.接受在当天的hh:mm (小时:分钟)式的时间指定。假如该时间已过去,那么就放在第二天执行。例如:at 17:00

        2.使用midnight(深夜),noon(中午),teatime(饮茶时间,一般是下午4点)等模糊的词语来指定时间。

        3.采用12小时计时制,即在时间后面加上AM(上午)或PM(下 午)来说明是上午还是下午。例如:12pm

     4.指定命令执行的具体日期,指定格式为month day(月日)或mm/dd/yy(月/日/年)或dd.mm.yy(日.月.年),指定的日期必须跟在指定时间的后面。例如:04:00 2021-03-1

        5.使用相对计时法。指定格式为:now + count timeunits。其中now就是当前时间time-units是时间单位,这里能够是minutes(分 钟),hours (小时)、days(天)、weeks(星期) count是时间的数量,几天,几小时 例如:now + 5 minutes

        6、直接使用today (今天)、tomorrow(明天)来指定完成命令的时间。

4.作业一:at命令的白名单和黑名单

为了安全起见,我们不能让任意用户都可以使用at命令,以防止主机被劫持或者被黑客利用,所以我们还需要对at命令进行管控。主要通过两个文件来实现对at命名使用的限制,在没有这两个文件之前,at命令的执行是在编辑好任务后直接等待atd 服务的执行,但是在有这两个 文件之后,at命令的执行就发生了改变,它首先要通过这两个文 件进行用户的限制,具体流程如下:要注意的是一般假设系统中所有用户都是可信任的,故而系统中at.allow文件是默认不存在的,而at.deny是默认存在且为空的,从而允许所有人都可以使用 at命令。

实验:限制用户使用at命令,实验开始前我们需要另外创建两个用户——fire和mars。我们分别尝试: 将mars增加至at.allow文件中,观察; 将fire增加至at.deny文件中,观察;

1、创建用户

2、将mars写入at.allow文件中,此时切换至两个用户观察他 们是否可以使用at命令:

3、在两个文件都存在的情况下,将fire写入at.deny文件中,

4、此时切换至两个用户观察他们是否可以使用at命令 在deny和allow文件都存在的情况下,我们的系统只会扫描读 取allow文件中的内容,此时我们的allow文件内容为空,也就意味着我们不允许任何用户使用at命令,当然linux系统中的超级用户——root除外。

#创建两个用户并修改密码
[root@localhost home]# useradd mars
[root@localhost home]# useradd fire
[root@localhost home]# ls
fire  mars  roo
[root@localhost home]# passwd mars
Changing password for user mars.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@localhost home]# passwd fire
Changing password for user fire.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@localhost home]# cd /etc
[root@localhost etc]# ls at.allow
ls: cannot access 'at.allow': No such file or directory
#创建at.allow文件并将mars写入
[root@localhost etc]# vim at.allow
[root@localhost etc]# vim at.allow
[root@localhost etc]# ls at.deny
at.deny
#将fire写入at.deny文件中
[root@localhost etc]# vim at.deny
[root@localhost etc]# su - mars
[mars@localhost ~]$ at now + 1min
warning: commands will be executed using /bin/sh
at> echo "hello mars" > /home/mars/log.txt
at> <EOT>
job 7 at Mon May 13 16:22:00 2024
[mars@localhost ~]$ su - fire
Password: 
[fire@localhost ~]$ at now + 2 min
You do not have permission to use at.
[fire@localhost ~]$ 
#fire无权限使用at命令

二、定时执行任务

定时执行的例行性工作:每隔一定的周期就需要执行一次

1、crontab介绍

Crontab 是一个在 Unix 和 Linux 操作系统上用于定时执行任务的工具。它允许用户创建和管理计划任务,以便在特定的时间间隔或时间点自动运行命令或脚本。Crontab是cron table的缩写,cron指的是Unix系统中的一个后台进程,它用于执行预定的任务。

2、crontab 命令工作流程

crontab 命令通常用于在 Linux 系统中设置定期执行的任务,并将这些任务存储在 crontab 文件中。

为了使用 crontab 命令,系统需要依赖crond 服务的支持。通常情况下,在操作系统安装时,crond服务会被默认安装并自动启动。crond进程会每分钟定期检查crontab文件中的任务列表,以确定是否有需要执行的任务,如果有,它会自动执行这些任务。当用户使用crontab命令新建任务计划之后,该项 jobs 就会被 /var/spool/cron/ 目录下,

而且以用户账号来创建一个文件,每一项任务计划为一行。

crond 检测的时间周期是 “分钟”, 每分钟会读取一次 /etc/crontab,以及 /var/spool/cron 里面的记录并执行。

crond 执行的每一项任务计划,都会被记录到 /var/log/cron 这个日志文件。

两个对象:后台服务 crond ===》读取 配置文件 crontab文件

前台命令 crontab ===》结果 配置文件crontab 文件

通过: ps -ef |grep crond 验证是否后台服务启动

3、Crontab命令语法

crontab [-u user] file crontab [-u user] [ -e | -l | -r ]

 -u user用来设定某个用户的crontab服务。

-file:file是命令文件的名字,表示将file做为crontab的任务列表文件并载入crontab。

 如果在命令行中没有指定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将它们载入crontab。

-e:编辑某个用户的crontab文件内容。如果不指定用户,则表示编辑当前用户的crontab文件。

-l:显示某个用户的crontab文件内容,如果不指定用户,则表示显示当前用户的crontab文件内容。

-r:从/var/spool/cron目录中删除某个用户的crontab文件,如果不指定用户,则默认删除当前用户的crontab文件。

-i:在删除用户的crontab文件时给确认提示。以下是可以用于指定时间的特殊符号:

"*" 代表所有的取值范围内的数字,任何时刻都接受。特别要注意!

"*/n" 表示每n个单位间隔

"-" 时间范围

"," 分隔的时段

4、Crontab计划任务的格式

5、操作案例:

crontab -e    # 编辑crontab文件内容

# 每天的凌晨2点执行重启 ftp 共享服务

00 02 * * * service vsftpd restart

crontab -l    # 查看当前有几条计划任务

# 每个月的15号凌晨三点半重启服务器

30 03 15 * * /usr/sbin/reboot        # whereis reboot 查看reboot指令位置

 # 每周日的凌晨一点半

30 01 * * 7 xxxx

,:离散时间点取值

10,40 * * * * 每小时的第10分钟和第40分钟

10,40 * * * 2,5 每周二和周五的每小时的第10分钟和第40分钟

-:连续时间点

10 02 * * * 1-5 每周一到五的2点10分

/:对应取值范围每多久一次

*/3 * * * * 每3分钟

作业二:重复执行数据库备份

# mysql 安装:

 dnf install mysql-server #安装

 systemctl enable mysqld #开机启动

 systemctl start mysqld #开启服务

 mysql_secure_installation #初始密码和用户配置

 mysql -uroot -p #登录数据库 123abc!@#

mkdir /tmp/backup # 创建脚本文件

vim mysql_backup.sh

#!/bin/bash

mysqldump -uroot -p123456 -B mysql > /tmp/backup/mysql.sql

crontab -e

# 每一分钟执行备份数据库脚本

*/1 * * * * sh /root/mysql_backup.sh

# 查看日志

tail -f /var/log/cron

# 进入目录,库已经备份

cd /tmp/backup/

流程如下:

[roo@localhost ~]$ su - root
Password: 
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
redhat.repo
[root@localhost yum.repos.d]# vim aliyun.repo
[root@localhost yum.repos.d]# dnf install mysql-server
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

appstream                                       1.8 MB/s |  19 MB     00:10    
baseos                                          4.8 MB/s | 8.1 MB     00:01    
Last metadata expiration check: 0:00:02 ago on Wed 15 May 2024 09:42:24 AM CST.
Dependencies resolved.
================================================================================
 Package                       Arch      Version             Repository    Size
================================================================================
Installing:
 mysql-server                  x86_64    8.0.36-1.el9        appstream     17 M
Installing dependencies:
 mariadb-connector-c-config    noarch    3.2.6-1.el9         appstream     11 k
 mecab                         x86_64    0.996-3.el9.4       appstream    356 k
 mysql                         x86_64    8.0.36-1.el9        appstream    2.8 M
 mysql-common                  x86_64    8.0.36-1.el9        appstream     74 k
 mysql-errmsg                  x86_64    8.0.36-1.el9        appstream    505 k
 mysql-selinux                 noarch    1.0.5-1.el9         appstream     36 k
 protobuf-lite                 x86_64    3.14.0-13.el9       appstream    232 k

Transaction Summary
================================================================================
Install  8 Packages

Total download size: 21 M
Installed size: 179 M
Is this ok [y/N]: y
Downloading Packages:
(1/8): mariadb-connector-c-config-3.2.6-1.el9.n  37 kB/s |  11 kB     00:00    
(2/8): mysql-common-8.0.36-1.el9.x86_64.rpm     280 kB/s |  74 kB     00:00    
(3/8): mecab-0.996-3.el9.4.x86_64.rpm           416 kB/s | 356 kB     00:00    
(4/8): mysql-errmsg-8.0.36-1.el9.x86_64.rpm     750 kB/s | 505 kB     00:00    
(5/8): mysql-selinux-1.0.5-1.el9.noarch.rpm      91 kB/s |  36 kB     00:00    
(6/8): mysql-8.0.36-1.el9.x86_64.rpm            1.0 MB/s | 2.8 MB     00:02    
(7/8): protobuf-lite-3.14.0-13.el9.x86_64.rpm   108 kB/s | 232 kB     00:02    
(8/8): mysql-server-8.0.36-1.el9.x86_64.rpm     2.6 MB/s |  17 MB     00:06    
--------------------------------------------------------------------------------
Total                                           2.7 MB/s |  21 MB     00:07     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : mariadb-connector-c-config-3.2.6-1.el9.noarch          1/8 
  Installing       : mysql-common-8.0.36-1.el9.x86_64                       2/8 
  Installing       : mysql-8.0.36-1.el9.x86_64                              3/8 
  Installing       : mysql-errmsg-8.0.36-1.el9.x86_64                       4/8 
  Installing       : protobuf-lite-3.14.0-13.el9.x86_64                     5/8 
  Running scriptlet: mysql-selinux-1.0.5-1.el9.noarch                       6/8 
  Installing       : mysql-selinux-1.0.5-1.el9.noarch                       6/8 
  Running scriptlet: mysql-selinux-1.0.5-1.el9.noarch                       6/8 
libsemanage.semanage_direct_install_info: Overriding mysql module at lower priority 100 with module at priority 200.

  Installing       : mecab-0.996-3.el9.4.x86_64                             7/8 
  Running scriptlet: mysql-server-8.0.36-1.el9.x86_64                       8/8 
  Installing       : mysql-server-8.0.36-1.el9.x86_64                       8/8 
  Running scriptlet: mysql-server-8.0.36-1.el9.x86_64                       8/8 
  Running scriptlet: mysql-selinux-1.0.5-1.el9.noarch                       8/8 
  Running scriptlet: mysql-server-8.0.36-1.el9.x86_64                       8/8 
  Verifying        : mariadb-connector-c-config-3.2.6-1.el9.noarch          1/8 
  Verifying        : mecab-0.996-3.el9.4.x86_64                             2/8 
  Verifying        : mysql-8.0.36-1.el9.x86_64                              3/8 
  Verifying        : mysql-common-8.0.36-1.el9.x86_64                       4/8 
  Verifying        : mysql-errmsg-8.0.36-1.el9.x86_64                       5/8 
  Verifying        : mysql-selinux-1.0.5-1.el9.noarch                       6/8 
  Verifying        : mysql-server-8.0.36-1.el9.x86_64                       7/8 
  Verifying        : protobuf-lite-3.14.0-13.el9.x86_64                     8/8 
Installed products updated.

Installed:
  mariadb-connector-c-config-3.2.6-1.el9.noarch                                 
  mecab-0.996-3.el9.4.x86_64                                                    
  mysql-8.0.36-1.el9.x86_64                                                     
  mysql-common-8.0.36-1.el9.x86_64                                              
  mysql-errmsg-8.0.36-1.el9.x86_64                                              
  mysql-selinux-1.0.5-1.el9.noarch                                              
  mysql-server-8.0.36-1.el9.x86_64                                              
  protobuf-lite-3.14.0-13.el9.x86_64                                            

Complete!
[root@localhost yum.repos.d]# systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost yum.repos.d]# systemctl start mysqld
[root@localhost yum.repos.d]# mysql_secure_installation 

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : n

 ... skipping.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
[root@localhost yum.repos.d]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.36 Source distribution

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.03 sec)

mysql> exit
Bye
[root@localhost yum.repos.d]# cd ~
[root@localhost ~]# mkdir /tmp/backup
[root@localhost ~]# vim mysql_bcakuo.sh
[root@localhost ~]# vim mysql_bcakuo.sh
[root@localhost ~]# ls
anaconda-ks.cfg  mysql_bcakuo.sh
[root@localhost ~]# mv mysql_backuo.sh mysql_backup.sh
mv: cannot stat 'mysql_backuo.sh': No such file or directory
[root@localhost ~]# mv mysql_bcakuo.sh mysql_backup.sh
[root@localhost ~]# ls
anaconda-ks.cfg  mysql_backup.sh
[root@localhost ~]# pwd
/root
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]# tail -f /var/log/cron

三、搭建FTP服务器

1.安装ftp服务器

离线安装:rpm -ivh vsftpd-1.1.3-8.i386.rpm
在线安装:yum install vsftpd
 卸载程序:yum remove vsftpd

2.修改配置文件

cd /etc/vsftpd #进入vsftp的配置目录
cp vsftpd.conf vsftpd.conf_bak #将原始配置文件备份
vim /etc/vsftpd/vsftpd.conf #修改配置文件
以下配置适用于rhel9.3 
anonymous_enable=YES
anon_upload_enable=YES
anon_mkdir_write_enable=YES


配置项的说明:
local_enable=NO|YES #本地登陆控制,NO表示禁止,YES表示允许
anonymous_enable=yes/no #是否允许匿名用户访问 。 yes表示允许,no表示不允许
anon_upload_enable=YES/NO #上传控制 YES表示同意,NO表示否定
anon_world_readable_only=NO/YES #下载控制,NO表示可以下载不可读文件,YES表示不能下载
anon_mkdir_write_enable=YES|NO #目录创建控制 
max_clients=2 #表示最大同时访问数为2
#local_root=/mnt #家目录控制,默认进入的家目录是进入登录用户的家目录
write_enable=NO|YES #写权限控制,NO表示不允许写,YES表示允许
chroot_local_user=YES #锁定用户到自己的家目录中
vsftpd.conf 配置文件里可以写入此参数:
userlist_deny=NO 表示允许user_list的用户登录,则user_list变白名单。
/etc/vsftpd/ftpusers ##放入的用户永久黑名单
/etc/vsftpd/user_list ##放入的用户默认黑名单

3.修改本地上传目录权限

cd /var/ftp/pub
mkdir /var/ftp/pub/upload
chmod 777 /var/ftp/pub/upload -R
chmod 777 /var/ftp/pub -R

4.重启FTP服务

systemctl restart vsftpd.service
systemctl status vsftpd.service

5.测试

在win10 的地址栏输入: ftp://服务器ip地址,可以看到默认的共享文件 pub。

6.存在的问题

如果无法访问,权限不足 请检查以下两项是否关闭。
1、关闭 SELinux
setenforce 0
getenforce 查看是否关闭:----Permissive 是关闭状态
或者:vim /etc/sysconfig/selinux 将 SELINUX=enforcing 改为 SELINUX=disabled 然后重启
2、关闭 防火墙 
systemctl stop firewalld #关闭防火墙
systemctl status firewalld #查看状态
systemctl disable firewalld #关闭防火墙开机启动
ftp客户端操作命令:
1、ftp IP地址,默认账号:ftp 密码: ftp
2、dir 查看目录内容
3、cd xxx 切换目录
4、get xxx.txt 下载文件 
5、put xxx.txt 上传文件
6、by 退出环境

拓展-虚拟用户访问:
1、建立认证文件模板
建立模板需要在/etc/vsftp/的目录下完成
建立 “vim /etc/vsftpd/ftp_auth_file”


写3个虚拟用户和密码,写的时候注意后面的空格,空格也是会被计算在内的。
2、加密认证文件
然后我们把写出的文件,转换成系统可识别的认证文件
cd /etc/vsftpd/
“db_load -T -t hash -f ftp_auth_file ftp_auth_file.db”
转换出的就是一串加密字符
3、建立pam认证文件
pam是我们系统中的一个插件程序,用来确认配对信息,
例如我们用ftp登录主机用户获取信息是,需要先输入用户名,然后输入密码才可以进入,可以来查看pam 的配置目录里的文件内容。
在pam.d目录里是有vsftp的认证文件的,文件内容我们列出来了,
所以我们建立的虚拟用户也需要建立这样的认证文件才可以让别人访问
建立文件 vim /etc/pam.d/test ,test这个文件名可以自定义
[root@localhost pam.d]# cat test 
account required pam_userdb.so
db=/etc/vsftpd/ftp_auth_file
auth required pam_userdb.so
db=/etc/vsftpd/ftp_auth_file
我们来分析一下这个文件内容:
account:账户
auth:认证,也就是密码
required:必须通过,强制认证
pam_userdb.so:pam的插件名称
db=/etc/vsftpd/ftp_auth_file:
指定pam插件认证的文件,注意的是我们刚转换的格式
ftp_auth_file.db,
但是这里默认找的就是后缀为 .db 的文件,所以不需要加 .db
 4、编辑vsftp主配置文件
vim /etc/vsftpd/vsftpd.conf
pam_service_name=test:指定认证的文件,之前用的是/
etc/pam.d/vsftp
guest_enable=YES:指定虚拟用户功能开启
guest_username=ftp:指定虚拟用户在ftp服务器上的用户身份,
 ftp的家目录就是/var/ftp/pub目录
可以查看/etc/passwd里的内容找到
完成这些设定后就完成了虚拟用户的建立, 重启服务
systemctl restart vsftpd
就可以测试登录查看。
5、测试登录

拓展-虚拟用户家目录的独立设定
默认的虚拟用户登录的家目录都在/var/ftp里,那他们创建的目录上传的东西都会在一起,如果要删除某个虚拟用户的文件,找起来就很麻烦了,所以为了方便管理将这些虚拟用户建立分开的家目录。
首先去给每个虚拟用户建立他们的家目录
mkdir /home/test{1..3}
touch /home/test1/abc1
touch /home/test2/abc2
touch /home/test3/abc38/8
编辑配置文件:vim /etc/vsftpd/vsftpd.conf
写入两条配置
local_root=/home/$USER: 指定用户的登录的家目录, 
$USER是系统对于用户的一个变量指代
user_sub_token=$USER: 指定上面写的 $USER是系统中的,否则vsftp无法识别
完成后,依然是重启服务
systemctl restart vsftpd
测试登录

  • 19
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值