配置RHCSA+RHCE的考场环境

张启昊
张启昊
发布于 2024-06-07 / 65 阅读
0
0

配置RHCSA+RHCE的考场环境

配置RHCSA+RHCE考场环境

作者:欧灿荣
邮箱:13825091606@139.com
更新日期:2023年04月07号

1. 安装3台Linux

使用Rocky Linux 8版本。Workstation代表电脑的主系统,要求安装图形界面。另外2台,只需要命令行界面。每台只需要1张网卡,NAT模式,即可连接外网。

本教程使用Rocky Linux做演示,官网地址如下,下载8.x的Minimal版本,目前考试还是8。

https://rockylinux.org

Rocky Linux官网

2. 安装后进行初始化

2.1 测试网络

测试网络:

ping www.qq.com

现在应该是可以ping通外网的。如果不能ping通,先解决网络问题。典型的故障有:

  • 你的电脑网络本身有问题。请排查。
  • 安装系统的时候,忘记设置网络开机自启动了?

修复方法如下:

nmcli con mod 网卡名称 connection.autoconnect yes
nmcli con up 网卡名称

网卡名称通过以下命令查询:

nmcli c show

或者

ip addr

举例:

nmcli con mod ens160 connection.autoconnect yes
nmcli con up ens160

2.2 安装EPEL源和VIM编辑器

把Rocky Linux自带的仓库地址替换为阿里云:

sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' -i.bak /etc/yum.repos.d/[rR]ocky*.repo
yum -y install epel-release vim

把EPEL仓库地址替换为阿里云:

sed -e 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' -e 's|^metalink|#metalink|' -i.bak /etc/yum.repos.d/epel*

把系统升级到最新:

yum -y update

更新完后,关机

shutdown now

然后把刚才的虚拟机复制成多份,在VMware虚拟机的文件夹里找到刚刚的虚拟机文件夹,复制多份出来重命名就行。比如workstation,servera,serverb,serverc,serverd。共5个文件夹。

2.3 设置VMware DHCP的网段

为了匹配我做环境,你需要把VMware里NAT网络的DHCP网段修改为192.168.122.0,这个改动仅仅影响使用NAT网络且使用DHCP获取地址的虚拟机。

设置VMware DHCP的网段

如果你有其它正在使用NAT网络的虚拟机,把虚拟机的IP段手动修改为122网段或者设置为DHCP后重启虚拟机网络即可继续连接外网。

然后把所有的虚拟机打开。

2.4 定制hosts文件

为了简化域名解析的配置,直接修改/etc/hosts文件。相当于静态绑定IP地址与域名之间的关系。

在workstation上执行以下命令:

vim /etc/hosts

添加以下内容:

192.168.122.101 servera.lab.example.com servera
192.168.122.102 serverb.lab.example.com serverb

servera,serverb,serverc,serverd为受控端,可以不写。当然,如果servera想通过域名访问serverb,那就要在servera上修改/etc/hosts文件,把serverb的IP和域名绑定起来。

RHCSA只有servera和serverb.
RHCE有servera,serverb,serverc,serverd

2.5 修改IP与主机名

按以下要求为各系统修改IP地址和主机名:

系统IP地址
workstation.lab.example.com192.168.122.100
mars.lab.example.com192.168.122.101
venus.lab.example.com192.168.122.102
  • 网关IP: 打开VMware-编辑-虚拟网络编辑器-NAT模式-NAT设置找到网关地址
  • DNS名称服务器IP: 223.5.5.5(阿里云的公网DNS服务器地址)

配置网卡,xxx是网卡标识号,使用nmcli c show查看网卡标识号:

替换掉下面命令行中的xxx:

nmcli con mod xxx ipv4.method manual ipv4.addr "192.168.122.100/24" ipv4.gateway "网关IP" ipv4.dns "223.5.5.5" connection.autoconnect yes

重加加载网卡配置:

nmcli con up xxx

设置主机名:

workstation:

hostnamectl set-hostname workstation.lab.example.com

servera:

hostnamectl set-hostname servera.lab.example.com

serverb:

hostnamectl set-hostname serverb.lab.example.com

2.6 Workstation安装图形界面

