Linux学习第12周

1、简述linux操作系统启动流程

centos6操作系统:
(1) 加载BIOS的硬件信息,开机自检,获取第一个启动设备
(2) 读取第一个启动设备MBR的引导加载程序(grub)的启动信息
(3) 加载核心操作系统的核心信息,核心开始解压缩,并尝试驱动所有的硬件设备
(4) 核心执行init程序,并获取默认的运行信息
(5) init程序执行/etc/rc.d/rc.sysinit文件,重新挂载根文件系统
(6) 启动核心的外挂模块
(7) init执行运行的各个批处理文件(scripts)
(8) init执行/etc/rc.d/rc.local
(9) 执行/bin/login程序,等待用户登录
(10) 登录之后开始以Shell控制主机

Cent7和Centos8系统:
(1) UEFi或BIOS初始化,运行POST开机自检
(2) 选择启动设备
(3) 引导装载程序, centos7是grub2,加载装载程序的配置文件:
(4) 加载initramfs驱动模块
(5) 加载内核选项
(6) 内核初始化,centos7使用systemd代替init
(7) 执行initrd.target所有单元,包括挂载/etc/fstab
(8) 从initramfs根文件系统切换到磁盘根目录
(9) systemd执行默认target配置,配置文件/etc/systemd/system/default.target
(10) systemd执行sysinit.target初始化系统及basic.target准备操作系统
(11) systemd启动multi-user.target下的本机与服务器服务
(12) systemd执行multi-user.target下的/etc/rc.d/rc.local
(13) Systemd执行multi-user.target下的getty.target及登录服务
(14) systemd执行graphical需要的服务

2、制作一个只运行shell的linux系统

(1) 分区并创建文件系统
增加一块20G新硬盘sdb,使用fdisk /dev/sdb创建两个分区:/dev/sdb1对应/boot,/dev/sdb2对应根 /
然后给两个分区创建文件系统
[root@centos6 ~]#mkfs.ext4 /dev/sdb1
[root@centos6 ~]#mkfs.ext4 /dev/sdb

(2) 挂载boot
子目录必须为boot
[root@centos6 ~]#mkdir /mnt/boot
[root@centos6 ~]#mount /dev/sdb1 /mnt/boot
(3) 安装grub
[root@centos6 ~]#grub-install --root-directory=/mnt/ /dev/sdb
(4) 准备内核和initramfs文件
[root@centos6 ~]#cp /boot/vmlinuz-2.6.32-754.el6.x86_64 /mnt/boot/vmlinuz
[root@centos6 ~]#cp /boot/initramfs-2.6.32-754.el6.x86_64.img /mnt/boot/initramfs.img
(5) 建立grub.conf
[root@centos6 ~]#cat /mnt/boot/grub/grub.conf
default=0
timeout=6
title wang linux
root (hd0,0)
kernel /vmlinuz root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs.img
[root@centos6 ~]#tree /mnt/boot
/mnt/boot
├── grub
│ ├── device.map
│ ├── e2fs_stage1_5
│ ├── fat_stage1_5
│ ├── ffs_stage1_5
│ ├── grub.conf
│ ├── iso9660_stage1_5
│ ├── jfs_stage1_5
│ ├── minix_stage1_5
│ ├── reiserfs_stage1_5
│ ├── stage1
│ ├── stage2
│ ├── ufs2_stage1_5
│ ├── vstafs_stage1_5
│ └── xfs_stage1_5
├── initramfs.img
├── lost+found
└── vmlinuz
2 directories, 16 files
(6) 准备根下面相关程序和库
[root@centos6 ~]#mkdir /mnt/sysroot
[root@centos6 ~]#mount /dev/sdb2 /mnt/sysroot
[root@centos6 ~]#mkdir –pv /mnt/sysroot/{boot,dev,sys,proc,etc,lib,lib64,bin,sbin,tmp,var,usr,opt,home,root,mnt,media}
复制bash命令和相关库文件,
使用/data/copycmd.sh脚本进行命令和相关库文件的复制
/copycmd.sh脚本内容如下:

