RHCSA
练习题1
配置网络设置
在 mars.lab.example.com
上执行
将 mars
配置为具有以下网络配置:
- 主机名: mars.lab.example.com
- IP 地址: 192.168.122.101
- 子网掩码: 255.255.255.0
- 网关: 192.168.122.2
- 名称服务器: 223.5.5.5
作答:
nmcli con mod ens160 ipv4.method manual ipv4.add 192.168.122.101/24 ipv4.gateway 192.168.122.2 ipv4.dns 223.5.5.5
nmcli con up ens160
练习题2
配置您的系统以使用默认存储库
在 mars.lab.example.com 上修改 yum 存储库。
配置您的系统, 以将这些位置用作默认存储库。
Rocky Linux baseos仓库:
https://mirrors.aliyun.com/rockylinux/$releasever/BaseOS/$basearch/os/
Rocky Linux appstream仓库:
https://mirrors.aliyun.com/rockylinux/$releasever/AppStream/$basearch/os/
作答:
cd /etc/yum.repos.d
for i in `ls .`;do mv -f $i $i.bak;done
vim rhcsa.repo
写仓库配置文件
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/rockylinux/$releasever/BaseOS/$basearch/os/
gpgcheck=0
enable=1
[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/rockylinux/$releasever/AppStream/$basearch/os/
gpgcheck=0
enable=1
yum clean all
yum makecache
练习题3×××
调试 SELinux
在 mars.lab.example.com 上执行非标准端口82上运行的 Web 服务器在提供内容时遇到问题。
根据需要调试并解决问题, 使其满足以下条件:
- 系统上的 Web 服务器能够提供 /var/www/html 中所有现有的 HTML 文件( 注: 不要删除或以其他方式改动现有的文件内容)
- Web服务器在端口 82 上提供此内容
- Web服务器在系统启动时自动启动
作答:
yum install -y httpd
vim /etc/httpd/conf/httpd.conf
修改端口
Listen 80 -> Listen 82
systemctl start httpd
journalctl -xe
semanage port -a -t http_port_t -p tcp 82
systemctl restart httpd
systemctl status httpd
firewall-cmd --add-port=82/tcp
firewall-cmd --add-port=82/tcp --permanent
semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
restorecon -R -v /var/www/html
练习题4
创建用户帐户
在 mars.lab.example.com
上执行
创建下列用户、 组和组成员资格:
名为 admins
的组
用户 lucy
,作为次要组从属于 admins
用户 harry
,作为次要组还从属于 admins
用户 jams
,无权访问系统上的交互式shell且不是 admins
的成员
lucy
、harry
和 jams
的密码应当都是 redhat
作答:
groupadd admins
useradd lucy -G admins
useradd harry -G admins
useradd jams -s /usr/sbin/nologin
echo redhat | passwd --stdin lucy
echo redhat | passwd --stdin harry
echo redhat | passwd --stdin jams
练习题5
配置cron作业
在 mars.lab.example.com
上执行
用户 lucy
必须配置一个 cron
作业, 该作业每隔 2 分钟( 当地时间) 运行
且执行:/bin/echo hiya
作答:
yum provides crontab
yum install -y cronie
crontab -e -u lucy
写入定时任务
*/2 * * * * /bin/echo hiya
练习题6
创建协作目录
在 mars.lab.example.com
上执行
创建具有以下特征的协作目录 /home/test
/home/test
的组所有权是admins
- 目录应当可被
admins
的成员读取、 写入和访问, 但任何其他用户不具这些权限。(当然,root
用户有权访问系统上的所有文件和目录) /home/test
中创建的文件自动将组所有权设置到admins
组
作答:
mkdir /home/test
chgrp admins /home/test
chmod 770 /home/test
chmod g+s /home/test
练习题7×××
配置 NTP
在 mars.lab.example.com
上执行
配置您的系统, 使其成为 classroom.example.com
的 NTP 客户端。
考试时提示: classroom.example.com
是 sidecar.lab.example.com
的 DNS
别名
我们这里使用NTP服务器 ntp.aliyun.com
做练习。
作答:
systemctl enable chronyd --now
man chrony.conf
# /EXAMPLES 中找到以下示例
server foo.example.net iburst
server bar.example.net iburst
server baz.example.net iburst
写入配置文件
vim /etc/chrony.conf
# 注释掉 pool 池,添加 ntp 服务器
#pool 2.rocky.pool.ntp.org iburst
server ntp.aliyun.com iburst
systemctl restart chronyd.service
验证
chronyc sources -v
timedatectl | grep -B1 NTP
练习题8×××
配置 autofs
在 mars.lab.example.com
上执行
以按照如下所述自动挂载远程用户的主目录:
classroom.example.com
(192.168.122.100)NFS 导出/rhome
到您的系统。此文件系统包含为用户lily
预配置的主目录。lily
的主目录是classroom.example.com:/rhome/lily
。lily
的主目录应自动挂载到本地/rhome
下的/rhome/lily
。- 主目录必须可供其用户写入。
lily
的密码是redhat
作答:
在 workstation
上操作
用 workstation
模拟 nfs
服务器
mkdir /rhome
useradd lily -d /rhome/lily -u 2001
echo redhat | passwd --stdin lily
yum list | grep nfs
yum install -y nfs-utils
systemctl enable nfs-server --now
firewall-cmd --add-service=mountd --permanent
firewall-cmd --add-service=nfs --permanent
firewall-cmd --add-service=rpc-bind --permanent
firewall-cmd --reload
vim /etc/exports
写配置文件
/rhome/lily 192.168.122.0(rw,sync)
查看本地共享目录
showmount -e
在 mars
上操作
mkdir /rhome
useradd lily -d /rhome/lily -u 2001
echo redhat | passwd --stdin lily
yum install -y nfs-utils
showmount -e serverb
yum install -y autofs
vim /etc/auto.master.d/nfs.autofs
写配置文件
/rhome /etc/auto.nfs
cp /etc/auto.misc /etc/auto.nfs
vim /etc/auto.nfs
写配置文件
lily -fstype=nfs,rw,sync 192.168.122.100:/rhome/lily
systemctl enable autofs --now
systemctl restart autofs
验证
df -TH
su - lily
touch test.txt
练习题9×××
配置 /var/tmp/fstab
权限
在 mars.lab.example.com
上执行
将文件 /etc/fstab
复制到 /var/tmp/fstab
。
配置 /var/tmp/fstab
的权限以满足如下条件:
- 文件
/var/tmp/fstab
由root
用户所有 - 文件
/var/tmp/fstab
属于组root
- 文件
/var/tmp/fstab
应不能被任何人执行 - 用户
lucy
能够读取和写入/var/tmp/fstab
- 用户
harry
无法写入或读取/var/tmp/fstab
- 所有其他用户( 当前或未来) 能够读取
/var/tmp/fstab
作答:
cp /etc/fstab /var/tmp/fstab
setfacl -m u:lucy:rw,u:harry:- /var/tmp/fstab
检查
getfacl /var/tmp/fstab
练习题10
配置用户帐户
在 mars.lab.example.com
上执行
配置用户 jacks
, 用户 ID 为 3533
。
此用户的密码应当为 redhat
。
作答:
useradd jacks -u 3533
echo redhat | passwd --stdin jacks
检查
id jacks
练习题11
查找文件
在 mars.lab.example.com
上执行
查找用户 querys
所有的文件并将其副本放入 /root/findfiles
目录。
作答:
mkdir /root/findfiles
find / -user querys -type f -exec cp {} /root/findfiles/ \;
练习题12
查找字符串
在 mars.lab.example.com
上执行
- 查找文件
/usr/share/doc/words/readme.txt
中包含字符串crosswords
的所有行。 - 将所有这些行的副本按原始顺序放在文件
/root/list
中。 /root/list
不得包含空行, 且所有行必须是/usr/share/doc/words/readme.txt
中原始行的确切副本
作答:
grep crosswords /usr/share/doc/words/readme.txt > /root/list
练习题13
创建存档
在 mars.lab.example.com
上执行
创建一个名为 /root/backup.tar.gz
的tar
存档, 其应包含 /usr/local
的内容。
该 tar
存档必须使用 gzip
进行压缩
作答:
tar zcvf /root/backup.tar.gz /usr/local
练习题14
脚本考试题1:
练习题一:
在 /root
下新建名为 newsearch
的脚本,用于定位与 /usr
下的文件
-
脚本
newsearch
应定位到/usr
下面所有大于 30K 且小于50K 并有设置用户SUID特殊权限位的文 件。 -
执行时,脚本
newsearch
应将查找到的文件列表保存到/root/myoutput.txt
中
作答:
vim /root/newsearch
编写脚本
#! /bin/bash
find /usr -type f -size +30k -size -50k -perm /u=s -exec ls {} > /root/myoutput.txt \;
执行
chmod +x /root/newsearch
./newsearch
检查
cat /root/myoutput.txt
练习题15
脚本考试题2:
-
在
/usr/bin
目录下创建一个repwis
脚本,查找/usr/
目录下小于10M并且组 ID不为root
的文件; -
将查找到的文件放在
/root/myoutput
文件夹内。
作答:
vim /usr/bin/repwis
编写脚本
#! /bin/bash
mkdir -p /root/myoutput
find /usr/ -type f -size -10M -not -group root -exec cp {} /root/myoutput/ \;
执行
chmod +x /usr/bin/repwis
repwis
检查
ll /root/myoutput
练习题16
容器数据卷挂载,通过以下方式扩展上一个任务
- 配置
systemd-journald
服务,要求永久保留日志数据 - 将
/var/log/journal
目录及任何子目录中的任何*.journal
文件复制 到/home/contsvc/container_journal
中
作答:
查看日志
ll /run/log/journal
日志持久化
vim /etc/systemd/journald.conf
# 添加
Storage=persistent
systemctl list-units | grep jour
# 找到
systemd-journald.service
systemctl restart systemd-journald.service
检查
ll /var/log/journal
更改所属组
grep jour /etc/group
# 找到
systemd-journal:x:190:
chgrp -R systemd-journal /var/log/journal
复制文件
mkdir /home/contsvc/container_journal
chown contsvc:contsvc /home/contsvc/container_journal
find /var/log/journal --type f --name *.journal --exec cp {} /home/contsvc/container_journal/ \;
练习题17
新增容器题目。
容器题目考试时必须先明确版本是否高于 1.6(存在版本坑会导致容器 0 分),若不高于 1.6,一定要先进行版本升级。
使用容器
-
利用注册表服务器上的
httpd
镜像,创建名为httpserver
的容器 -
容器服务启动时能自动将
/home/contsvc/container_journal
挂载到容器中 的/var/log/journal
上 -
可能存在已经配置的容器仓库模版它位于
/tmp/containers-services/registries.conf
,仓库 地址location = "f0pqsi2y.mirror.aliyuncs.com
" -
将其配置为以
systemd
服务的形式运行,且仅面向现有用户contsvc
该服务应命名为container-httpserver
,此服务在系统重启后将自动启动
作答:
新建用户
useradd contsvc
echo redhat | passwd --stdin contsvc
安装/升级 podman
yum module install -y container-tools
yum update -y podman
查看配置文件存放位置
man containers-registries.conf
# 找到存放位置
Container engines will use the $HOME/.config/containers/registries.conf if it exists, otherwise they will use /etc/containers/registries.conf
查看配置文件格式
vim /etc/containers/registries.conf
创建配置文件
su - contsvc
mkdir .config/containers/
vim .config/containers/registries.conf
编写配置文件
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
insecure = false
blocked = false
location = "f0pqsi2y.mirror.aliyuncs.com"
运行容器
podman search httpd
# 找到位置
docker.io/library/httpd
podman run -d --name httpserver -v /home/contsvc/container_journal:/var/log/journal docker.io/library/httpd
查看/停止/删除容器
podman ps
podman stop httpserver
podman rm httpserver
查看 systemd 服务存放位置
man systemd.unit
# 找到位置
~/.config/systemd/user/*
创建配置文件
mkdir -p ~/.config/systemd/user
cd ~/.config/systemd/user
podman generate systemd --files --name httpserver --new
设置开机自启
ssh contsvc@mars
systemctl --user daemon-reload
systemctl --user enable container-httpserver --now
检查容器
podman ps
systemctl --user status container-httpserver
配置 linger 使用户退出时,服务不会退出
loginctl enable linger
loginctl show-user contsvc
# 看到
Linger=yes
练习题18
小题型改动:
在容器上运行命令:logger -p local5.info "This is an RHCSA exam"
时,容器上 的 /var/log/journal/
和容器主机上的 /home/contsvc/container_journal/
中均应显示消息。
提示:操作有两种形式,一是进去容器里进行操作,二是直接使用容器执行命令的形式。
作答:
podman exec httpserver logger -p local5.info "This is an RHCSA exam"
练习题19
新增 sudo
权限设置
为用户 student
添加 sudo
权限,不需要密码
用户 devops
附属于 sudo
权限组
作答:
vim /etc/sudoers.d/student
# 添加配置
student ALL=(ALL) NOPASSWD:ALL
useradd devops -G wheel
echo redhat | passwd --stdin devops
练习题20
umask练习
配置用户 devops
权限要求如下:
用户 devops
创建得所有文件得默认权限是 -rw-r-----
同一用户下创建所有文件目录权限为 drwxr-x---
作答:
su - devops
vim .bash_profile
# 添加
umask 027
source .bash_profile
检查
touch test
mkdir testdir
ll
练习题21
- 配置创建新用户的密码策略
- 创建新用户时,默认密码策略为20天后密码过期
作答:
vim /etc/login.defs
# 找到密码天数并修改
PASS_MAX_DAYS 99999 -> PASS_MAX_DAYS 20
练习题22
配置用户 student 登录时,提示"welcome.."
作答:
su - student
vim .bash_profile
# 添加
echo "welcome.."
source .bash_profile
检查
su - student
练习题23
创建一个名ex200的应用,student用户执行应用时,就会输出一句话(练习时随便写一句吧,考试时题目有指定的)。
作答:
mkdir ~/bin
vim /bin/ex200
编写脚本
#! /bin/bash
echo hello
执行
chmod +x /bin/ex200
ex200
练习题24×××
设置 root
密码
在 venus.lab.example.com
上执行
将 venus
的 root
密码设置为 redhat
。
您需要获得系统访问权限才能进行此操作
作答:
重启 venus,选择内核处按 e
,在倒数第二行以 linux
开头的行末尾添加 rd.break console=tty0
,ctrl+x 保存退出
进入系统
mount -o rw,remount /sysroot # 重新挂载/sysroot为可读写模式,并切换根目录为/sysroot
chroot /sysroot
echo redhat | passwd --stdin root
touch /.autorelabel # 更新selinux上下文
exit
exit # 重启
练习题25
配置您的系统以使用默认存储库
在 venus.lab.example.com
上执行
yum
存储库已可以从http://mirrors.aliyun.com/centosvault/8.5.2111/BaseOS/$basearch/os/
和 http://mirrors.aliyun.com/centosvault/8.5.2111/AppStream/$basearch/os/
使用配置您的系统,以将这些位置用作默认存储库。
作答:
同练习题2
练习题26×××
调整逻辑卷大小
在 venus.lab.example.com
上执行
将逻辑卷 lilylv
及其文件系统的大小调整到230MiB。确保文件系统内容保持不变。
注: 分区大小很少与请求的大小完全相同, 因此可以接受范围为 217MiB 到 243MiB 的大小。
作答:
lvextend -rL 230M /dev/mapper/lilyvg-lilylv
练习题27×××
添加交换分区
在 venus.lab.example.com
上执行
向您的系统添加一个额外的交换分区 756MiB 交换分区应在系统启动时自动挂载
不要删除或以任何方式改动系统上的任何现有交换分区
作答:
分区
fdisk /dev/sdb
# 分区
n
+756M
t
82
w
格式化交换分区
mkswap /dev/sdb2
查看 UUID
blkid
写入 /etc/fstab
echo "UUID=f24d617d-cf5d-48df-ac21-e3527b241ef0 none swap defaults 0 0" >> /etc/fstab
激活交换分区
swapon -a
检查
swapon -s
练习题28×××
创建逻辑卷
在 venus.lab.example.com
上执行
根据如下要求, 创建新的逻辑卷:
- 逻辑卷取名为
qa
,属于qagroup
卷组, 大小为60个扩展块 qagroup
卷组中逻辑卷的扩展块大小应当为16MiB- 使用
ext3
文件系统格式化新逻辑卷。 - 该逻辑卷应在系统启动时自动挂载到
/mnt/qa
下
作答:
分区
fdisk /dev/sdb
# 分区
n
+1G
t
8e
w
创建物理卷/卷组/逻辑卷
pvcreate /dev/sdb3
vgcreate qagroup -s 16m /dev/sdb3
lvcreate -n qa -l 60 qagroup
格式化
mkfs.ext3 /dev/mapper/qagroup-qa
挂载
mkdir -p /mnt/qa
blkid
# 找到UUID
echo "UUID=e4ce8964-2167-417d-95c6-0afd072f7b7e /mnt/qa ext3 defaults 0 0" >> /etc/fstab
mount -a
检查
df -TH
lsblk
练习题29×××
创建 VDO 卷
在 venus.lab.example.com
上执行
根据如下要求, 创建新的 VDO 卷:
使用未分区的磁盘
该卷的名称为 vdough
该卷的逻辑大小为 50G
该卷使用 xfs
文件系统格式化
该卷( 在系统启动时)挂载到 /vbread
下
作答:
下载 vdo
yum provides vdo
yum install -y vdo
查看 vod 创建方法
man vod
# /EXAMPLE 找到
vdo create --name=vdo0 --device=/dev/sdb1 --vdoLogicalSize=10T
创建 vdo 卷
vdo create --name=vdough --device=/dev/sdc --vdoLogicalSize=50G
格式化卷
mkfs.xfs /dev/mapper/vdough
挂载
mkdir /bread
man vdo
# /\/dev 找到
/dev/mapper/vdo0 /vdo xfs defaults,x-systemd.requires=vdo.service 0 0
echo "/dev/mapper/sdc /vbread xfs defaults,x-systemd.requires=vdo.service 0 0" >> /etc/fstab
mount -a
检查
df -TH
lsblk
练习题30×××
配置系统调优
在 venus.lab.example.com
上执行
为您的系统选择建议的 tuned
配置集并将它设为默认设置
作答:
tuned-adm recommend
tuned-adm profile vitural-guest
检查
tuned-adm active