一、systemd简介

Systemd是一款用于Linux操作系统的系统和服务管理工具,在RHEL7之前使用init来初始化系统,从RHEL7之后开始使用systemd来初始化系统,也就是操作系统启动时运行的第一个程序

systemd对比init的优势:

  • systemd是并行启动 init是串行启动
  • systemd启动更快;而init相对较慢
  • init通过脚本的方式来维持系统的启动条目,systemd使用单元进行管理

二、systemctl用法

systemctl是Linux系统中用于管理systemd的命令行工具。它是systemd的一部分,用于控制和管理systemd服务、单元(Unit)以及相关配置

2.1 查看系统的服务单元

[root@localhost ~]# systemctl list-units --type service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION                                                                  
accounts-daemon.service            loaded active running Accounts Service                                                             
alsa-state.service                 loaded active running Manage Sound Card State (restore and store)                                  
atd.service                        loaded active running Job spooling tools                                                           
auditd.service                     loaded active running Security Auditing Service                                                    
avahi-daemon.service               loaded active running Avahi mDNS/DNS-SD Stack                                                      
bluetooth.service                  loaded active running Bluetooth service                                                            
chronyd.service                    loaded active running NTP client/server  
....
以下省略

以atd服务为例,以上命令执行回显各字段意义如下:

atd.service(服务名)

loaded(服务配置是否正确载入)

active(服务的高级状态)

running(服务低级状态)     

Job spooling tools(服务描述信息)

服务的高级状态与低级状态

高级状态: 服务的总状态,表示服务是否在系统中运行

低级状态: 服务程序的进程状态

或者我们还可以通过以下命令查看服务状态

systemd管理服务_linux

绿色高亮的字体为服务的状态,active是服务的高级状态,括号里面的是服务的低级状态,running表示服务当前正在运行, exited表示服务已经正确运行过一次,已退出,等待下一次运行,只有运行的那一瞬间才是running状态

2.2 管理开机自启的服务

systemctl  list-unit-files –type service  查看服务的单元文件

[root@localhost ~]# systemctl list-unit-files 
UNIT FILE                                  STATE    
proc-sys-fs-binfmt_misc.automount          static   
-.mount                                    generated
boot.mount                                 generated
dev-hugepages.mount                        static   
dev-mqueue.mount                           static   
home.mount                                 generated
proc-fs-nfsd.mount                         static   
proc-sys-fs-binfmt_misc.mount              static   
run-vmblock\x2dfuse.mount                  enabled  
sys-fs-fuse-connections.mount              static   
sys-kernel-config.mount                    static   
sys-kernel-debug.mount                     static   
tmp.mount                                  disabled 
var-lib-machines.mount                     static   
var-lib-nfs-rpc_pipefs.mount               static   
cups.path                                  enabled  
ostree-finalize-staged.path                disabled 
systemd-ask-password-console.path          static   
systemd-ask-password-plymouth.path         static   
systemd-ask-password-wall.path             static   
session-2.scope                            transient
accounts-daemon.service                    enabled  
alsa-restore.service                       static   
alsa-state.service                         static   
anaconda-direct.service                    static   
anaconda-fips.service                      static   
anaconda-nm-config.service                 static   
anaconda-noshell.service                   static   
anaconda-pre.service                       static   
anaconda-shell@.service                    static   
anaconda-sshd.service                      static   
anaconda-tmux@.service                     static   
anaconda.service                           static   
arp-ethers.service                         disabled 
atd.service                                enabled  
auditd.service                             enabled  
auth-rpcgss-module.service                 static   
autovt@.service                            enabled  
avahi-daemon.service                       enabled  
blivet.service                             static   
blk-availability.service                   disabled 
bluetooth.service                          enabled  
bolt.service                               static   
brltty.service                             disabled 
btattach-bcm@.service                      static   
canberra-system-bootup.service             disabled 
canberra-system-shutdown-reboot.service    disabled 
canberra-system-shutdown.service           disabled 
chrony-dnssrv@.service                     static   
chrony-wait.service                        disabled 
chronyd.service                            enabled  
cni-dhcp.service                           disabled 
cockpit-motd.service                       static   
cockpit-wsinstance-http.service            static   
cockpit-wsinstance-https-factory@.service  static   
cockpit-wsinstance-https@.service          static   
cockpit.service                            static   
colord.service                             static   
configure-printer@.service                 static   
console-getty.service                      disabled 
container-getty@.service                   static   
cpupower.service                           disabled 
crond.service                              enabled

