二、RHCSA試題解析

Federico發表於2019-01-03

一、設定YUM倉庫

YUM的軟體庫源地址為:http://content.example.com/rhel7.0/x86_64/dvd,將此配置為作業系統的預設軟體倉庫。

方法一(修改配置檔案):

vim /etc/yum.repos.d/yum.repo

[base]
name=RHCSA
baseurl=http://content.example.com/rhel7.0/x86_64/dvd
enable=1
gpgcheck=0

  建立yum源需要主機配置檔案必須存放在/etc/yum.repos.d下,並且以.repo結尾。

name:定義源名稱
baseurl:定義源地址
enable:定義是否啟用
gpgcheck:定義是否檢查軟體簽名,如果設定為1,需要設定:gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
baseurl:支援使用本地路徑、FTP、http三種資源格式:

baseurl=file:///
baseurl=ftp://
baseurl=http://

  本地路徑位置中第三個/表示本地根目錄,與上述gpgkey本地路徑格式一致。

方法二(使用命令列進行新增):

yum-config-manager -add http://content.example.com/rhel7.0/x86_64/dvd
  如果當前系統沒有yum-config-manager命令需要安裝yum-utils軟體包。

二、調整邏輯卷大小

將邏輯卷/dev/VolGroup/lv_home和其檔案系統大小調整到15G,要確保檔案系統中的內容保持完整。

檢視當前邏輯卷大小

[root@serverX ~]# lvscan
  ACTIVE            '/dev/VolGroup/lv_swap' [<1.97 GiB] inherit
  ACTIVE            '/dev/VolGroup/lv_root' [50.00 GiB] inherit
  ACTIVE            '/dev/VolGroup/lv_home' [11.48 GiB] inherit //lv_home大小為11.48G

  使用lvscan命令可以檢視出當前LV/dev/VolGroup/lv_home大小為11.48G。

檢視組卷大小

[root@serverX ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <63.51 GiB
  PE Size               4.00 MiB
  Total PE              16258
  Alloc PE / Size       16243 / <63.45 GiB
  Free  PE / Size       15 / 60.00 MiB //硬碟可供指派給邏輯卷的單位
  VG UUID               dcN4dI-xFex-69UJ-6L1d-b4b4-9BEZ-YdPD6f

  檢視當前硬碟可供指派給邏輯卷的最小單位已經不足以使用,所以需要對當前VG進行擴充套件,在此我們做一個分割槽用於擴充套件VG。

新增所需分割槽

parted /dev/sdb
(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? ext4
Start? 1
End? 5GiB
(parted) print
Model: ATA CentOS Linux-1 (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  5369MB  5368MB  primary

將新新增的分割槽加入邏輯卷中

[root@serverX ~]# vgextend VolGroup /dev/sdb1 //將sdb1分割槽加入卷組
  Physical volume "/dev/sdb1" successfully created.
  Volume group "VolGroup" successfully extended
[root@serverX ~]# lvextend -L 15GiB /dev/VolGroup/lv_home //擴充套件邏輯卷至15GiB
  Size of logical volume VolGroup/lv_home changed from 11.48 GiB (2939 extents) to 15.00 GiB (3840 extents).
  Logical volume VolGroup/lv_home successfully resized.
[root@serverX ~]# resize2fs /dev/VolGroup/lv_home //更新邏輯卷
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/VolGroup/lv_home is mounted on /home; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/VolGroup/lv_home is now 3932160 blocks long.

[root@serverX ~]# lvscan //重新掃描確認結果
  ACTIVE            '/dev/VolGroup/lv_swap' [<1.97 GiB] inherit
  ACTIVE            '/dev/VolGroup/lv_root' [50.00 GiB] inherit
  ACTIVE            '/dev/VolGroup/lv_home' [15.00 GiB] inherit

三、建立使用者組

建立下列使用者、組以及和組的成員關係:

  • 一個名為adminuser的組;
  • 一個名為natasha的使用者,其屬於adminuser,這個組是該使用者的從屬組;
  • 一個名為harry的使用者,屬於adminuser,這個組是該使用者的從屬組;
  • 一個名為sarah的使用者,其在系統中沒有可互動的shell,並且不是adminuser組的成員;
  • natasha、harry、sarah的密碼都要設定為flectrag。
[root@serverX ~]# groupadd adminuser
[root@serverX ~]# useradd -G adminuser natasha //-G指定使用者所屬的從屬組
[root@serverX ~]# useradd -G adminuser harry
[root@serverX ~]# useradd -s /sbin/nologin sarah //-s 指定登入shell,/sbin/nologin表示不能登入系統
[root@serverX ~]# echo flectrag|passwd --stdin natasha
Changing password for user natasha.
passwd: all authentication tokens updated successfully.
[root@serverX ~]# echo flectrag|passwd --stdin harry
Changing password for user harry.
passwd: all authentication tokens updated successfully.
[root@serverX ~]# echo flectrag|passwd --stdin sarah
Changing password for user sarah.
passwd: all authentication tokens updated successfully.

四、使用訪問控制列表配置檔案許可權

拷貝檔案/etc/fstab到/var/tmp/fstab,配置檔案/var/tmp/fstab的許可權:

  • 檔案/var/tmp/fstab的擁有者是root;
  • 檔案/var/tmp/fstab輸入root組;
  • 檔案/var/tmp/fstab對任何人都不可執行;
  • 使用者natasha能夠對檔案/var/tmp/fstab執行讀和寫操作;
  • 使用者harry對檔案/var/tmp/fstab既不能讀,也不能寫;
  • 所有其他使用者(當前的和將來的)能夠對檔案/var/tmp/fstab進行讀操作。
[root@serverX ~]# cp /etc/fstab /var/tmp/
[root@serverX ~]# setfacl -m u:natasha:rw /var/tmp/fstab
[root@serverX ~]# setfacl -m u:harry:- /var/tmp/fstab

五、配置一個crontab任務

為使用者natasha配置一個定時任務,每天在本地時間14:23時執行以下命令:
bin/echo hiya

[root@serverX ~]#crontab -e -u natasha
23 14 * * * /bin/echo hiya

  計劃任務時間格式為分時日月周,後面直接加要執行的命令即可。
crontab -e -u user:為指定使用者編輯計劃任務
crontab -l:檢視當前使用者所有的計劃任務
crontab -r:清除計劃任務

特殊字元 代表意義
* 代表任何時刻,例如分鐘為*,代表每分鐘都執行
, 代表分割時段,例如小時為3,5表示3時和5時執行此計劃任務
- 代表一個時間段範圍,例如日配置為1-10,表示相應月的1-10號執行此計劃任務
/n n表示數字,如果分鐘為*/5,表示每五分鐘執行一次計劃任務

六、建立一個多使用者使用目錄

建立一個多使用者共同使用的目錄/home/admins,特性如下:

  • /home/admins目錄的組所有權是adminuser;
  • adminuser組的成員對目錄有讀寫和執行的許可權。除此之外的其他所有使用者沒有任何許可權(root除外);
  • 在/home/admins目錄中建立的檔案,其組所有權會自動設定為屬於adminuser組。
[root@serverX ~]# mkdir /home/admins
[root@serverX ~]# chown :adminuser /home/admins/ //設定目錄所屬組為adminuser
[root@serverX ~]# chmod 2770 /home/admins //2770特殊許可權為2表示為SGID,使用者在此目錄下的群組將會變成該目錄的群組,770表示檔案的屬主屬組擁有讀寫執行許可權。
[root@serverX ~]# ll /home/
total 0
drwxrws---. 2 root     adminuser  6 Jan  3 17:28 admins

七、升級作業系統核心

新版本核心檔案可以從http://rhgls.domainX.example.com/pub/updates/獲取。升級作業系統核心,同事需要滿足下列要求:

  • 當系統重新啟動之後升級的核心要作為預設的核心;
  • 原來的核心要被保留,並且仍然可以正常啟動。
[root@serverX ~]# uname -r
3.10.0-693.el7.x86_64
[root@serverX ~]# wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm
--2019-01-03 18:08:33--  https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm
Resolving elrepo.org (elrepo.org)... 69.195.83.87
Connecting to elrepo.org (elrepo.org)|69.195.83.87|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 47850944 (46M) [application/x-rpm]
Saving to: ‘kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm’

100%[===================================================================================================================>] 47,850,944   603KB/s   in 79s

2019-01-03 18:09:53 (589 KB/s) - ‘kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm’ saved [47850944/47850944]
[root@serverX ~]# rpm -ivh kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm
warning: kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID baadae52: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:kernel-ml-4.19.12-1.el7.elrepo   ################################# [100%]
[root@serverX ~]# cat /boot/grub2/grub.cfg |grep menuentry //過濾啟動項,檢視grub配置檔案中是否存在安裝後的核心啟動引數。
if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
  menuentry_id_option=""
export menuentry_id_option
menuentry 'CentOS Linux (4.19.12-1.el7.elrepo.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-693.el7.x86_64-advanced-da5515d9-ffb3-4589-99c5-699b3fcb547f' {
menuentry 'CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-693.el7.x86_64-advanced-da5515d9-ffb3-4589-99c5-699b3fcb547f' {
menuentry 'CentOS Linux (0-rescue-529c8dcd6c464addae7654f5958443c6) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-529c8dcd6c464addae7654f5958443c6-advanced-da5515d9-ffb3-4589-99c5-699b3fcb547f' {
[root@serverX ~]#grub2-set-default 'CentOS Linux (4.19.12-1.el7.elrepo.x86_64) 7 (Core)' //將預設啟動核心設定為新安裝核心
[root@serverX ~]#grub2-editenv list //確認是否設定成功
saved_entry=CentOS Linux (4.19.12-1.el7.elrepo.x86_64) 7 (Core)
[root@serverX ~]#reboot 
[root@serverX ~]#uname -r
4.19.12-1.el7.elrepo.x86_64

八、使用LDAP進行使用者集中認證

系統host.domainX.example.com提供了一個LDAP驗證服務。將系統按照以下要求繫結到這個服務上:

  • 驗證伺服器的基本 DN 是:dc=domainX,dc=example,dc=com;
  • 帳戶資訊和驗證資訊都是由 LDAP 提供的;
  • 連線要使用證照進行加密,證照可以在下面的連結中下載 :ftp://host.domainX.example.com/pub/domainX.crt;
  • 當正確完成配置後,使用者 ldapuserX 應該能夠登入到您的系統中,但是沒有主目錄。 當您完成 autofs 的題目之後,才能生成主目錄;
  • 使用者 ldapuserX 的密碼是 password。
[root@serverX ~]# yum -y install sssd
[root@serverX ~]# authconfig-tui

二、RHCSA試題解析
二、RHCSA試題解析

[root@serverX ~]# cd /etc/openldap/certs/
[root@serverX certs]# wget ftp://host.domainx.example.com/pub/domainX.crt
[root@serverX certs]# systemctl restart sssd
[root@serverX certs]# id ldapuserX
uid=17X(ldapuserX) gid=17X(ldapuserX) groups=17X(ldapuserX)

九、autofs配置

按照以下要求配置autofs用來自動掛載LDAP使用者的家目錄:

  • host.domainX.example.com(172.24.10.250)通過 NFS v3 版本輸出 /rhome 目錄到您的系統,這個檔案系統包含了使用者 ldapuserX 的主目錄,並且已經預先配置好;
  • ldapuserX 使用者的家目錄是 host.domainX.example.com:/rhome/ldapuserX;
  • ldapuserX 的家目錄應該掛載到本地的/rhome/ldapuserX 目錄下;
  • 使用者對其家目錄是可寫的;
  • ldapuserX 使用者的密碼是 password。
[root@serverX ~]# yum -y install autofs
[root@serverX ~]# mkdir /rhome/ldapuserX -p
[root@serverX ~]# vim /etc/auto.master //指定監控點
/rhome/ldapuserX /etc/guests.rule
[root@serverX ~]# vim /etc/guests.rule
ldapuserX -rw,v3 host.domainX.example.com:/rhome/ldapuserX //通過NFS V3版本以可讀寫方式進行掛載
[root@serverX ~]# systemctl start autofs
[root@serverX ~]# systemctl enable autofs
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.
[root@serverX ~]# su - ldapuserX -c 'pwd'
/rhome/ldapuserX

十、配置NTP客戶端

配置作業系統,讓其作為ntp1.aliyun.com的NTP客戶端。

[root@serverX ~]# yum -y install chrony
[root@serverX ~]# vim /etc/chrony.conf
server ntp1.aliyun.com iburst //註釋原有預設NTP地址
[root@serverX ~]# systemctl restart chronyd
[root@serverX ~]# timedatectl
      Local time: Fri 2019-01-04 14:39:02 CST
  Universal time: Fri 2019-01-04 06:39:02 UTC
        RTC time: Fri 2019-01-04 06:39:02
       Time zone: America/New_York (CST, +0800)
     NTP enabled: yes //檢視NTP是否啟用
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

十一、配置一個使用者賬戶

建立一個名為alex的使用者,使用者ID為3456。密碼是flectrag

[root@serverX ~]# useradd -u 3456 alex
[root@serverX ~]# echo flectrag | passwd --stdin alex
Changing password for user alex.
passwd: all authentication tokens updated successfully.

十二、新增一個swap分割槽

在作業系統中新增一個大小為512MiB的swap分割槽,詳細資訊如下:

  • 當作業系統啟動時,swap分割槽可以自動掛載;
  • 不要移除或修改當前系統中其他的swap分割槽。
[root@serverX ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x001379c8.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +512M
Partition 1 of type Linux and of size 512 MiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@serverX ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=05291f58-7136-436b-b5ed-28df93547bbc
[root@serverX ~]# vim /etc/fstab
/dev/sdb1 swap swap defaults 0 0
[root@serverX ~]# swapon -a
[root@serverX ~]# swapon -s //檢視當前交換分割槽啟動情況
Filename                Type        Size    Used    Priority
/dev/dm-1                               partition   2097148 0   -2
/dev/sdb1                               partition   524284  0   -3

十三、查詢檔案

找出所有使用者student擁有的檔案,並且把它們拷貝到/root/findfiles目錄中。

mkdir /root/findfiles ; find / -user student -type f |xargs -i cp {} /root/findfiles/

十四、查詢一個字串

在檔案/usr/share/dict/words 中查詢到所有包含字串 seismic 的行:

  • 將找出的行按照原文的先後順序拷貝到/root/wordlist 檔案中;
  • /root/wordlist 檔案不要包含空行,並且其中的所有行的內容都必須是/usr/share/dict/words 檔案中原始行的準確副本。
[root@serverX ~]# grep 'seismic' /usr/share/dict/words > /root/wordlist

十五、建立一個邏輯卷

根據下面的要求建立一個新的邏輯卷:

  • 邏輯卷命名為 database,屬於 datastore 卷組,並且邏輯卷的大小為 50 個物理擴充套件單元 (physical extent);
  • 在 datastore 卷組中的邏輯卷,物理擴充套件單元 (physical extent) 大小應為 16 MiB;
  • 使用 ext3 檔案系統對新的邏輯捲進行格式化,此邏輯卷應該在系統啟動的時候自動掛載在 /mnt/database 目錄下。
[root@serverX ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2):
First sector (1050624-209715199, default 1050624):
Using default value 1050624
Last sector, +sectors or +size{K,M,G} (1050624-209715199, default 209715199): +1G //因為題目要求PE大小為16MiB,邏輯卷大小為50PE,所以用於做此邏輯卷的分割槽必須大於50x16,free PE數小於50會導致建立失敗。
Partition 2 of type Linux and of size 50 MiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@serverX ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x001379c8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1050623      524288   83  Linux
/dev/sdb2         1050624     1153023       51200   83  Linux
[root@serverX ~]# partprobe /dev/sdb ;sync
[root@serverX ~]# vgcreate -s 16MiB datastore /dev/sdb2
  Physical volume "/dev/sdb2" successfully created.
  Volume group "datastore" successfully created
[root@serverX ~]# lvcreate -l 50 -n database datastore
  Logical volume "database" created.
[root@serverX ~]# mkfs.ext3 /dev/datastore/database
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
51296 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=209715200
7 block groups
32768 blocks per group, 32768 fragments per group
7328 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

[root@serverX ~]# mkdir /mnt/database
[root@serverX ~]# vim /etc/fstab
/dev/datastore/database /mnt/database ext3 defaults 0 0
[root@serverX ~]# mount -a 

十六、建立一個歸檔檔案

建立一個名為 /root/backup.tar.bz2 的歸檔檔案,其中包含 /usr/local 目錄中的內容,tar 歸檔必須使用 bzip2 進行壓縮。

tar jcf /root/backup.tar.bz2 /usr/local/

相關文章