(7) 准备网卡驱动
[root@centos6 ~]#ethtool -i eth0
driver: e1000
version: 7.3.21-k8-NAPI
firmware-version:1.4.7
bus-info: 0000:02:01.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
[root@centos6 ~]#modinfo -n e1000
/lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
[root@centos6 ~]#cp /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/
(8) 准备新的虚拟机
将前一虚拟机sdb硬盘对应的vmdk文件增加进去,删除原有磁盘,开机启动。

3、总结systemctl管理命令及system unit文件格式

(1) systemctl管理命令
命令格式:
systemctl COMMAND name.service
#启动:相当于service name start
systemctl start name.service
#停止:相当于service name stop
systemctl stop name.service
#重启:相当于service name restart
systemctl restart name.service
#查看状态:相当于service name status
systemctl status name.service
#禁止自动和手动启动:
systemctl mask name.service
#取消禁止
systemctl unmask name.service
#查看某服务当前激活与否的状态:
systemctl is-active name.service
#查看所有已经激活的服务:
systemctl list-units --type|-t service
#查看所有服务:
systemctl list-units --type service --all|-a
#设定某服务开机自启,相当于chkconfig name on
systemctl enable name.service
#设定某服务开机禁止启动:相当于chkconfig name off
systemctl disable name.service
#查看所有服务的开机自启状态,相当于chkconfig --list
systemctl list-unit-files --type service
#用来列出该服务在哪些运行级别下启用和禁用:chkconfig –list name
ls /etc/systemd/system/.wants/name.service
#查看服务是否开机自启:
systemctl is-enabled name.service
#列出失败的服务
systemctl --failed --type=service
#开机并立即启动或停止
systemctl enable --now postfix
systemctl disable --now postfix
#查看服务的依赖关系:
systemctl list-dependencies name.service
#杀掉进程:
systemctl kill unitname
(2) system unit文件格式
service unit file文件通常由三部分组成:
(1) [Unit]
定义与Unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等
Unit段的常用选项:
Description:描述信息
After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反
Requires:依赖到的其它units,强依赖,被依赖的units无法激活时,当前unit也无法激活
Wants:依赖到的其它units,弱依赖
Conflicts:定义units间的冲突关系
(2) [Service]*
与特定类型相关的专用选项;此处为Service类型
(3) [Install]
定义由"systemctl enable"以及"systemctl disable"命令在实现服务启用或禁用时用到的一些选项
Install段的常用选项
Alias:别名,可使用systemctl command Alias.service
RequiredBy:被哪些units所依赖,强依赖
WantedBy:被哪些units所依赖,弱依赖
Also:安装本服务的时候还要安装别的相关服务

4、破解centos7密码

(1) 方法1
1) 启动时任意键暂停启动
2) 按e键进入编辑模式
3) 将光标移动linux 开始的行,添加内核参数rd.break
4) 按ctrl-x启动
5) 重新挂载根目录,并修改其挂载属性
mount –o remount,rw /sysroot
6) 切换根并更重新设置root密码
chroot /sysroot
passwd root
7) 如果SELinux是启用的,才需要执行下面操作,如查没有启动,不需要执行
touch /.autorelabel
8) 重启
exit
reboot
(2) 方法2
1) 启动时任意键暂停启动
2) 按e键进入编辑模式
3) 将光标移动linux 开始的行,改为rw init=/sysroot/bin/sh
4) 按ctrl-x启动
5) 切换根并更重新设置root密码
chroot /sysroot
passwd root
6) 如果SELinux是启用的,才需要执行下面操作,如查没有启动,不需要执行
touch /.autorelabel
7) 重启
exit
reboot

猜你喜欢

转载自blog.51cto.com/14255962/2642342