您现在的位置是: 首页> 学无止境> 心得笔记> 心得笔记

Linux启动流程和服务管理(init和systemd)

张伟江2021-10-26 09:59心得笔记1465人已围观

目录

一:Linux启动流程

       init和Systemd的区别

二:Linux服务管理(service,systemctl)


一:Linux启动流程

Rhel6启动过程:


Rhel7启动过程:

 GRUB2相较于GRUB一代的提升:更健壮、可移植、更强大。支持BIOS、EFI和OpenFirmware,支持GPT和MBR分区表。支持非Linux系统,如苹果HFS文件系统和Windows的NTFS文件系统

systemd 被设计用来改进 sysvinit 的缺点,它和ubuntu的upstart是竞争对手,预计会取代它们。

systemd的目标是:尽可能启动更少进程;尽可能将更多进程并行启动。systemd尽可能减少对shell脚本的依赖。传统sysvinit使用inittab来决定运行哪些shell脚本,大量使用shell脚本被认为是效率低下无法并行的原因。systemd使用了Linux专属技术,不再顾及POSIX兼容。

init和Systemd的区别

init: 

  • 一是启动时间长,init是串行启动,只有前一个进程启动完,才会启动下一个进程
  • 二是启动脚本复杂,Init进程只是执行启动脚本,不管其他事情,脚本需要自己处理各种情况,这往往使得脚本变得很长
  • 由Linux内核加载运行,位于 /sbin/init   ,是系统中第一个进程,PID永远为1

对于支持 service 的程序,安装的时候,会自动的在 /etc/init.d 目录添加一个配置文件。当我们使用 service 控制程序时,比如执行开启httpd的服务:service httpd  start  。那么我们的 service 就会开启 /etc/init.d/httpd 配置文件里面指向的 /usr/sbin/httpd 可执行文件

systemd:

  • 按需启动服务,减少系统资源消耗。
  • 尽可能并行启动进程,减少系统启动等待时间
  • 由Linx内核加载运行,位于 /usr/lib/systemd/systemd  ,是系统中第一个进程,PID永远为1

对于支持 systemd 的程序,安装的时候,会自动的在 /usr/lib/systemd/system 目录添加一个配置文件。当我们使用 systemctl 控制该程序时,比如执行开启httpd服务:systemctl  start  httpd.service 。那么我们的 systemctl 就会开启 httpd.service 配置里面指向的 /usr/sbin/httpd 可执行文件

如果我们想让该程序开机启动,我们可以执行命令 systemctl enable  httpd,这个命令相当于在 /etc/systemd/system 目录添加一个软链接,指向 /usr/lib/systemd/system 目录下的 httpd.service 文件。这是因为开机时,Systemd只执行 /etc/systemd/system 目录里面的配置文件。

Init 进程的配置文件

参数说明
/etc/init.d/服务启动脚本配置文件存放目录
/etc/inittab默认运行级别配置文件
/etc/init/rcS.conf系统初始化配置文件
/etc/init/rc.conf各运行级别初始化的配置文件
/etc/init/rcS-sulogin.conf单用户模式启动 /sbin/sushell 环境的配置文件
/etc/init/control-alt-delete.conf终端下的 ctrl+alt+del 热键操作的配置文件
/etc/sysconfig/inittty终端的配置文件
/etc/init/start-ttys.conf配置tty终端的开启数量、设备文件
/etc/init/tty.conf  或  /etc/init/serial.conf控制tty终端的开启

Systemd进程的配置文件

参数说明
/etc/systemd/system/default.target取代/etc/inittab文件配置,通常符号链接到 /lib/systemd/system/graphical.target
/run/systemd/system/系统执行过程中所产生的服务脚本所在目录
/etc/systemd/system/里面存放着不同级别的开启自启服务 
/usr/lib/systemd/system/ 和 /lib/systemd/system/ 和,两个文件完全一样,因为lib是/usr/lib的软链接每个服务最主要的启动脚本设置,类似于之前的 /etc/init.d/

 运行级别和说明

运行级别说明Rehl 6/7 命令Rhel7 命令
0关机状态,使用该级别将会关机init  0 poweroff
1系统救援模式,多用于系统维护init  1systemctl  isolate  rescue.target
2字符界面的多用户模式(不可访问网络)init  2systemctl  isolate  mutil-user.target
3字符界面的完整多用户模式,大多数服务器主机运行此级别init  3systemctl  isolate  mutil-user.target
4未分配使用init  4systemctl  isolate  mutil-user.target
5图形界面的多用户模式,提供了图形桌面操作环境init  5systemctl  isolate  graphical.target
6重新启动主机init  6reboot

查看运行级别:

  • runlevel : 显示切换前的运行级别 和当前运行级别 (6/7)
  • systemctl get-default : 显示当前运行级别 (7)

永久设置开机模式

  •  systemctl set-default multi-user.target    开机默认为文本模式
  •  systemctl set-default graphical.target     开机默认为图形模式
  •  修改 /etc/inittab 默认运行级别配置文件

二:Linux服务管理(service,systemctl)

Rhel6 用 service 和 chkconfig 来管理服务,它是 SystemV 架构下的一个工具。
Rhel7 是用 systemctl  来管理服务,它融合了之前的 service 和 chkconfig 的功能于一体。可以使用它永久性或只在当前会话中启用/禁用服务。systemctl 是 systemd 架构下的一个工具。

动作Rhel6 旧指令Rhel7新指令
启动某服务service  httpd   startsystemctl  start   httpd
停止某服务service  httpd   stop systemctl  stop   httpd
重启某服务service  httpd   restartsystemctl  restart  httpd
检查服务状态service  httpd  statussystemctl  status  httpd
删除某服务 chkconfig  --del  httpd停掉应用,删除其配置文件
使服务开机自启动chkconfig  --level   5  httpd   onsystemctl   enable  httpd
使服务开机不自启动chkconfig  --level   5  httpd   offsystemctl   disable  httpd
显示所有已启动的服务chkconfig  --listsystemctl list-unit-files | grep enabled
加入自定义服务chkconfig  --add  testsystemctl   load  test
查询服务是否开机自启chkconfig --list | grep httpdsystemctl  is-enabled   httpd
查看启动失败的服务 systemctl  --failed

systemd的一些常用命令:

列出所有可用单元 : systemctl  list-unit-files
列出所有运行的单元: systemctl list-unit-files | grep enabled 
列出所有可用服务:  systemctl list-unit-files  --type=service
列出所有运行的服务: systemctl list-unit-files  --type=service | grep enabled 
屏蔽httpd服务:systemctl  mask httpd

点赞(0) 打赏

文章评论 共有 0 条评论

暂无评论

本栏推荐

猜你喜欢

站点信息

  • 建站时间:2018年10月24日
  • 网站程序:fastadmin
  • 文章统计301篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信号:zwj982215226

打赏本站

  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!
  • 微信扫码:你说多少就多少~
  • 支付宝扫码:你说多少就多少~

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部