CentOS7之Systemd管理

Systemd

本章节内容:

CentOS7启动

Unit介绍

服务管理和查看

启动排错

破解口令

修复grub2


1、 CentOS启动流程:POST –> Boot Sequence –> Bootloader –> kernel + initramfs(initrd) –> rootfs–> /sbin/init

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

 选择启动设备

 引导装载程序, centos7是grub2

 加载装载程序的配置文件:/etc/grub.d/ /etc/default/grub /boot/grub2/grub.cfg(注:一般上修改grub.cfg配置文件,是直接修改/etc/default/grub,然后使用命令#grub2-mkconfig /boot/grub2/grub.cfg来生成修改

 blob.png

 加载initramfs驱动模块

 加载内核选项

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

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

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

 systemd执行默认target配置,配置文件/etc/systemd/default.target /etc/systemd/system/

 systemd执行sysinit.target初始化系统及basic.target准备操作系统

 systemd启动multi-user.target下的本机与服务器服务

 systemd执行multi-user.target下的/etc/rc.d/rc.local

 systemd执行multi-user.target下的getty.target及登入服务

 systemd执行graphical需要的服务(此为图形界面所有)

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

3、Systemd新特性:

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

按需启动守护进程(端口与服务独立分离)

自动化的服务依赖关系管理

同时采用socket式与D-Bus总线式激活服务

系统状态快照

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

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

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

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

5、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 目录

6、Systemd的特性:

关键特性:

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

基于d-bus的激活机制:

基于device的激活机制:

基于path的激活机制:

系统快照:保存各unit的当前状态信息于持久存储设备中

向后兼容sysvinit脚本

 不兼容:

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

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

7、Systemd管理服务:

   管理系统服务:CentOS 7: service unit

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

  命令:systemctlCOMMAND name.service

  启动:service name start ==> systemctlstart name.service

  停止:service name stop ==> systemctlstop name.service

  重启:service name restart ==> systemctlrestart name.service

  状态:service name status ==> systemctlstatus 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

   blob.png

  取消禁止:systemctl unmask name.service

   blob.png

查看某服务当前激活与否的状态:systemctl is-active name.service

blob.png

  查看所有的服务:systemctl list-unit-files –type|-t service

  blob.png 

  查看所有激活的服务:systemctl list-units –type service –all|-a

  blob.png

  设定某服务开机自启(chkconfig命令的对应关系):chkconfig name on ==> systemctlenable name.service   

  设定某服务开机禁止启动:chkconfig name off ==> systemctl disable name.service

blob.png

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

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

用来列出该服务在哪些运行级别下启用和禁用

chkconfig sshd -list ==>ls /etc/systemd/system/*.wants/sshd.service

查看服务是否开机自启:systemctl is-enabled name.service

其它命令:查看服务的依赖关系:systemctl list-dependencies name.service

blob.png

杀掉进程:systemctlkill 进程名

Systemctl命令事例:

显示所有单元状态:systemctl 或systemctl list-units

只显示服务单元的状态:systemctl –type=service

  显示sshd服务单元:systemctl status sshd.service–l

  验证sshd服务当前是否活动:systemctl is-active sshd

  启动,停止和重启sshd服务

systemctl start sshd.service

systemctl stop sshd.service

systemctl restart sshd.service

   重新加载配置:systemctl reload sshd.service

   列出活动状态的所有服务单元:systemctl list-units –type=service

   列出所有服务单元:systemctl list-units –type=service –all

   查看服务单元的启用和禁用状态:systemctl list-unit-files –type=service

   列出失败的服务:systemctl –failed –type=service

   列出依赖的单元:systemctl list-dependencies sshd

   验证sshd服务是否开机启动:systemctl is-enabled sshd

   禁用network,使之不能自动启动,但手动可以:systemctl disable network

   启用network:systemctl enable network

   禁用network,使之不能手动或自动启动:systemclt mask network

   启用network:systemctl umask network

7、服务状态:

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

  loaded:Unit配置文件已处理

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

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

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

  inactive:不运行

  enabled:开机启动

  disabled:开机不启动

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

8、运行级别:target units

unit配置文件:.target

ls /usr/lib/systemd/system/*.target

systemctl list-unit-files –type target –all

 blob.png

级别切换:init N ==> systemctl isolate name.target

eg:systemctl isolate multi-user.target

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

 blob.png

查看target:runlevel 或who -r

systemctl list-units –type target

 blob.png

获取默认运行级别:/etc/inittab==> systemctl get-default

  blob.png

修改默认级别:/etc/inittab==> systemctl set-default name.target

systemctl set-default multi-user.target

ls -l /etc/systemd/system/default.target

blob.png

9、其他命令:

切换至紧急救援模式:systemctl rescue

 切换至emergency模式:systemctl emergency (注:当切换到紧急救援模式失败,可以尝试切换到该模式救援)

 其它常用命令:

传统命令init,poweroff,halt,reboot都成为systemctl的软链接:

关机:systemctl halt、systemctlpower off

重启:systemctl reboot

挂起:systemctl suspend

休眠:systemctl hibernate

休眠并挂起:systemctl hybrid-sleep

10、service units 文件格式:

   /etc/systemd/system:系统管理员和用户使用

/usr/lib/systemd/system:发行版打包者使用

 以“#” 开头的行后面的内容会被认为是注释

 相关布尔值,1、yes、on、true 都是开启,0、no、off、false 都是关闭。

 时间单位默认是秒,所以要用毫秒(ms)分钟(m)等请显式说明

   service unit file文件通常由三部分组成

[Unit]:定义与Unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等

[Service]:与特定类型相关的专用选项;此处为Service类型

[Install]:定义由“systemctl enable”以及"systemctl disable“命令在实现服务启用或禁用时用到的一些选项

   blob.png

(1)Unit段的常用选项:

   Description:描述信息

   After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反

   Requires:依赖到的其它units,强依赖,被依赖的units无法激活时,当前unit即无法激活

   Wants:依赖到的其它units,弱依赖

   Conflicts:定义units间的冲突关系

(2)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服务意外终止后,会再次自动启动此服务

(3)Install段的常用选项:

Alias:别名,可使用systemctl command Alias.service

RequiredBy:被哪些units所依赖,强依赖

WantedBy:被哪些units所依赖,弱依赖

Also:安装本服务的时候还要安装别的相关服务

注意:对于新创建的unit文件,或者修改了的unit文件,要通知systemd重载此配置文件,而后可以选择重启

# systemctl daemon-reload

 

事例:vim /etc/systemd/system/bak.service

[Unit]

Description=backup my etc

Requires=atd.service

[Service]

Type=simple

ExecStart=/bin/bash -c "echo /testdir/bak.sh|at now"

[Install]

WantedBy=multi-user.target

#systemctl daemon-reload

#systemctl start bak

 

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

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

systemd.unit=emergency.target

systemd.unit=recure.target

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

12、启动排错:

文件系统损坏:先尝试自动修复,失败则进入emergency shell,提示用户修复

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

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

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

13、破解root密码:

  启动时任意键暂停启动

  按e键进入编辑模式

  将光标移动linux16开始的行,添加内核参数rd.break

  按ctrl-x启动

  #mount –o remount,rw /sysroot

  #chroot /sysroot

  #passwd root

  #touch /.autorelabel

#exit

#exit

14、修改grub2:

   GRUB“the Grand Unified Bootloader”

引导提示时可以使用命令行界面

可从文件系统引导

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

 修复配置文件:grub2-mkconfig > /boot/grub2/grub.cfg(grub2-mkconfig -o /boot/grub2/grub.cfg)

 修复grub:grub2-install /dev/sda BIOS环境

grub2-install UEFI环境

 

习题:

1、 为编译安装的httpd服务,实现service unit文件

编译安装httpd:#./configure –prefix=/usr/local/httpd –sysconfdir=/etc/httpd

blob.png

编写httpd的服务Unit:

blob.png

blob.png

2、破解centos7 口令

blob.png

在linux16该行的后面输入rd.break完成后,使用Ctrl+x启动系统

blob.png

下图为进入破解的命令行操作界面:

blob.png

执行破解的步骤:

blob.png

注:需要等待一段时间,修改过后root的密码,系统需要扫描整个磁盘的文件,将没有安全标签的文件给打上标签。

3、修改默认的启动内核

#vim /etc/default/grub

blob.png

4、启动时临时禁用SELinux

blob.png

blob.png

5、启动时进入emergency模式

blob.png

blob.png

输入root的密码后得到下图的界面:

blob.png

6、删除编译安装的新内核

#rm -rf /boot/*.3.18.41*

#vim /boot/grub2/grub.cfg 删除相应的kernel信息

#rm -rf /lib/modules/3.18.41*

#rm -rf /usr/src/linux-3.18.41 (一般建议在编译安装完成后,将相应的源码文件删除掉)

 

 

 

1、求每班总成绩和平均成绩

name class score

wang   1    100

zhang  2    90

li     1    80

 

方法一:

# awk '!/^name/{if($2==1){num1++;sum1+=$3}else{num2++;sum2+=$3}}END{print "class1",sum1,

sum1/num1,"\n""class2",sum2,sum2/num2}' score

blob.png

方法二:

# awk '!/^name/{class[$2]++;sum[$2]+=$3}END{for(i in class){print i,sum[i],sum[i]/class[i]}}' score

blob.png

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

(1)
上一篇 2016-09-23 09:06
下一篇 2016-09-23 09:06

相关推荐

  • ☞Nginx

    Nginx 概述 yum安装Nginx 编译安装Nginx 主要文件路径 主配置文件nginx.conf main block:全局配置段 http/https 协议相关配置段 性能优化初步 调试、定位问题的配置 events段配置 worker、method、accept_mutex http协议相关配置段 虚拟主机 基于PORT、IP、Server Na…

    Linux干货 2016-10-24
  • N22 网络班第五周课程作业

    1、显示当前系统上root、fedora或user1用户的默认shell; grep -E "^(root|fedora|user1)\>" /etc/passwd | ll grep -E "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f 1,7…

    Linux干货 2016-09-19
  • N25-第19周博客作业

    1、描述Tomcat的架构; 2、详细解释Tomcat的配置文件及配置文件中的参数所代表的含义; 3、配置apache通过mod_proxy模块与Tomcat连接的详细过程; 4、配置基于mod_jk的负载均衡; 5、配置tomcat集群,能够实现用户的session会话保持。 1、描述Tomcat的架构; Tomcat组件,分为4类: 顶层类组件:包括&l…

    2017-05-21
  • 第十周 N21 总有刁民想害朕

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)   加电-MBR-GRUB-/sysinit-init X 对应的服务-/etc/rc.d     MBR        读取分区表     GRUB      &…

    Linux干货 2016-09-26
  • linux文件系统上的权限

    文件系统上的权限是指文件系统上的文件和目录的权限,主要针对三类对象进行的(访问者)进行的定义: 针对文件来说 r 使用工具cat nano去看文件里面内容的权限 w 可以修改文件的内容 x 二进制程序以及脚本需要发起系统调用,去启动为一个进程 针对目录来说 r 是否可以查看目录里面的内容 有r权限的话 可以使用ls -l w 对目录里面是否可以创建文件或者目…

    Linux干货 2017-04-01
  • 网络管理之多网卡bond,mode1

    一.概述 Bonding     就是将多块网卡绑定同一IP地址对外提供服务,可以实现高可用或者负载均衡。当然,直接给两块网卡设置同一IP地址是不可能的。通过bonding,虚拟一块网卡对外提供连接,物理网卡的被修改为相同的MAC地址。 Bonding的工作模式     Mode…

    Linux干货 2016-09-15

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-26 12:17

    文章知识总结的很详细,赞一个