CentOS7的启动及Systemd的管理✧

CentOS7的启动流程

    POST–>Boot Sequence–>Bootloader–>kernel+initramfs(initrd)–>rootfs–>/sbin/init

    init:

        CentOS5:SysV init

        CentOS6:Upstart

        CentOS7:Systemd

    blob.png

    Systemd:系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其他进程

    blob.png

    Systemd新特性:

        系统引导是实现服务并行启动

        按需启动守护进程

        系统状态快照

        自动化的服务依赖关系

    

    核心概念:unit

        unit表示不同类型的systemd对象,通过配置文件进行标识和配置,文件中主要包含了系统服务,监听socket,保存的系统快照以及其他与init相关的信息

    配置文件:

        /usr/lib/systemd/system:每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/

        /run/systemd/system:系统执行过程中产生的服务脚本,比上面目录优先运行

        /etc/systemd/system:管理员建立的执行脚本,类似于/etc/rc.d/rcN.d/Sxx类的功能,比上面的优先级高


    UNIT类型:

        Systemctl -t help 查看unit类型

    blob.png

        Service unit:文件扩展名.service 用于定义系统服务

        Target unit:文件扩展名.target 用于模拟实现运行级别

        Device unit: .device 用于定义内核识别的设备

        Mount unit: .mount 定义文件系统的挂载点

        Socket unit: .socket 用于标识进程间通行用的socket文件,也可在系统启动时,延迟启动服务,实现按需启动

        Snapshot unit: .snapshot 管理系统快照

        Swap unit: .swap 用于标识swap设备

        Automount unit: .automount 文件系统的自动挂载点

        Path unit: .path 用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激活服务,如spool目录

        blob.png


    特性:

    关键特性:

        基于socket的激活机制:socket与服务程序分离

        基于d-bus的激活机制:系统进程间通信的一种机制 desktop-bus

        基于device的激活机制

        基于path的激活机制

        系统快照:保存个unit的当前状态信息于持久存储设备中向后兼容sysv init脚本

    不兼容:

        systemctl命令固定不变,不可扩展

        非有systemd启动的服务,systemctl无法与之通信和控制


    系统服务管理:

        CentOS7:service unit    注意:能兼容早期的服务脚本

        命令:systemctk Command name.service

        启动:systemctl name start –> systemctl start name.service

        停止:systemctl name stop –> systemctl stop name.service

        重启:systemctl name restart –> systemctl restart name.service

        状态:systemctl name status –> systemctl status name.service 

blob.png

        条件式重启:以启动才重启,否则不做任何操作

        service name condrestart –> systemctl try-restart name.service

        重载或重启服务:先加载,再启动

        systemctl reload-or-restart name.service

        重载或条件式重启服务:

        systemctl reload-or-try-restart name.service

        禁止自动和手动启动:

        systemctl mask name.service

        取消禁止:

        systemctl unmask name.service  

blob.png   

