新建分区表1 案例1:使用Yum软件源
1.1 问题
本例要求为虚拟机 server0指定可用的Yum软件源,相关要求如下:
软件库源为 http://content.example.com/rhel7.0/x86_64/dvd
将此配置为虚拟机 server0 的默认软件仓库
1.2 步骤
实现此案例需要按照如下步骤进行。
步骤一:检查现有yum仓库,去除不可用的设置
1)列出yum库
[root@server0 ~]# yum repolist
已加载插件:langpacks
rhel_dvd | 4.1 kB 00:00:00
(1/2): rhel_dvd/group_gz | 134 kB 00:00:00
(2/2): rhel_dvd/primary_db | 3.4 MB 00:00:00
源标识 源名称 状态
rhel_dvd Remote classroom copy of dvd 4,305
repolist: 4,305
2)移除不可用的yum库配置文件
当执行yum repolist操作报错时,才执行此步骤(否则此步可跳过)。
[root@server0 ~]# mkdir /etc/yum.repos.d/repobak
[root@server0 ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repobak/
步骤二:添加指定的yum仓库配置
1)使用yum-config-manager工具建立新配置文件
[root@server0 ~]# yum-config-manager --add-repo http://content.example.com/rhel7.0/x86_64/dvd
已加载插件:langpacks
adding repo from: http://content.example.com/rhel7.0/x86_64/dvd
[content.example.com_rhel7.0_x86_64_dvd]
name=added from: http://content.example.com/rhel7.0/x86_64/dvd
baseurl=http://content.example.com/rhel7.0/x86_64/dvd
enabled=1
2)修改新建的仓库配置,添加gpgcheck=0以禁用GPG签名检查
[root@server0 ~]# vim /etc/yum.repos.d/content.example.com_rhel7.0_x86_64_dvd
[content.example.com_rhel7.0_x86_64_dvd]
name=added from: http://content.example.com/rhel7.0/x86_64/dvd
baseurl=http://content.example.com/rhel7.0/x86_64/dvd
enabled=1
gpgcheck=0
步骤三:确认新配置的yum源可用
[root@server0 ~]# yum clean all //清理缓存
已加载插件:langpacks
正在清理软件源: content.example.com_rhel7.0_x86_64_dvd
Cleaning up everything
[root@server0 ~]# yum repolist //重新列出可用的源
已加载插件:langpacks
content.example.com_rhel7.0_x86_64_dvd | 4.1 kB 00:00:00
(1/2): content.example.com_rhel7.0_x86_64_dvd/group_gz | 134 kB 00:00:00
(2/2): content.example.com_rhel7.0_x86_64_dvd/primary_db | 3.4 MB 00:00:00
源标识 源名称 状态
content.example.com_rhel7.0_x86_64_dvd added from: http://content.example.com 4,305
repolist: 4,305
2 案例2:硬盘分区及格式化
2.1 问题
本例要求熟悉硬盘分区结构,使用fdisk分区工具在磁盘 /dev/vdb 上按以下要求建立分区:
采用默认的 msdos 分区模式
第1个分区 /dev/vdb1 的大小为 200MiB
第2个分区 /dev/vdb2 的大小为 2000MiB
第3个分区 /dev/vdb3 的大小为 1000MiB
完成分区后,能够配置开机自动挂载 /dev/vdb2 分区:
文件系统类型为 EXT4
将其挂载到 /mnt/part2 目录
2.2 方案
fdisk分区工具用来建立msdos分区方案,其交互模式中的主要指令如下:
m:列出指令帮助
p:查看当前的分区表信息
n:新建分区
d:删除分区
t:更改分区标识
q:放弃分区更改并退出
w:保存对分区表所做的更改
2.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:新建分区表
1)打开fdisk工具,操作磁盘/dev/vdb
[root@server0 ~]# fdisk /dev/vdb
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 0x9ac1bc10.
Command (m for help): //交互操作提示信息
2)新建第1个分区/dev/vdb1
Command (m for help): n //新建分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p //类型为p(主分区)
Partition number (1-4, default 1): 1 //分区编号1
First sector (2048-20971519, default 2048): //起始位置默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +200M
Partition 1 of type Linux and of size 200 MiB is set //结束位置+200MiB大小
Command (m for help): p //确认当前分区表
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 83 Linux
3)新建第2个分区/dev/vdb2
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p //类型为p(主分区)
Partition number (2-4, default 2): 2 //分区编号2
First sector (411648-20971519, default 411648): //起始位置默认
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-20971519, default 20971519): +2000M
Partition 2 of type Linux and of size 2 GiB is set //结束位置+2000MiB大小
Command (m for help): p //确认当前分区表
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 83 Linux
/dev/vdb2 411648 4507647 2048000 83 Linux
4)新建第3个分区/dev/vdb3
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (4507648-20971519, default 4507648):
Using default value 4507648
Last sector, +sectors or +size{K,M,G} (4507648-20971519, default 20971519): +1000M
Partition 3 of type Linux and of size 1000 MiB is set
Command (m for help): p //确认当前分区表
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 83 Linux
/dev/vdb2 411648 4507647 2048000 83 Linux
/dev/vdb3 4507648 6555647 1024000 83 Linux
5)调整分区类型标识(可选)
将/dev/vdb1的类型(默认为83,表示EXT2/3/4分区)修改为8e(LVM设备):
Command (m for help): t //修改分区类型标识
Partition number (1-3, default 3): 1 //指定第1个分区
Hex code (type L to list all codes): 8e //类型改为8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p //确认当前分区表
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 8e Linux LVM
/dev/vdb2 411648 4507647 2048000 83 Linux
/dev/vdb3 4507648 6555647 1024000 83 Linux
6)保存分区更改,退出fdisk分区工具
Command (m for help): w //保存并退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
6)刷新分区表
[root@server0 ~]# partprobe /dev/vdb //重新检测磁盘分区
//或者
[root@server0 ~]# reboot //对已使用中磁盘的分区调整,应该重启一次
.. ..
步骤二:格式化及挂载分区
1)将分区/dev/vdb2格式化为EXT4文件系统
[root@server0 ~]# mkfs.ext4 /dev/vdb2
.. ..
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
2)配置开机自动挂载
[root@server0 ~]# vim /etc/fstab
.. ..
/dev/vdb2 /mnt/part2 ext4 defaults 0 0
3)创建挂载点,并验证挂载配置
[root@server0 ~]# mkdir /mnt/part2 //创建挂载点
[root@server0 ~]# mount -a //挂载fstab中的可用设备
[root@server0 ~]# df -hT /mnt/part2/ //检查文档所在的文件系统及设备
Filesystem Type Size Used Avail Use% Mounted on
/dev/vdb2 ext4 1.9G 5.9M 1.8G 1% /mnt/part2
3 案例3:配置NTP网络时间客户端
3.1 问题
本例要求配置虚拟机 server0,能够自动校对系统时间。相关信息如下:
NTP服务器位于 classroom.example.com
此客户机的系统时间应当与NTP服务器的时间保持同步
3.2 方案
NTP服务端可以为客户端提供标准的日期时间。
在RHEL7主机中可以配置软件包chrony来使用NTP时间同步。
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:确认已安装NTP客户端软件包chrony
通常系统会默认安装此软件包:
[root@server0 ~]# rpm -q chrony
chrony-1.29.1-1.el7.x86_64
如果检查发现此软件包没有安装,请通过yum命令安装:
[root@server0 ~]# yum -y install chrony
.. ..
如果发现此软件包已经安装,但配置文件/etc/chrony.conf损坏或信息丢失,可删除此文件后重新安装chrony软件包:
[root@server0 ~]# rm -rf /etc/chrony.conf
[root@server0 ~]# yum -y reinstall chrony
已加载插件:langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 chrony.x86_64.0.1.29.1-1.el7 将被 重新安装
--> 解决依赖关系完成
.. ..
步骤二:调整NTP客户端配置
1)修改/etc/chrony.conf文件内的server配置
移除不可用的NTP服务器记录,正确添加可用的记录:
[root@server0 ~]# vim /etc/chrony.conf
.. ..
#server 0.rhel.pool.ntp.org iburst //注释掉默认的server配置,
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
server classroom.example.com iburst //添加新的配置
.. ..
2)开启NTP时间同步
[root@server0 ~]# timedatectl //查看现有状态
.. ..
NTP enabled: no //NTP可能尚未启用
NTP synchronized: no //尚未完成过一次NTP同步
.. ..
[root@server0 ~]# timedatectl set-ntp yes //启用NTP同步
[root@server0 ~]# timedatectl //查看启用后的状态
.. ..
NTP enabled: yes //NTP已经启用
NTP synchronized: no //尚未完成过一次NTP同步
.. ..
步骤三:测试NTP时间同步
1)先设置一个错误的系统日期时间
[root@server0 ~]# date -s '2001-09-11 11:30:00' //调整日期时间
2001年 09月 11日 星期二 11:30:00 CST
[root@server0 ~]# date //确认调整结果
2001年 09月 11日 星期二 11:30:01 CST
2)启动系统服务chronyd,并设为开机自启
[root@server0 ~]# systemctl restart chronyd
[root@server0 ~]# systemctl enable chronyd
3)重新查看当前的系统时间
重启chronyd服务后稍等片刻,当前系统的日期时间应该恢复正常(与NTP服务器保持一致):
[root@server0 ~]# date
2016年 12月 23日 星期五 23:44:53 CST
再次执行timedatectl查看,会发现NTP synchronized的值已经变成yes:
[root@server0 ~]# timedatectl
.. ..
NTP enabled: yes //NTP已经启用
NTP synchronized: no //已经完成过一次NTP同步
.. ..