在Workstation主机上执行以下命令,安装图形界面:

dnf groupinstall workstation -y
systemctl set-default graphical.target
systemctl isolate graphical.target

Workstation就是用来模拟考试时台式机的系统
servera和serverb不需要安装图形界面

3. 登录workstation的图形界面

注意:考试时,节点名称一般node1,node2,而不是servera和serverb,对号入座就行,根据题目做题。

第1次进入图形界面,会要求新建一个账号,填写student即可,密码使用redhat就可以了。

3.1 配置ssh免密码登录

改为ssh公钥认证

先在Workstation的student用户上,产生一对密钥:

ssh-keygen -t RSA

一路回车

查看公钥和私钥文件:

ll ~/.ssh

在Workstation上,把student用户的公钥传给servera和serverb,必须要student用户上操作:

ssh-copy-id root@servera
ssh-copy-id root@serverb

3.2 在WorkStation桌面上打开Terminal终端

2个窗口,servera(root用户),serverb(root用户)

参考命令如下:

窗口1:

ssh root@servera

窗口2:

ssh root@serverb

现在你应该可以在Workstation的student用户上免密登录servera和serverb了。

3.3 无法ssh怎么办?

如果发现无法ssh或ping通某台服务器,那就要到控制台打开服务器显示器,检查sshd服务

检查ssh状态:

systemctl status sshd

开启ssh:

systemctl enable sshd --now

3.4 练习形式

方式一:日常练习模式
Windows+xshell+练习题

方式二:(考试模式)
Workstation的图形界面+Terminal+Firefox打开练习题(可以上传到有道云笔记或者其它在线笔记平台)

2种方式按自己的意愿选择。

4. 搭建RHCE环境

根据以上经验,按要求搭建RHCE实验环境。

考试节点练习节点IP地址
ansible.lab.example.comworkstation.lab.example.com192.168.122.200
node1.lab.example.comservera.lab.example.com192.168.122.201
node2.lab.example.comserverb.lab.example.com192.168.122.202
node3.lab.example.comserverc.lab.example.com192.168.122.203
node4.lab.example.comserverd.lab.example.com192.168.122.204

workstation可以共用,关键做好IP地址的切换。自己写一个脚本吧。

5. 制作RHCSA和RHCE的切换脚本

5.1 制作RHCSA的切换脚本

su -
vim ~/reset_rhcsa_workstation.sh

添加以下内容,把xxx替换成你的网卡标识符。

#!/bin/bash
nmcli con mod xxx ipv4.method manual ipv4.addr "192.168.122.100/24" ipv4.gateway "192.168.122.1" ipv4.dns "223.5.5.5" connection.autoconnect yes
nmcli con up xxx
cat <<EOF > /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.101 servera.lab.example.com servera
192.168.122.102 serverb.lab.example.com serverb
EOF
rm -f /root/.ssh/known_hosts
rm -f /home/student/.ssh/known_hosts
rm -f /home/ansible/.ssh/known_hosts
useradd student
echo "redhat" |passwd --stdin student
useradd ansible
echo "redhat" |passwd --stdin ansible

添加执行权限:

chmod +x ~/reset_rhcsa_workstation.sh

5.2 制作RHCE的切换脚本

su -
vim ~/reset_rhce_workstation.sh

添加以下内容,把xxx替换成你的网卡标识符。

#!/bin/bash
nmcli con mod xxx ipv4.method manual ipv4.addr "192.168.122.200/24" ipv4.gateway "192.168.122.1" ipv4.dns "223.5.5.5" connection.autoconnect yes
nmcli con up xxx
cat <<EOF > /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.201 servera.lab.example.com servera
192.168.122.202 serverb.lab.example.com serverb
192.168.122.203 serverc.lab.example.com serverc
192.168.122.204 serverd.lab.example.com serverd
EOF
rm -f /root/.ssh/known_hosts
rm -f /home/student/.ssh/known_hosts
rm -f /home/ansible/.ssh/known_hosts
useradd student
echo "redhat" |passwd --stdin student
useradd ansible
echo "redhat" |passwd --stdin ansible

添加执行权限:

chmod +x ~/reset_rhce_workstation.sh

