Use the chkconfig and systemctl commands to enable or disable the Linux service

  • 2020-12-19 21:25:20
  • OfStack

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, the system adds the initialization of those services and systemd scripts by default, but they are not enabled at this time.

We need to turn those services on and off manually. Linux has three well-known and 1 - straight initialization systems in use.

What is an initialization system?

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

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

init first determines the level of Linux to run based on the /etc/inittab file, and then launches all other processes and applications in the background based on the level of run.

The BIOS, MBR, GRUB, and kernel programs start working as part 1 of the Linux boot program before init is started.

Here are the runlevels available in Linux (a total of 7 runlevels from 0 to 6) :

0: turn it off
1: Single user mode
2: Multi-user mode (without NFS)
3: Full multi-user mode
4. The system is not in use
5: Graphical interface mode
6: restart

Here are the three most commonly used initialization systems in the Linux system:

System V (Sys V) Upstart systemd

What is System V (Sys V) ?

System V (Sys V) is the first and traditional initialization system of class Unix. init is the first program to be started during the kernel boot process and is the parent of all programs.

Most Linux releases start with a traditional initialization system called System V (Sys V). Over the past few years, several initialization systems have been released to address design limitations in standard versions, such as launchd, Service Management Facility, systemd, and Upstart.

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

What is a Upstart & # 63;

Upstart is an alternative to the event-based /sbin/init daemon, which handles the startup of tasks and services during system startup, monitors them during system run, and shuts them down when the system is shut down.

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

Upstart was used on Ubuntu systems from 9.10 to Ubuntu 14.10 and based on RHEL 6 before it was replaced by systemd.

What is a systemd & # 63;

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

systemd is compatible with SysV and LSB initialization scripts. It can directly replace the SysV initialization system. systemd is the first program to be started by the kernel, and its PID is 1.

systemd is the parent of all programs, and Fedora 15 was the first release to replace upstart with systemd. systemctl is used on the command line and is the primary tool for managing systemd's daemons/services, such as :(on, restart, close, enabled, disabled, overload, and status)

systemd uses.service files instead of the bash script (used by SysVinit). systemd adds all daemons to cgroups to sort, and you can view the system level by browsing /cgroup/systemd files.

How to use the chkconfig command to enable or disable boot services ?

The chkconfig utility is a command-line tool that allows you to start selected services at a specified run level and list all available services and their current Settings.

In addition, it allows us to enable or disable services from startup. If you have super admin privileges (root or sudo) to run this command.

All of the service scripts in the/etc/rd d/init d file

How do I list all the services at the run level

-- The list parameter shows all of the services and their current status (run level with the service 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 do I view the status of a specified service

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

Let's say I want to see the status of an auditd service at run level


# chkconfig --list| grep auditd
auditd    0:off 1:off 2:on 3:on 4:on 5:on 6:off

How do I enable a service at a specified run level

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


# chkconfig --level 35 httpd on

How do I disable a service at a specified run level

The --level parameter is also used to disable services at specified run levels, as shown below with httpd services disabled at run levels 3 and 5.


# chkconfig --level 35 httpd off

How do I add a new service to the startup list

The add parameter allows us to add any new services to the startup list. By default, the new services are automatically started at runlevels 2, 3, 4, and 5.


# chkconfig --add nagios

How do I remove a service from the startup list

The --del parameter can be used to remove the service from the startup list, and the following shows how to remove the Nagios service from the startup list.


# chkconfig --del nagios

How do I enable or disable startup services using the systemctl command?

systemctl is used on the command line and is a basic tool for managing systemd's daemons/services, such as :(on, restart, close, enable, disable, overload, and status).

All service create unit file with/etc systemd/system /.

How do I list all the services

Use the following command to list all the services (enabled and disabled).


# 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.

Use the following format to match the current state of the service you want to view with a regular expression. Here's how to view the status of the httpd service using the systemctl command.


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

How do I make the specified service boot up

Enable 1 specified service using the systemctl command in the following format. Enabling the service will create a symbolic link as follows:


# 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 reconfirm that the service is enabled.


# systemctl is-enabled httpd
enabled

How do I disable a specified service

Disabling the service by running the following command will remove the symbolic link you 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 reconfirm that the service is disabled.


# chkconfig --level 35 httpd on
0

How do I view the current health level of my system

Use the systemctl command to verify your system's current runlevel. The runlevel command still works under systemd, but runlevels are a legacy concept for systemd. So I suggest you use the systemctl command entirely.

We are currently at run level 3, which is equivalent to ES281en-ES282en.target as shown below.


# chkconfig --level 35 httpd on
1

conclusion


Related articles: