📜  Linux管理员-使用systemctl进行资源管理(1)

📅  最后修改于: 2023-12-03 15:32:42.867000             🧑  作者: Mango

Linux管理员-使用systemctl进行资源管理

简介

在Linux系统中,systemd是一个系统和服务管理器。systemctl是与systemd来进行交互的命令行工具,用来管理systemd系统守护进程的状态,以及systemd单元和服务的启动、停止、启用和禁用等操作。

本文将介绍如何使用systemctl进行资源管理。

systemctl命令
查看系统中所有服务的状态

使用systemctl命令,不带任何参数,可以查看系统中所有服务的状态。例如:

systemctl

输出结果类似于:

  UNIT                                                                        LOAD      ACTIVE     SUB          DESCRIPTION
  proc-sys-fs-binfmt_misc.automount                                            loaded    active     waiting      Arbitrary Executable File Formats File System Automount Point
  sys-devices-pci0000:00-0000:00:04.0-virtio0-block-vda.vda1.device            loaded    active     plugged      /sys/devices/pci0000:00/0000:00:04.0/virtio0/block/vda/vda1
  sys-devices-pci0000:00-0000:00:04.0-virtio0-net-enp0s4.device                loaded    active     plugged      /sys/devices/pci0000:00/0000:00:04.0/virtio0/net/enp0s4
  sys-devices-pci0000:00-0000:00:05.0-virtio0-input-input0.event0.device      loaded    active     plugged      /sys/devices/pci0000:00/0000:00:05.0/virtio0/input/input0/event0
  sys-devices-pci0000:00-0000:00:05.0-virtio0-input-input0.device             loaded    active     plugged      /sys/devices/pci0000:00/0000:00:05.0/virtio0/input/input0
  sys-devices-pci0000:00-0000:00:06.0-virtio0-virtio1-input-input0.event0.device loaded    active     plugged      /sys/devices/pci0000:00/0000:00:06.0/virtio0/virtio1/input/input0/event0
  sys-devices-pci0000:00-0000:00:06.0-virtio0-virtio1-input-input0.device    loaded    active     plugged      /sys/devices/pci0000:00/0000:00:06.0/virtio0/virtio1/input/input0
  sys-devices-pci0000:00-0000:00:06.0-virtio0-virtport1p1.device              loaded    active     plugged      /sys/devices/pci0000:00/0000:00:06.0/virtio0/virtport1p1
  systemd-ask-password-console.path                                           loaded    active     waiting      Dispatch Password Requests to Console Directory Watch
  systemd-ask-password-wall.path                                              loaded    active     waiting      Forward Password Requests to Wall Directory Watch
  init.scope                                                                  loaded    active     running      System and Service Manager
  session-1.scope                                                             loaded    active     running      Session 1 of user root
  -:pam_unix(sudo:session):session-2                                          loaded    active     running      pam_unix(sudo:session): session 2 opened for user root by (uid=0)
  cronie.service                                                              loaded    active     running      Periodic Command Scheduler
  dbus.service                                                                loaded    active     running      D-Bus System Message Bus
  firewalld.service                                                           loaded    active     running      firewalld - dynamic firewall daemon
  kdump.service                                                               loaded    active     exited       Crash recovery kernel arming
  kmod-static-nodes.service                                                   loaded    active     exited       Create list of static device nodes for the current kernel
  network.service                                                             loaded    active     exited       LSB: Bring up/down networking
  rsyslog.service                                                             loaded    active     running      System Logging Service
  sshd.service                                                                loaded    active     running      OpenSSH server daemon
  systemd-journald.service                                                    loaded    active     running      Journal Service
  systemd-logind.service                                                      loaded    active     running      Login Service
  systemd-udevd.service                                                       loaded    active     running      udev Kernel Device Manager
  tuned.service                                                               loaded    active     running      Dynamic System Tuning Daemon
  upower.service                                                              loaded    active     running      Daemon for power management
  yum-cron.service                                                            loaded    active     running      Run automatic yum updates as a cron job
  crond.service                                                               loaded    inactive   dead         Regular background program processing daemon
  getty@tty1.service                                                          loaded    inactive   dead         Getty on tty1
  rc-local.service                                                            loaded    inactive   dead         /etc/rc.local Compatibility
  systemd-journal-flush.service                                               loaded    inactive   dead         Flush Journal to Persistent Storage
  systemd-remount-fs.service                                                   loaded    inactive   dead         Remount Root and Kernel File Systems
查看单个服务的状态

使用systemctl命令,加上参数servicename(指定服务名称),可以查看单个服务的状态。例如:

systemctl status cronie.service

输出结果类似于:

● cronie.service - Periodic Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/cronie.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2021-07-30 10:30:29 CST; 2 days ago
 Main PID: 815 (crond)
   CGroup: /system.slice/cronie.service
           └─815 /usr/sbin/crond -n

Jul 30 10:30:29 localhost.localdomain systemd[1]: Starting Periodic Command Scheduler...
Jul 30 10:30:29 localhost.localdomain crond[815]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 74% if used.)
Jul 30 10:30:29 localhost.localdomain systemd[1]: Started Periodic Command Scheduler.
启动单个服务

使用systemctl命令,加上参数start和servicename(指定服务名称),可以启动单个服务。例如:

systemctl start cronie.service
停止单个服务

使用systemctl命令,加上参数stop和servicename(指定服务名称),可以停止单个服务。例如:

systemctl stop cronie.service
重启单个服务

使用systemctl命令,加上参数restart和servicename(指定服务名称),可以重启单个服务。例如:

systemctl restart cronie.service
设置单个服务开机自启动

使用systemctl命令,加上参数enable和servicename(指定服务名称),可以设置单个服务开机自启动。例如:

systemctl enable cronie.service
设置单个服务禁止开机自启动

使用systemctl命令,加上参数disable和servicename(指定服务名称),可以设置单个服务禁止开机自启动。例如:

systemctl disable cronie.service
总结

使用systemctl命令,可以管理系统中的所有服务,包括查询服务状态、启动、停止、重启、开机自启动和禁止开机自启动等操作。可以帮助Linux管理员更高效地进行资源管理。