5.3 切换环境

切换成RHCSA的Workstation

su -
./reset_rhcsa_workstation.sh

切换成RHCE的Workstation

su -
./reset_rhce_workstation.sh

5.4 思考

既然workstation能共用,那么rhcsa与rhce的servera与serverb是否能共用呢?

6. RHCSA与RHCE共用servera和serverb

6.1 servera的2个参考脚本

rhcsa_servera:

su -
vim ~/reset_rhcsa_servera.sh
#!/bin/bash
hostnamectl set-hostname servera.lab.example.com
nmcli con mod xxx ipv4.method manual ipv4.addr "192.168.122.101/24" ipv4.gateway "192.168.122.1" ipv4.dns "223.5.5.5" connection.autoconnect yes
nmcli con up xxx
cat <<EOF > /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.102 serverb.lab.example.com serverb
EOF
rm -f /root/.ssh/known_hosts
rm -f /home/student/.ssh/known_hosts
rm -f /home/ansible/.ssh/known_hosts
useradd student
echo "redhat" |passwd --stdin student
useradd ansible
echo "redhat" |passwd --stdin ansible

rhce_servera:

su -
vim ~/reset_rhce_servera.sh
#!/bin/bash
hostnamectl set-hostname servera.lab.example.com
nmcli con mod xxx ipv4.method manual ipv4.addr "192.168.122.201/24" ipv4.gateway "192.168.122.1" ipv4.dns "223.5.5.5" connection.autoconnect yes
nmcli con up xxx
cat <<EOF > /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.202 serverb.lab.example.com serverb
192.168.122.203 serverc.lab.example.com serverc
192.168.122.204 serverd.lab.example.com serverd
EOF
rm -f /root/.ssh/known_hosts
rm -f /home/student/.ssh/known_hosts
rm -f /home/ansible/.ssh/known_hosts
useradd student
echo "redhat" |passwd --stdin student
useradd ansible
echo "redhat" |passwd --stdin ansible

servera切换到rhcsa环境:

~/reset_rhcsa_servera.sh

servera切换到rhce环境:

~/reset_rhce_servera.sh

6.2 serverb的2个参考脚本

rhcsa_serverb:

vim ~/reset_rhcsa_serverb.sh
#!/bin/bash
hostnamectl set-hostname serverb.lab.example.com
nmcli con mod xxx ipv4.method manual ipv4.addr "192.168.122.102/24" ipv4.gateway "192.168.122.1" ipv4.dns "223.5.5.5" connection.autoconnect yes
nmcli con up xxx
cat <<EOF > /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.101 servera.lab.example.com servera
EOF
rm -f /root/.ssh/known_hosts
rm -f /home/student/.ssh/known_hosts
rm -f /home/ansible/.ssh/known_hosts
useradd student
echo "redhat" |passwd --stdin student
useradd ansible
echo "redhat" |passwd --stdin ansible

rhce_serverb:

vim ~/reset_rhce_serverb.sh
#!/bin/bash
hostnamectl set-hostname serverb.lab.example.com
nmcli con mod xxx ipv4.method manual ipv4.addr "192.168.122.202/24" ipv4.gateway "192.168.122.1" ipv4.dns "223.5.5.5" connection.autoconnect yes
nmcli con up xxx
cat <<EOF > /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.201 servera.lab.example.com servera
192.168.122.203 serverc.lab.example.com serverc
192.168.122.204 serverd.lab.example.com serverd
EOF
rm -f /root/.ssh/known_hosts
rm -f /home/student/.ssh/known_hosts
rm -f /home/ansible/.ssh/known_hosts
useradd student
echo "redhat" |passwd --stdin student
useradd ansible
echo "redhat" |passwd --stdin ansible

serverb切换到rhcsa环境:

~/reset_rhcsa_serverb.sh

serverb切换到rhce环境:

~/reset_rhce_serverb.sh

RHCE需要再建2台虚拟机,分别是serverc和serverd。按视频教程安装好后,完成以下任务:

  1. 设置固定IP;
  2. 修改/etc/hosts文件,写上其它受控机的信息;
  3. 创建student和ansible用户,并设置sudo免密(所有受控机都要设置)。

评论