以上回显字段STATE表示服务的状态:

状态

含义

enabled

开机自启

disabled

开机不自启

static

服务不能启动(服务自己不能启动,但是可以被其他服务激活)

masked

服务不能启动(除了服务本身不能启动外,也不能被其他服务激活)一般在服务之间发生冲突时,为了避免冲突的服务引起故障,将某一个服务彻底屏蔽所使用

2.3 服务的管理命令对比

RHEL7 以前

RHEL7 以后

功能

service httpd start

systemctl start httpd

启动服务

service httpd stop

systemctl stop  httpd

停止服务

service httpd restart

systemctl restart httpd

重启服务

service httpd reload

systemctl reload  httpd

重新载入服务的配置文件

chkconfig httpd on

systemctl enable  httpd

服务开机自启

chkconfig httpd off

systemctl disable  httpd

禁用服务开机自启

service httpd status

systemctl status httpd

查看服务的状态(开机和运行的状态)


systemctl is-enable httpd

查询服务的开机状态


systemctl is-active httpd

查询服务的当前状态


systemctl reload-or-restart  httpd

优先载入服务配置,如果不能载入配置,则重启服务


systemctl enable --now httpd

将服务设置为开机自启且当前立即启动


systemctl disable --now httpd

将禁用服务开机自启且当前立即停止服务


systemctl mask httpd

锁定一个服务(服务无法启动)


systemctl unmask httpd

解锁一个服务


systemctl list-dependencies httpd

查询服务的依赖关系

三、systemd启动目标

systemd中的启动目标是用于定义服务或系统单元如何启动的配置文件。它定义了服务或单元的启动顺序、依赖关系和启动参数。 systemd提供了多种启动目标类型,如default.target、multi-user.target、graphical.target等。这些目标类型代表了不同的系统状态和配置级别。

启动目标

功能

graphical.target

图形化桌面模式,系统支持多用户,图形和基于文本的登录

multi-user.target

多用户模式,系统支持多用户,基于文本的登录

rescue.target

救援模式,系统初始化已完成,需要root账户登录

emergency.target

紧急模式,只读挂载根文件系统,需要root账户登录

3.1 设置systemd启动目标

命令

作用

systemctl get-default

查询默认的启动目标

systemctl set-default multi-user.target

设置默认的启动目标

systemctl isolate  graphical.target

临时切换到其他的启动目标,等同于init x

在RHEL7 以前,systemd启动目标对应系统的运行级别,指系统在不同的运行状态下所处的级别。可以通过init命令临时切换运行级别,例如临时切换到图形化桌面环境,即“init 5”,一共有7个运行级别,分别为:

  • 运行级别1:单用户模式。在这个模式下,系统仅允许root用户登录,其他用户无法登录。此模式用于系统维护,例如在忘记root密码时,可以在此模式下修改密码。
  • 运行级别2:多用户模式,但不支持网络服务。在这个模式下,多个用户可以登录系统,但网络服务无法使用。
  • 运行级别3:完全多用户模式。在这个模式下,多个用户可以登录系统,且网络服务正常运行。这是Linux服务器常见的运行级别。
  • 运行级别4:多用户模式,但关闭网络服务。在这个模式下,多个用户可以登录系统,但网络服务被关闭。
  • 运行级别5:图形化桌面模式。
  • 运行级别6:重启模式。在这个模式下,系统会立即重启。
  • 运行级别0:关闭系统。在这个模式下,系统会关闭,立即关机。


运行级别的配置和切换可以通过修改/etc/inittab文件来实现。在这个文件中,可以设置系统的默认运行级别以及其他运行级别的配置。例如,可以将默认运行级别设置为3,即完全多用户模式。

需要注意的是,不同Linux发行版对运行级别的命名和功能可能存在差异。例如,在Debian系统中,运行级别1为单用户模式,运行级别3为完全多用户模式;而在Fedora系统中,运行级别1为单用户模式,运行级别3为多用户模式。

3.2 在系统启动时临时指定启动级别

操作步骤:

  1. 重启系统
  2. 将光标移动到要启动的内核
  3. 按e编译当前条目
  4. 将光标移至以linux开头的行,此为内核命令行
  5. 添加systemd.unit=NAME.target
  6. 按ctrl+x继续启动

例如:系统启动时,指定启动级别为多用户模式,仅当前这一次有效

systemd管理服务_运行级别_02