How to Enable or Disable Linux Services Using chkconfig and systemctl Commands

How to Enable or Disable Linux Services Using chkconfig and systemctl Commands

This is an important (and wonderful) topic for Linux administrators, so everyone must know and practice how to use them more effectively.

In Linux, whenever you install any package with services and daemons, by default the initialization and systemd scripts for those services are added, but they are not enabled at that time.

We need to manually turn on or off those services. There are three well-known init systems in Linux that are still in use.

What is the init system?

On Linux/Unix based operating systems, init (short for initialization) is the first process started during the kernel boot system startup process.

The process id (pid) of init is 1, and it will run in the background until the system is shut down.

init first determines the level at which Linux is running based on the /etc/inittab file, and then starts all other processes and applications in the background based on the run level.

The BIOS, MBR, GRUB, and kernel programs start working as part of the Linux boot process before starting init.

The following are the run levels available in Linux (there are seven run levels from 0 to 6):

0: Shutdown
1: Single-user mode
2: Multi-user mode (without NFS)
3: Full multi-user mode
4: System not in use
5: Graphical interface mode
6: Restart

The following are the three most commonly used init systems in Linux systems:

  • System V (Sys V)
  • Upstart
  • systemd

What is System V (Sys V)?

System V (Sys V) is the first and traditional init system for Unix-like systems. init is the first program started during the kernel boot system startup process. It is the parent process of all programs.

Most Linux distributions originally used the traditional init system called System V (Sys V). Over the past few years, several init systems have been released to address design limitations in the standard distribution, such as launchd, Service Management Facility, systemd, and Upstart.

But systemd has been adopted by several major Linux distributions to replace the traditional SysV init system.

What is Upstart?

Upstart is an event-based replacement for the /sbin/init daemon that handles starting tasks and services during the system boot process, monitoring them while the system is running, and shutting them down at system shutdown.

It was originally designed for Ubuntu, but it can also be perfectly deployed in all other Linux systems to replace the ancient System-V.

Upstart was used in Ubuntu from 9.10 to Ubuntu 14.10 and RHEL 6-based systems, after which it was replaced by systemd.

What is systemd?

systemd is a new init system and system manager that is used in all major Linux distributions to replace the traditional SysV init system.

systemd is compatible with SysV and LSB init scripts. It is a direct replacement for the SysV init system. systemd is the first program started by the kernel and its PID is 1.

systemd is the parent process of all programs, and Fedora 15 is the first distribution to use systemd instead of upstart. systemctl is used for command line and it is the main tool for managing systemd daemons/services, such as: (start, restart, shutdown, enable, disable, reload and status)

systemd uses .service files instead of bash scripts (which SysVinit uses). systemd adds all daemons to cgroups in order, you can view the system hierarchy by browsing the /cgroup/systemd file.

How to enable or disable boot services using chkconfig command?

The chkconfig utility is a command-line tool that allows you to start selected services at a specified runlevel, as well as list all available services and their current settings.

In addition, it allows us to enable or disable services from startup. The prerequisite is that you have super administrator privileges (root or sudo) to run this command.

All service scripts are located in the /etc/rd.d/init.d file

How to list all services in a runlevel

The --list option will show all services and their current status (runlevels at which the service is 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 view the status of a specified service

If you want to check the status of a service in a run level, you can use the following format to match the required service.

For example, I want to check the status of the auditd service in the 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 service in a specific runlevel

Use the --level parameter to enable a service at a specified run level. The following shows how to enable the httpd service at run levels 3 and 5.

# chkconfig --level 35 httpd on

How to disable a service at a specific runlevel

Also use the --level parameter to disable services at the specified run level. The following shows how to disable the httpd service at run level 3 and run level 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 services to the startup list. By default, newly added services will be automatically started in run levels 2, 3, 4, and 5.

# chkconfig --add nagios

How to remove a service from the startup list

You can use the --del parameter to delete a service from the startup list. The following shows how to delete a Nagios service from the startup list.

# chkconfig --del nagios

How to use systemctl command to enable or disable the automatic startup service?

systemctl is a command line tool used to manage systemd daemons/services, such as (start, restart, stop, enable, disable, reload and status).

The unit files created by all services are located in /etc/systemd/system/.

How to list all services

Use the following command to list all services (both 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.

Use the following format to match the current status of the service you want to view using a regular expression. The following uses the systemctl command to view the status of the httpd service.

# systemctl list-unit-files --type=service | grep httpd
httpd.service disabled

How to enable a specified service to start automatically at boot

Use the following format of systemctl command to enable a specific service. Enabling the service will create a symbolic link, as shown 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 confirm that the service is enabled.

# systemctl is-enabled httpd
enabled

How to disable a specific service

Disabling the service by running the following command will remove the symbolic link created when you enabled the service.

# systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.

Run the following command to confirm whether the service is disabled.

# systemctl is-enabled httpd
disabled

How to view the current run level of the system

Use the systemctl command to confirm the current runlevel of your system. The runlevel command still works under systemd, however, runlevel is a historical concept for systemd. So I suggest you use systemctl command for everything.

We are currently in runlevel 3, which is equivalent to the multi-user.target shown below.

# 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

Summarize

The above is the method I introduced to you to enable or disable Linux services using chkconfig and systemctl commands. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Detailed explanation of chkconfig command under Linux
  • Linux command detailed explanation of chkconfig command usage
  • Use of Linux chkconfig command

<<:  JS implements request dispatcher

>>:  CentOS7 64-bit installation mysql graphic tutorial

Recommend

Detailed explanation of nginx shared memory mechanism

Nginx's shared memory is one of the main reas...

About the role of meta in HTML (collected and sorted from the Internet)

W3Cschool explains it this way The <meta> el...

Analysis of the process of deploying Python applications in Docker containers

Simple application deployment 1. Directory struct...

Solution to Element-ui upload file upload restriction

question Adding the type of uploaded file in acce...

Detailed explanation of various usages of proxy_pass in nginx

Table of contents Proxy forwarding rules The firs...

How to compile and install opencv under ubuntu

Easy installation of opencv2: conda install --cha...

Example of how to check the capacity of MySQL database table

This article introduces the command statements fo...

JavaScript realizes the effect of mobile modal box

This article example shares the specific code of ...

About Zabbix custom monitoring items and triggers

Table of contents 1. Monitoring port Relationship...

Why can't the MP4 format video embedded in HTML be played?

The following code is in my test.html. The video c...

How to prompt and open hyperlink a

<br />The countless information on the Inter...

How to use mysqldump to backup MySQL data

1. Introduction to mysqldump mysqldump is a logic...

Detailed explanation of MySQL delayed replication library method

Simply put, delayed replication is to set a fixed...

Detailed explanation of Vue's methods and properties

Vue methods and properties 1. Methods Usage 1 met...