对于 Linux 管理员来讲这是一个重要(美妙)的话题,因此每一个人都必须知道,并练习怎样才能更高效的使用它们。linux
在 Linux 中,不管什么时候当你安装任何带有服务和守护进程的包,系统默认会把这些服务的初始化及 systemd 脚本添加进去,不过此时它们并无被启用。ios
咱们须要手动的开启或者关闭那些服务。Linux 中有三个著名的且一直在被使用的初始化系统。正则表达式
在以 Linux/Unix 为基础的操做系统上,init (初始化的简称) 是内核引导系统启动过程当中第一个启动的进程。shell
init 的进程 id (pid)是 1,除非系统关机不然它将会一直在后台运行。bash
init 首先根据 /etc/inittab 文件决定 Linux 运行的级别,而后根据运行级别在后台启动全部其余进程和应用程序。socket
BIOS、MBR、GRUB 和内核程序在启动 init 以前就做为 Linux 的引导程序的一部分开始工做了。工具
下面是 Linux 中可使用的运行级别(从 0~6 总共七个运行级别):操作系统
下面是 Linux 系统中最经常使用的三个初始化系统:命令行
System V(Sys V)是类 Unix 系统第一个也是传统的初始化系统。init 是内核引导系统启动过程当中第一支启动的程序,它是全部程序的父进程。ssr
大部分 Linux 发行版最开始使用的是叫做 System V(Sys V)的传统的初始化系统。在过去的几年中,已经发布了好几个初始化系统以解决标准版本中的设计限制,例如:launchd、Service Management Facility、systemd 和 Upstart。
可是 systemd 已经被几个主要的 Linux 发行版所采用,以取代传统的 SysV 初始化系统。
Upstart 是一个基于事件的 /sbin/init 守护进程的替代品,它在系统启动过程当中处理任务和服务的启动,在系统运行期间监视它们,在系统关机的时候关闭它们。
它最初是为 Ubuntu 而设计,可是它也可以完美的部署在其余全部 Linux系统中,用来代替古老的 System-V。
Upstart 被用于 Ubuntu 从 9.10 到 Ubuntu 14.10 和基于 RHEL 6 的系统,以后它被 systemd 取代。
systemd 是一个新的初始化系统和系统管理器,它被用于全部主要的 Linux 发行版,以取代传统的 SysV 初始化系统。
systemd 兼容 SysV 和 LSB 初始化脚本。它能够直接替代 SysV 初始化系统。systemd 是被内核启动的第一个程序,它的 PID 是 1。
systemd 是全部程序的父进程,Fedora 15 是第一个用 systemd 取代 upstart 的发行版。systemctl 用于命令行,它是管理 systemd 的守护进程/服务的主要工具,例如:(开启、重启、关闭、启用、禁用、重载和状态)
systemd 使用 .service 文件而不是 bash 脚本(SysVinit 使用的)。systemd 将全部守护进程添加到 cgroups 中排序,你能够经过浏览 /cgroup/systemd 文件查看系统等级。
chkconfig 实用程序是一个命令行工具,容许你在指定运行级别下启动所选服务,以及列出全部可用服务及其当前设置。
此外,它还容许咱们从启动中启用或禁用服务。前提是你有超级管理员权限(root 或者 sudo)运行这个命令。
全部的服务脚本位于 /etc/rd.d/init.d文件中
--list 参数会展现全部的服务及其当前状态(启用或禁用服务的运行级别):
# chkconfig --list NetworkManager 0:off 1:off 2:on 3:on 4:on 5:on 6:off abrt-ccpp 0:off 1:off 2:off 3:on 4:off 5:on 6:off abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off . .
若是你想查看运行级别下某个服务的状态,你可使用下面的格式匹配出须要的服务。
好比说我想查看运行级别中 auditd 服务的状态
# chkconfig --list| grep auditd auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
使用 --level 参数启用指定运行级别下的某个服务,下面展现如何在运行级别 3 和运行级别 5 下启用 httpd 服务。
# chkconfig --level 35 httpd on
一样使用 --level 参数禁用指定运行级别下的服务,下面展现的是在运行级别 3 和运行级别 5 中禁用 httpd 服务。
# chkconfig --level 35 httpd off
-–add 参数容许咱们添加任何新的服务到启动列表中,默认状况下,新添加的服务会在运行级别 二、三、四、5 下自动开启。 # chkconfig --add nagios
可使用 --del 参数从启动列表中删除服务,下面展现的是如何从启动列表中删除 Nagios 服务。
# chkconfig --del nagios
systemctl 用于命令行,它是一个用来管理 systemd 的守护进程/服务的基础工具,例如:(开启、重启、关闭、启用、禁用、重载和状态)。
全部服务建立的 unit 文件位与 /etc/systemd/system/。
使用下面的命令列出所有的服务(包括启用的和禁用的)。
# systemctl list-unit-files --type=service UNIT FILE STATE arp-ethers.service disabled auditd.service enabled autovt@.service enabled blk-availability.service disabled brandbot.service static chrony-dnssrv@.service static chrony-wait.service disabled chronyd.service enabled cloud-config.service enabled cloud-final.service enabled cloud-init-local.service enabled cloud-init.service enabled console-getty.service disabled console-shell.service disabled container-getty@.service static cpupower.service disabled crond.service enabled . . 150 unit files listed.
使用下面的格式经过正则表达式匹配出你想要查看的服务的当前状态。下面是使用 systemctl 命令查看 httpd 服务的状态。
# systemctl list-unit-files --type=service | grep httpd httpd.service disabled
使用下面格式的 systemctl 命令启用一个指定的服务。启用服务将会建立一个符号连接,以下可见:
# systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
运行下列命令再次确认服务是否被启用。
# systemctl is-enabled httpd enabled
运行下面的命令禁用服务将会移除你启用服务时所建立的符号连接。
# systemctl disable httpd Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
运行下面的命令再次确认服务是否被禁用。
# systemctl is-enabled httpd disabled
使用 systemctl 命令确认你系统当前的运行级别,runlevel 命令仍然可在 systemd 下工做,不过,运行级别对于 systemd 来讲是一个历史遗留的概念。因此我建议你所有使用 systemctl 命令。
咱们当前处于运行级别 3, 它等同于下面显示的 multi-user.target。
# systemctl list-units --type=target UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System cloud-config.target loaded active active Cloud-config availability cryptsetup.target loaded active active Local Encrypted Volumes getty.target loaded active active Login Prompts local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User System network-online.target loaded active active Network is Online network-pre.target loaded active active Network (Pre) network.target loaded active active Network paths.target loaded active active Paths remote-fs.target loaded active active Remote File Systems slices.target loaded active active Slices sockets.target loaded active active Sockets swap.target loaded active active Swap sysinit.target loaded active active System Initialization timers.target loaded active active Timers