如何在Linux列出启动时启动的服务

时间:2020-03-21 11:44:44  来源:igfitidea点击:

默认情况下,系统boot子时会自动启动一些重要的系统服务。
例如,NetworkManager和Firewalld服务将在系统启动时自动启动。
启动服务也称为Linux和Unix的操作系统中的守护程序。
他们将继续在背景中运行,并在没有任何用户干预的情况下进行工作。
除系统服务外,还有其他一些第三方应用程序还将自己添加到启动。
在此简要教程中,让我们了解如何在Linux和Unix系统中的启动时查找和列出启动服务。

列出在Linux的引导下启动的服务

查找启动服务列表将根据"init"系统而有所不同。
Systemd是Linux发行版主要较新版本的默认初始系统。

如果系统与"SystemD"System Manager一起运行,则可以使用以下命令列出所有服务:

$sudo systemctl list-unit-files --type=service

示例输出:

UNIT FILE                                  STATE           VENDOR PRESET
accounts-daemon.service                    enabled         enabled      
acpid.service                              disabled        enabled      
alsa-restore.service                       static          enabled      
alsa-state.service                         static          enabled      
alsa-utils.service                         masked          enabled      
anacron.service                            enabled         enabled      
apparmor.service                           enabled         enabled      
apport-autoreport.service                  static          enabled      
Hyman@theitroad                    static          enabled      
apport.service                             generated       enabled      
.
.
.
Hyman@theitroad                 static          enabled      
whoopsie.service                           disabled        enabled      
Hyman@theitroad            disabled        enabled      
Hyman@theitroad              disabled        enabled      
wpa_supplicant.service                     enabled         enabled      
Hyman@theitroad                    disabled        enabled      
x11-common.service                         masked          enabled      
Hyman@theitroad                         static          enabled      
xfs_scrub_all.service                      static          enabled      
Hyman@theitroad                    static          enabled      
265 unit files listed.

如上所述,此命令显示Linux系统中的所有服务(在系统引导时启用和禁用)的列表。
我们可以通过在上述输出中的状态部分查看它来验证它。
以启动启动的服务标记为已启用,并且未启动的服务标记为已禁用。

仅列出System Boot的已启用的服务,运行:

$sudo systemctl list-unit-files --type=service --state=enabled --all

示例输出:

UNIT FILE                                  STATE   VENDOR PRESET
accounts-daemon.service                    enabled enabled      
anacron.service                            enabled enabled      
apparmor.service                           enabled enabled      
Hyman@theitroad                            enabled enabled      
avahi-daemon.service                       enabled enabled      
.
.
.
udisks2.service                            enabled enabled      
ufw.service                                enabled enabled      
unattended-upgrades.service                enabled enabled      
vboxweb.service                            enabled enabled      
wpa_supplicant.service                     enabled enabled      
74 unit files listed.

要列出系统引导的所有禁用服务,请运行:

$sudo systemctl list-unit-files --type=service --state=disabled --all

就像我已经说过,一些旧的Linux发行版可以使用Sysv或者Upstart作为默认Init系统。

如果系统使用'sysv',请运行以下命令以列出所有服务:

$sudo service --status-all

示例输出:

[ + ]  acpid
 [ - ]  alsa-utils
 [ - ]  anacron
 [ + ]  apparmor
 [ + ]  apport
 [ + ]  avahi-daemon
 [ + ]  bluetooth
 [ - ]  console-setup.sh
 [ + ]  cron
 [ - ]  cryptdisks
 [ - ]  cryptdisks-early
 [ + ]  cups
 [ + ]  cups-browsed
 [ + ]  dbus
 [ - ]  dns-clean
 [ + ]  dnsmasq
 [ + ]  exim4
 [ + ]  gdm3
 [ + ]  grub-common
 [ + ]  hddtemp
 [ - ]  hwclock.sh
 [ + ]  irqbalance
 [ + ]  kerneloops
 [ - ]  keyboard-setup.sh
 [ + ]  kmod
 [ + ]  lm-sensors
 [ - ]  lvm2
 [ - ]  lvm2-lvmpolld
 [ + ]  network-manager
 [ + ]  networking
 [ + ]  openvpn
 [ - ]  plymouth
 [ - ]  plymouth-log
 [ - ]  pppd-dns
 [ + ]  procps
 [ - ]  pulseaudio-enable-autospawn
 [ - ]  rsync
 [ + ]  rsyslog
 [ - ]  saned
 [ - ]  screen-cleanup
 [ + ]  smartmontools
 [ - ]  speech-dispatcher
 [ - ]  spice-vdagent
 [ + ]  sysstat
 [ + ]  udev
 [ + ]  ufw
 [ + ]  unattended-upgrades
 [ - ]  uuidd
 [ + ]  virtualbox
 [ - ]  whoopsie
 [ - ]  x11-common

其中'+'表示服务正在运行," - "表示停止的服务。
如果你看到 '?'在输出中,无法确定服务状态(出于某种原因)。

要列出在引导时启用的所有服务,请运行:

$sudo chkconfig --list

此命令将列出每个运行级别的每个服务的状态。

上述命令的示例输出将是:

acpid           0:off   1:off   2:on    3:on    4:on    5:on    6:off
anamon          0:off   1:off   2:off   3:off   4:off   5:off   6:off
atd             0:off   1:off   2:off   3:on    4:on    5:on    6:off
[...]

在上面的命令中,"开"表示服务在启动时启动。

我们还可以在下面的不同运行级别查看特定服务的状态:

$sudo chkconfig --list httpd

如果Linux系统使用"upstart",请运行此命令以列出所有启动服务:

$sudo initctl list

上面的命令将显示所有会话作业。

如果要显示所有系统作业,请运行:

$sudo initctl --system list

要列出所有服务并在每个运行级别显示其状态,请运行:

$sudo initctl list | awk '{ print  }' | xargs -n1 initctl show-config

要显示特定服务的状态,请运行此命令:

$initctl show-config <service_name>

禁用启动服务

我们在计算机上安装的应用程序越多,系统需要越长就才能启动。
为了提高Linux系统的启动时间,我们需要查找不必要的服务并禁用它们。

例如,如果我们不希望在启动时加载"无人值守升级"的服务,则可以使用命令禁用它:

$sudo systemctl disable --now unattended-upgrades.service

要知道在启动时启用了服务,请运行:

$sudo systemctl is-enabled <service-name>