Home  >  Article  >  Operation and Maintenance  >  Summary of Linux system startup and service management (organized and shared)

Summary of Linux system startup and service management (organized and shared)

WBOY
WBOYforward
2022-01-27 17:17:412481browse

This article brings you relevant knowledge about system startup and service management in Linux. During normal operation, the server rarely restarts, but if an unknown fault occurs or human error occurs, , an abnormal startup situation may occur. I hope this helps everyone.

Summary of Linux system startup and service management (organized and shared)

1. Foreword

During normal operation, the server rarely restarts, but if an unknown fault occurs or human error occurs, In this case, abnormal startup may occur. Although the Linux system startup process is relatively low-level knowledge, in order to deepen the understanding of the Linux system, this article is summarized.

2. Linux system startup process

Summary of Linux system startup and service management (organized and shared)

#2.1 Power-on self-test

After the server host is turned on, it will be based on the settings in the motherboard BIOS. The CPU, memory, graphics card, hard disk and other devices are initially tested. After successful detection, system control is transferred according to the preset startup sequence, and most of the time it is transferred to the local hard disk.

2.2 MBR boot

- When booting the system from the local hard disk, system control is first transferred to the system containing the MBR (Master Boot Record) settings in the first sector of the hard disk. The partition of the operating system boot file; or directly call the boot menu (such as GRUB) based on the boot information in the MBR record.

2.3 GRUB Menu

 For Linux operating systems, GRUB (Unified Boot Loader) is the most widely used multi-system boot program. After system control is passed to GRUB, the boot menu will be displayed for the user to choose, and the Linux kernel file will be loaded according to the selected option (or the default value), and then system control will be transferred to the kernel. It should be noted that Centos7 uses the GRUB2 boot boot loader.

2.4 Loading the Linux kernel

The Linux kernel is a pre-compiled special binary file, which is between various hardware resources and system programs and is responsible for resource allocation and scheduling. After the kernel takes over system control, it will fully control the running process of the entire Linux operating system.
In CentoS system, the default kernel file is located in "/boot/vmlinuz-3.10.0-514.el7.x86_64".

2.5 init process initialization

In order to complete the further system boot process, the Linux kernel first loads the "/sbin/init" program in the system into the memory and runs it. The init process is responsible for completing a series of system initialization process, and finally waits for the user to log in.

2.6 Summary of the system startup process

  • Detect the first device that can boot the system, such as a hard disk or optical drive
  • Run the startup GRUB placed in the MBR sector Boot program
  • GRUB boot program reads the GRUB configuration file/boot/grub2/grub.cfg to obtain the settings and path locations of the kernel and image file systems
  • Load the kernel and image file systems into the memory
  • Load the hardware driver, and the kernel loads the init process into the memory to run

3. Init process and systemd

3.1 init process

  • The /sbin/init program is loaded and run by the Linux kernel
  • The init process is the first process in the system
  • The PID number of the init process is always 1
[root@c7-1 ~]#ll /sbin/initlrwxrwxrwx. 1 root root 22 8月  13 21:44 /sbin/init -> ../lib/systemd/systemd

3.2 Centos5/6/7 init process comparison

Summary of Linux system startup and service management (organized and shared)

3.3 systemd

  • #systemd is an init software for the Linux operating system
  • CentOS7 uses a new systemd startup method to replace the traditional SysVinit
  • The first init process running in CentOS7 is /lib/systemd/systemd
[root@c7-1 ~]#ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 8月15 ?       00:00:31 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2      0  0 8月15 ?       00:00:00 [kthreadd]
root          4      2  0 8月15 ?       00:00:00 [kworker/0:0H]
root          6      2  0 8月15 ?       00:00:00 [ksoftirqd/0]
root          7      2  0 8月15 ?       00:00:00 [migration/0]
......

3.4 systemd unit type

Unit type Extension Description
Service .service Describes a system service
Socket . socket Describes a socket for inter-process communication
Device .device Describes a device file recognized by the kernel
Mount .mount Describes the mount point of a file system
Automount .automount Describes an automatic mount point for a file system
Swap .swap Describes a Memory swap device or swap file
Path .path Describes a file or directory in a file system
Timer .timer Describes a timer (used to implement cron-like scheduling tasks)
Snapshot .snapshot Used to save the state of a systemd
Scope .scope Use systemd’s bus interface for programming Create an external process
Slice .slice Describes a group of management system processes organized hierarchically in a Cgroup
Target .target Describes a group of systemd units

3.5 运行级别对应的 systemd 目标

运行级别 systemd 的 target 说明
0 target 关机状态,使用该级别时将会关闭主机
1 rescue.target 单用户模式,不需要密码验证即可登录系统,多用于系统维护
2 multi-user.target 用户定义/域特定运行级别。默认等同于3
3 multi-user.target 字符界面的完整多用户模式,大多数服务器主机运行在此级别
4 multi-user.target 用户定义/域特定运行级别。默认等同于3
5 graphical.target 图形界面的多用户模式,提供了图形桌面操作环境
6 reboot.target 重新启动,使用该级别时将会重启主机

3.6 systemd 管理命令 systemctl

格式:

systemctl COMMAND name.service		#.service 也可以省略

Sysvinit 和 Systemd 命令对比

Sysvinit命令 Systemd命令 含义
service name start systemctl start name 启动服务
service name stop systemctl stop name 关闭服务
service name restart systemctl restart name 重启服务不管当前是启动还是关闭状态
service name reload systemctl reload name 重新载入服务配置信息而不中断服务
service name condrestart systemctl condrestart name 运行状态的服务可以重启,不在运行状态无法重启
service name status systemctl status name 查看服务的运行状态
chkconfig name on systemctl enable name 设置服务为开机自启动
chkconfig name off systemctl disable name 设置服务为开机关闭
chkconfig name systemctl is-enabled name 检查服务是否开机自启
chkconfig name --add systemctl daemon-reload 创建一个新服务文件,或者变更配置的时候使用

示例:

#启动 httpd 服务
systemctl start httpd 
#停止 httpd 服务
systemctl stop httpd
#重启 httpd 服务
systemctl restart httpd
#查看 httpd 服务状态
systemctl status httpd
#禁止自动和手动启动服务(可以关闭运行的服务)
systemctl mask name.service	
#取消禁止
systemctl unmask name.service
#查看某服务当前是否正在运行
systemctl is-active name.service
#查看所有服务
systemctl list-unit-files --type=service
#查看所有已经激活的服务
systemctl list-unit-files --type=service | grep enabled
#列出启动失败的服务
systemctl --failed --type=service
或
systemctl list-units --state failed
或
systemctl list-unit-files --state=failed
#开启 httpd 服务并设置开机自启
systemctl enable --now httpd.service
#关闭 httpd 服务并关闭开机自启
systemctl disable --now httpd.service
#查看服务的依赖关系
systemctl list-dependencies name.service
#杀掉进程
systemctl kill unitname

4.总结

  作为一名运维工程,对 Linux 系统服务的管理是基本技能。一个系统在安装完成后免不了要对系统进行优化,关闭不必要的服务,节省系统资源,保护服务器不受网络攻击,这也是运维工作的职责所在,熟练掌握 Linux 系统的底层原理和基本操作,在面对问题的时候才不会茫然不知所措!

相关推荐:《Linux视频教程

The above is the detailed content of Summary of Linux system startup and service management (organized and shared). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete