Merge pull request #10957 from way-ww/master

翻译完成20181015 How to Enable or Disable Services on Boot in Linux Using chkconfig and systemctl Command.md
This commit is contained in:
Xingyu.Wang 2018-10-30 19:12:22 +08:00 committed by GitHub
commit 83546afb14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 485 additions and 248 deletions

View File

@ -1,248 +0,0 @@
Translating by way-ww
How to Enable or Disable Services on Boot in Linux Using chkconfig and systemctl Command
======
Its a important topic for Linux admin (such a wonderful topic) so, everyone must be aware of this and practice how to use this in the efficient way.
In Linux, whenever we install any packages which has services or daemons. By default all the services “init & systemd” scripts will be added into it but it wont enabled.
Hence, we need to enable or disable the service manually if its required. There are three major init systems are available in Linux which are very famous and still in use.
### What is init System?
In Linux/Unix based operating systems, init (short for initialization) is the first process that started during the system boot up by the kernel.
Its holding a process id (PID) of 1. It will be running in the background continuously until the system is shut down.
Init looks at the `/etc/inittab` file to decide the Linux run level then it starts all other processes & applications in the background as per the run level.
BIOS, MBR, GRUB and Kernel processes were kicked up before hitting init process as part of Linux booting process.
Below are the available run levels for Linux (There are seven runlevels exist, from zero to six).
* **`0:`** halt
* **`1:`** Single user mode
* **`2:`** Multiuser, without NFS
* **`3:`** Full multiuser mode
* **`4:`** Unused
* **`5:`** X11 (GUI Graphical User Interface)
* **`:`** reboot
Below three init systems are widely used in Linux.
* System V (Sys V)
* Upstart
* systemd
### What is System V (Sys V)?
System V (Sys V) is one of the first and traditional init system for Unix like operating system. init is the first process that started during the system boot up by the kernel and its a parent process for everything.
Most of the Linux distributions started using traditional init system called System V (Sys V) first. Over the years, several replacement init systems were released to address design limitations in the standard versions such as launchd, the Service Management Facility, systemd and Upstart.
But systemd has been adopted by several major Linux distributions over the traditional SysV init systems.
### What is Upstart?
Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.
It was originally developed for the Ubuntu distribution, but is intended to be suitable for deployment in all Linux distributions as a replacement for the venerable System-V init.
It was used in Ubuntu from 9.10 to Ubuntu 14.10 & RHEL 6 based systems after that they are replaced with systemd.
### What is systemd?
Systemd is a new init system and system manager which was implemented/adapted into all the major Linux distributions over the traditional SysV init systems.
systemd is compatible with SysV and LSB init scripts. It can work as a drop-in replacement for sysvinit system. systemd is the first process get started by kernel and holding PID 1.
Its a parant process for everything and Fedora 15 is the first distribution which was adapted systemd instead of upstart. systemctl is command line utility and primary tool to manage the systemd daemons/services such as (start, restart, stop, enable, disable, reload & status).
systemd uses .service files Instead of bash scripts (SysVinit uses). systemd sorts all daemons into their own Linux cgroups and you can see the system hierarchy by exploring `/cgroup/systemd` file.
### How to Enable or Disable Services on Boot Using chkconfig Commmand?
The chkconfig utility is a command-line tool that allows you to specify in which
runlevel to start a selected service, as well as to list all available services along with their current setting.
Also, it will allows us to enable or disable a services from the boot. Make sure you must have superuser privileges (either root or sudo) to use this command.
All the services script are located on `/etc/rd.d/init.d`.
### How to list All Services in run-level
The `-list` parameter displays all the services along with their current status (What run-level the services are enabled or disabled).
```
# 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
.
.
```
### How to check the Status of Specific Service
If you would like to see a particular service status in run-level then use the following format and grep the required service.
In this case, we are going to check the `auditd` service status in run-level.
```
# chkconfig --list| grep auditd
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
```
### How to Enable a Particular Service on Run Levels
Use `--level` parameter to enable a service in the required run-level. In this case, we are going to enable `httpd` service on run-level 3 and 5.
```
# chkconfig --level 35 httpd on
```
### How to Disable a Particular Service on Run Levels
Use `--level` parameter to disable a service in the required run-level. In this case, we are going to enable `httpd` service on run-level 3 and 5.
```
# chkconfig --level 35 httpd off
```
### How to Add a new Service to the Startup List
The `-add` parameter allows us to add any new service to the startup. By default, it will turn on level 2, 3, 4 and 5 automatically for that service.
```
# chkconfig --add nagios
```
### How to Remove a Service from Startup List
Use `--del` parameter to remove the service from the startup list. Here, we are going to remove the Nagios service from the startup list.
```
# chkconfig --del nagios
```
### How to Enable or Disable Services on Boot Using systemctl Command?
systemctl is command line utility and primary tool to manage the systemd daemons/services such as (start, restart, stop, enable, disable, reload & status).
All the created systemd unit files are located on `/etc/systemd/system/`.
### How to list All Services
Use the following command to list all the services which included enabled and disabled.
```
# systemctl list-unit-files --type=service
UNIT FILE STATE
arp-ethers.service disabled
auditd.service enabled
[email protected] enabled
blk-availability.service disabled
brandbot.service static
[email protected] 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
[email protected] static
cpupower.service disabled
crond.service enabled
.
.
150 unit files listed.
```
If you would like to see a particular service status then use the following format and grep the required service. In this case, we are going to check the `httpd` service status.
```
# systemctl list-unit-files --type=service | grep httpd
httpd.service disabled
```
### How to Enable a Particular Service on boot
Use the following systemctl command format to enable a particular service. To enable a service, it will create a symlink. The same can be found below.
```
# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
```
Run the following command to double check whether the services is enabled or not on boot.
```
# systemctl is-enabled httpd
enabled
```
### How to Disable a Particular Service on boot
Use the following systemctl command format to disable a particular service. When you run the command, it will remove a symlink which was created by you while enabling the service. The same can be found below.
```
# systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
```
Run the following command to double check whether the services is disabled or not on boot.
```
# systemctl is-enabled httpd
disabled
```
### How to Check the current run level
Use the following systemctl command to verify which run-level you are in. Still “runlevel” command works with systemd, however runlevels is a legacy concept in systemd so, i would advise you to use systemctl command for all activity.
We are in `run-level 3`, the same is showing below as `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
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/how-to-enable-or-disable-services-on-boot-in-linux-using-chkconfig-and-systemctl-command/
作者:[Prakash Subramanian][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.2daygeek.com/author/prakash/
[b]: https://github.com/lujun9972

View File

@ -0,0 +1,485 @@
如何使用chkconfig和systemctl命令启用或禁用linux服务
======
对于Linux管理员来说这是一个重要美妙的话题所以每个人都必须知道并练习怎样才能更高效的使用它们。
在Linux中无论何时当你安装任何带有服务和守护进程的包系统默认会把这些进程添加到 “init & systemd” 脚本中,不过此时它们并没有被启动 。
我们需要手动的开启或者关闭那些服务。Linux中有三个著名的且一直在被使用的init系统。
### 什么是init系统
在以Linux/Unix 为基础的操作系统上init (初始化的简称) 是内核引导系统启动过程中第一个启动的进程。
init的进程id(pid)是1除非系统关机否则它将会一直在后台运行。
Init 首先根据 `/etc/inittab` 文件决定Linux运行的级别然后根据运行级别在后台启动所有其他进程和应用程序。
BIOS, MBR, GRUB 和内核程序在启动init之前就作为linux的引导程序的一部分开始工作了。
下面是Linux中可以使用的运行级别从06总共七个运行级别
* **`0:`** 关机
* **`1:`** 单用户模式
* **`2:`** 多用户模式没有NFS
* **`3:`** 完全的多用户模式
* **`4:`** 系统未使用
* **`5:`** 图形界面模式
* **`:`** 重启
下面是Linux系统中最常用的三个init系统
* System V (Sys V)
* Upstart
* systemd
### 什么是 System V (Sys V)?
System V (Sys V)是类Unix系统第一个传统的init系统之一。init是内核引导系统启动过程中第一支启动的程序 ,它是所有程序的父进程。
大部分Linux发行版最开始使用的是叫作System VSys V的传统的init系统。在过去的几年中已经有好几个init系统被发布用来解决标准版本中的设计限制例如launchd, the Service Management Facility, systemd 和 Upstart。
与传统的 SysV init系统相比systemd已经被几个主要的Linux发行版所采用。
### 什么是 Upstart?
Upstart 是一个基于事件的/sbin/init守护进程的替代品它在系统启动过程中处理任务和服务的启动在系统运行期间监视它们在系统关机的时候关闭它们。
它最初是为Ubuntu而设计但是它也能够完美的部署在其他所有Linux系统中用来代替古老的System-V。
Upstart被用于Ubuntu 从 9.10 到 Ubuntu 14.10和基于RHEL 6的系统之后它被systemd取代。
### 什么是 systemd?
Systemd是一个新的init系统和系统管理器 和传统的SysV相比它可以用于所有主要的Linux发行版。
systemd 兼容 SysV 和 LSB init脚本。 它可以直接替代Sys V init系统。systemd是被内核启动的第一支程序它的PID 是1。
systemd是所有程序的父进程Fedora 15 是第一个用systemd取代upstart的发行版。systemctl用于命令行它是管理systemd的守护进程/服务的主要工具,例如:(开启,重启,关闭,启用,禁用,重载和状态)
systemd 使用.service 文件而不是bash脚本 (SysVinit 使用的). systemd将所有守护进程添加到cgroups中排序你可以通过浏览`/cgroup/systemd` 文件查看系统等级。
### 如何使用chkconfig命令启用或禁用引导服务?
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`参数允许我们添加任何信服务到启动列表中, 默认情况下新添加的服务会在运行级别2345下自动开启。
```
# chkconfig --add nagios
```
### 如何从启动列表中删除服务
可以使用 `--del` 参数从启动列表中删除服务下面展示的事如何从启动列表中删除Nagios服务。
```
# chkconfig --del nagios
```
### 如何使用systemctl命令启用或禁用开机自启服务
systemctl用于命令行它是一个基础工具用来管理systemd的守护进程/服务,例如:(开启,重启,关闭,启用,禁用,重载和状态)
所有服务创建的unit文件位与`/etc/systemd/system/`.
### 如何列出全部的服务
使用下面的命令列出全部的服务(包括启用的和禁用的)
```
# systemctl list-unit-files --type=service
UNIT FILE STATE
arp-ethers.service disabled
auditd.service enabled
[email protected] enabled
blk-availability.service disabled
brandbot.service static
[email protected] 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
[email protected] 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命令确认你系统当前的运行级别'运行级'别仍然由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
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/how-to-enable-or-disable-services-on-boot-in-linux-using-chkconfig-and-systemctl-command/
作者:[Prakash Subramanian][a]
选题:[lujun9972][b]
译者:[way-ww](https://github.com/way-ww)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.2daygeek.com/author/prakash/
[b]: https://github.com/lujun9972