blob.png   

    服务查看

        查看某服务当前激活与否的状态:

        systemctl is-active name.service

        查看所有已经激活的服务

        systemctl list-units –type service

        查看所有服务

        systemctl list-units –type service -all

        blob.png

        chkconfig命令的对应关系:

        设定某服务开机自启:

        chkconfig name on –> systemctl enable name.service

        设定某服务开机禁止启动:

        chkconfig name off –> systemctl disable name.service

        查看所有服务开机的自启状态:

        chkconfig –list –> systemctl list-unit-files –type service        

        查看服务是否开机自启:

        systemctl is-enable name.service

        blob.png

    其他命令:

            查看服务的依赖关系:

        systemctl list-dependencies name.service

        blob.png

        杀掉进程:

        systemctl kill 进程名

    

    服务状态:

        systemctl list-units –type service –all显示状态

        loaded:Unit配置文件已处理

        active(runinng):一次或多次持续处理的运行

        active(exited):成功完成一次性的配置

        active(waiting):运行中,等待一个事件

        inactive:不运行

        enabled:开机启动

        disabled:开机不启动

        static:开机不启动,但可被另一个启用的服务激活

        blob.png

    运行级别:

        unit配置文件: .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

    级别切换;

        init N –> systemctl isolate name.target

        只有/lib/systemd/system/*.target文件中AllowIsolate=yes才能切换(修改文件需执行systemctl daemon-reload才能生效

blob.png

    查看target:

        runlevel    

        systemctl list-units –type target

        systemctl list-units –type target –all

    获取默认运行级别:

        /etc/inittab–> systemctl get-default

    修改默认级别:

        /etc/inittab –> systemctl set-default name.target   

 blob.png

    其他命令:

        切换至紧急救援模式:

            systemctl rescue

        切换至emergency模式:

            systemctl emergency

        关机:

            systemctl halt/poweroff

        重启:

            systemctl reboot

        挂起:

            systemctl suspend 数据还保存在内存中

        休眠:

            systemctl hibernate 数据写入磁盘

        休眠并挂起

            systemctl hybri-sleep


CentOS7引导顺序

    UEFI或BIOS初始化,运行POST开机自检

    选择启动设备

    引导装载程序,CentOS7是grub2

    加载装载程序的配置文件:/etc/grub.d/ /etc/default/grub /boot/grub2/grub.cfg

    加载initramfs驱动模块

    加载内核选项

    内核初始化,CentOS7使用systemd代替init

    执行initrd.target所有单元,包括挂载/etc/fstab

    从initramfs根文件系统切换到磁盘根目录

    systemd执行默认target配置

        其配置文件:/etc/systemd/system/default.target /etc/systemd/system/

    blob.png


   内核参数设置:

    设置内核参数,只影响当次启动

    启动时,在linux16行后添加systemd.unit=desired.target

    system.unit=emergency.target

    system.unit=recure.target

    recure.target比emergency支持更多的功能,日志等


   启动排错:

    文件系统损坏:

    先尝试自动修复,失败则进入emergency shell,提示用户修复

    在/etc/fstab不存在对应设备的UUID等一段时间,如不可用,进入emergency shell

    在/etc/fstab不存在对应的挂载点,systemd尝试创建挂载点,否则提示进入emergency shell

    在/etc/fstab不正确的挂载选项,提示进入emergency shell


    破解root口令:

    按e进入编辑模式

    将光标移至linux16行尾,添加内核参数rd.break

    按ctrl+x启动

    重新挂载根文件系统mount -o remount,rw /sysroot

    切换当前的工作环境至/sysroot:chroot /sysroot

    passwd root

    touch /.autorelabel重新打标签

    blob.png

    blob.png

    blob.png

    blob.png

    修复Grub2

    引导时进入可以使用命令行的界面

    可从文件系统引导

    只要配置文件/boot/grub2/grub.cfg

    修复配置文件

    grub-mkconfig> /boot/grub2/grub.cfg

    修复grub

    grub2-install /dev/sda BIOS环境

    grub2-install UEFI环境 

    


原创文章,作者:Stupid_L,如若转载,请注明出处:http://www.178linux.com/48130

(1)
Stupid_LStupid_L
上一篇 2016-09-21 08:12
下一篇 2016-09-21 09:43

相关推荐

  • 马哥教育网络班21期第2周课程练习

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。     复制命令:cp         cp [OPTION]… [-T] SOURCE DEST     &…

    Linux干货 2016-07-16
  • Shell脚本编程初步

        shell是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。shell脚本(shell script),是一种为shell编写的脚本程序。业界所说的shell通常都是指shell脚本,但shell和shell script是两个不同的概念。shell编程跟java、php编程…

    Linux干货 2016-08-24
  • 运维工程师技能需求排行

    这是我今天在拉勾网搜索运维,翻完了4四页也招聘信息之后得到的,我的目的是想要看看之后的学习,哪个更应该成为重点,有些在我意料之中,有些还真的没想到,算是努力了一个小时的收获吧,分享给大家。
    注意:其中的看法仅代表个人观点,很多都是依靠我自己的学习经验和工作经验累积的

    Linux干货 2017-12-12
  • Linux文件类型及颜色标识整理

    Linux系统上的文件类型 -:f; 常规文件 , d:directory, 目录文件; b;block device,块设备文件,支持以'block'为单位进行随机访问; c:character device,字符设备文件,支持以'character'为单位进行线性访问;    &nbs…

    Linux干货 2016-10-16
  • bash数据类型探秘

    数组 变量:存储单个元素的内存空间数组:存储多个元素的连续的内存空间,相当于多个变量的在调用变量时最好加双引号,对于字符串中含有空格等字符的能更好的调用集合。数组名和索引索引:编号从0开始,属于数值索引( 偏移量从默认0开始 )注意:索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引, bash4.0版本之后开始支持。bash的数组支持稀疏格式(索引…

    Linux干货 2016-08-24
  • linux文件管理类、bash的基础命令以及习题。

    文件管理工具:cp, mv, rm cp命令:copy 源文件:目标文件; 单源复制:cp [OPTION]… [-T] SOURCE DEST 多源复制:cp [OPTION]… SOURCE… DIRECTORY         cp&nb…

    Linux干货 2016-11-05

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-23 11:28

    文章对systemd的相关知识总结的很详细,截图说明也很直观。