1、简述linux操作系统启动流程
1.POST:通电自检
2.BIOS:BOIS自检,加载硬盘
3.读取MBR,进行MBR引导
4.bootloader:grub引导菜单
5.加载内核和inintamfs模块
6.内核开始初始化,使用systemd来代替centos6以前的init程序
7.执行initrd.target
包括挂载/etc/fstab文件中的系统,此时挂载后,就可以切换到根目录了
8.从initramfs根文件系统切换到磁盘根目录
entos7表面是有“运行级别”这个概念,实际上是为了兼容以前的系统,每个所谓的“运行级别”都有对应的软连接指向,默认的启动级别时/etc/systemd/system/default.target,根据它的指向可以找到系统要进入哪个模式
模式:
0 ==> runlevel0.target, poweroff.target
1 ==> runlevel1.target, rescue.target
2 ==> runlevel2.target, multi-user.target
3 ==> runlevel3.target, multi-user.target
4 ==> runlevel4.target, multi-user.target
5 ==> runlevel5.target, graphical.target
6 ==> runlevel6.target, reboot.target
9.systemd执行sysinit.target
10.systemd启动multi-user.target下的本机与服务器服务
11.systemd执行multi-user.target下的/etc/rc.d/rc.local
12.Systemd执行multi-user.target下的getty.target及登录服务
getty.target是启动终端的systemd对象。如果到此步骤,系统没有被指定启动图形桌面,到此就可以结束了,如果要启动图形界面,需要在此基础上启动桌面程序
13.systemd执行graphical需要的服务
2、制作一个只运行shell的linux系统
1.增加一块新硬盘,刷新设备
[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host2/scan
2.分区并创建文件系统
#/dev/sdb1对应/boot /dev/sdb2对应根/
[root@localhost ~]# echo -e 'n\np\n1\n\n+1G\nw\n' | fdisk /dev/sdb
[root@localhost ~]# echo -e 'n\np\n2\n\n\nw\n' | fdisk /dev/sdb
[root@localhost ~]# mkfs.ext4 /dev/sdb1
[root@localhost ~]# mkfs.ext4 /dev/sdb2
3.挂载boot
[root@localhost ~]# mkdir /mnt/boot
[root@localhost ~]# mount /dev/sdb1 /mnt/boot/
4.安装grub
[root@localhost ~]# grub-install --root-directory=/mnt/ /dev/sdb
5.准备内核和initramfs文件
[root@localhost ~]# cp /boot/vmlinuz-2.6.32-754.el6.x86_64 /mnt/boot/vmlinuz
[root@localhost ~]# cp /boot/initramfs-2.6.32-754.el6.x86_64.img /mnt/boot/initramfs.img
6.建立grub.conf
[root@localhost ~]# vi /mnt/boot/grub/grub.conf
default=0
timeout=6
title centos6
root (hd0,0)
kernel /vmlinuz root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs.img
7.准备根下面相关程序和库
[root@localhost ~]# mkdir /mnt/sysroot
[root@localhost ~]# mount /dev/sdb2 /mnt/sysroot/
[root@localhost ~]# mkdir -pv /mnt/sysroot/{boot,dev,sys,proc,etc,bin,sbin,tmp,var,usr,home,root,lib,lib64,opt,mnt,media}
# 准备网卡驱动
[root@localhost ~]# ethtool -i eth0
[root@localhost ~]# modinfo -n e1000
[root@localhost ~]# cp /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/
[root@localhost ~]# ldd /bin/bash
[root@localhost ~]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/
[root@localhost ~]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64/
[root@localhost ~]# cp /lib64/libc.so.6 /mnt/sysroot/lib64/
[root@localhost ~]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/
[root@localhost ~]# cp /bin/bash /mnt/sysroot/bin/
[root@localhost ~]# chroot /mnt/sysroot
8.准备新的虚拟机
将前一虚拟机sdb硬盘对应的vmdk文件增加进去,删除原有磁盘,开机启动
3、总结systemctl管理命令及system unit文件格式
# 启动或停止服务
systemctl start name.service
systemctl stop name.service
# 重启服务
systemctl restart name.service
# 查看服务状态
systemctl status name.service
# 禁止自动和手动启动服务
systemctl mask name.service
# 取消禁止
systemctl unmask name.service
# 查看服务当前激活与否的状态
systemctl is-active name.service
# 查看所有已经激活的服务
systemctl list-units --type service
# 查看所有服务
systemctl list-units --type service --all
# 设置服务开机自启或禁止启动
systemctl enable name.service
systemctl disable name.service
# 查看所有服务的开机自动状态
systemctl list-unit-files --type service
# 查看服务是否开机自启
systemctl is-enable name.service
# 查看失败服务列表
systemctl --failed --type=service
# 开机并立即启动或停止
systemctl enable --now name.service
systemctl diable --now name.service
# 查看服务的依赖关系
systemctl list-dependencies name.service
# 杀掉进程
systemctl kill unitname
service unit file文件通常由三部分组成:
- [Unit]:定义与Unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等
- Description:描述信息
- After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反
- Requires:依赖到的其它units,强依赖,被依赖的units无法激活时,当前unit也无法激活
- Wants:依赖到的其它units,弱依赖
- Conflicts:定义units间的冲突关系
- [Service]:与特定类型相关的专用选项;此处为Service类型
- Type:定义影响ExecStart及相关参数的功能的unit进程启动类型
- simple:默认值,这个daemon主要由ExecStart接的指令串来启动,启动后常驻于内存中
- forking:由ExecStart启动的程序透过spawns延伸出其他子程序来作为此daemon的主要服务。原生父程序在启动结束后就会终止
- oneshot:与simple类似,不过这个程序在工作完毕后就结束了,不会常驻在内存中
- dbus:与simple类似,但这个daemon必须要在取得一个D-Bus的名称后,才会继续运作.因此通常也要同时设定BusNname= 才行
- notify:在启动完成后会发送一个通知消息。还需要配合 NotifyAccess 来让 Systemd 接收消息
- idle:与simple类似,要执行这个daemon必须要所有的工作都顺利执行完毕后才会执行。这类的daemon通常是开机到最后才执行即可的服务
- EnvironmentFile:环境配置文件
- ExecStart:指明启动unit要运行命令或脚本的绝对路径
- ExecStartPre: ExecStart前运行
- ExecStartPost: ExecStart后运行
- ExecStop:指明停止unit要运行的命令或脚本
- Restart:当设定Restart=1 时,则当次daemon服务意外终止后,会再次自动启动此服务
- PrivateTmp:设定为yes时,会在生成/tmp/systemd-private-UUID-NAME.service-XXXXX/tmp/目录
- Type:定义影响ExecStart及相关参数的功能的unit进程启动类型
- [Install]:定义由“systemctl enable”以及"systemctl disable“命令在实现服务启用或禁用时用到的一些选项
- Alias:别名,可使用systemctl command Alias.service
- RequiredBy:被哪些units所依赖,强依赖
- WantedBy:被哪些units所依赖,弱依赖
- Also:安装本服务的时候还要安装别的相关服务
4、破解centos7 密码
启动时任意键暂停启动
按e键进入编辑模式
将光标移动linux 开始的行,添加内核参数rd.break
按ctrl-x启动
mount –o remount,rw /sysroot
chroot /sysroot
passwd root
#如果SELinux是启用的,才需要执行下面操作,如查没有启动,不需要执行
touch /.autorelabel
exit
reboot