Linux chkconfig Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux chkconfig command and its practical applications. The lab covers understanding the purpose and usage of the chkconfig command, configuring service startup behavior, and managing service startup levels. We will learn how to enable, disable, and customize the startup behavior of system services, which is crucial for improving system performance and security. The lab provides hands-on examples and step-by-step guidance to help you effectively manage the startup of services in your Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") subgraph Lab Skills linux/service -.-> lab-422597{{"`Linux chkconfig Command with Practical Examples`"}} end

Understand the Purpose and Usage of chkconfig Command

In this step, we will explore the purpose and usage of the chkconfig command in Linux. The chkconfig command is a powerful tool used to manage the startup behavior of system services.

First, let's understand what the chkconfig command does. The chkconfig command is used to configure the runlevels at which a service will start or stop. Runlevels in Linux define the state of the system, and different runlevels are used for different purposes, such as booting into a graphical user interface (runlevel 5) or a command-line interface (runlevel 3).

Using the chkconfig command, you can enable or disable a service to start automatically at different runlevels. This allows you to control which services are running in your system, improving system performance and security.

Let's start by checking the current status of a service using chkconfig:

sudo chkconfig --list nginx

Example output:

nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off

This output shows that the nginx service is enabled to start automatically in runlevels 2, 3, 4, and 5, and disabled in runlevels 0, 1, and 6.

Now, let's enable the nginx service to start at all runlevels:

sudo chkconfig nginx on

Example output:

nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off

You can also disable a service to start at all runlevels:

sudo chkconfig nginx off

Example output:

nginx           0:off   1:off   2:off   3:off   4:off   5:off   6:off

The chkconfig command provides a simple and efficient way to manage the startup behavior of system services in Linux. In the next step, we will explore more advanced usage of the chkconfig command.

Configure Service Startup Behavior Using chkconfig

In this step, we will learn how to configure the startup behavior of system services using the chkconfig command.

First, let's check the current status of the sshd service:

sudo chkconfig --list sshd

Example output:

sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

This shows that the sshd service is currently enabled to start automatically in runlevels 2, 3, 4, and 5, and disabled in runlevels 0, 1, and 6.

Now, let's disable the sshd service from starting automatically in runlevel 2:

sudo chkconfig --level 2 sshd off

Example output:

sshd            0:off   1:off   2:off   3:on    4:on    5:on    6:off

You can also enable the sshd service to start automatically in a specific runlevel:

sudo chkconfig --level 2 sshd on

Example output:

sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

Additionally, you can use the --add option to add a new service to the chkconfig management:

sudo chkconfig --add my-custom-service

This will add the my-custom-service to the chkconfig management, allowing you to configure its startup behavior.

Remember, the changes you make using chkconfig will only affect the startup behavior of the services, not their current running state. If you want to start or stop a service immediately, you should use the appropriate service management commands, such as sudo service sshd start or sudo service sshd stop.

Manage Service Startup Levels with chkconfig

In this final step, we will learn how to manage the startup levels of system services using the chkconfig command.

The chkconfig command allows you to control the runlevels at which a service will start or stop. This is particularly useful when you want to ensure that a service is only running in specific runlevels, or when you need to troubleshoot issues related to service startup.

Let's start by checking the current runlevel of our system:

runlevel

Example output:

5

This shows that our system is currently running in runlevel 5, which is typically the default runlevel for a graphical user interface.

Now, let's say we want to disable the nginx service from starting automatically in runlevel 5, but keep it enabled in runlevels 3 and 4. We can do this using the following command:

sudo chkconfig --level 5 nginx off

Example output:

nginx           0:off   1:off   2:on    3:on    4:on    5:off   6:off

This command disables the nginx service in runlevel 5, while keeping it enabled in runlevels 2, 3, and 4.

You can also enable a service in a specific runlevel using the --level option:

sudo chkconfig --level 6 nginx on

Example output:

nginx           0:off   1:off   2:on    3:on    4:on    5:off   6:on

This command enables the nginx service to start automatically in runlevel 6, which is typically used for system reboot.

By using the chkconfig command with the --level option, you can precisely control the startup behavior of your system services, ensuring that they are running only in the desired runlevels.

Summary

In this lab, you learned the purpose and usage of the chkconfig command in Linux, which is used to manage the startup behavior of system services. You explored how to check the current status of a service using chkconfig, enable a service to start automatically at different runlevels, and disable a service to start at all runlevels. Additionally, you learned how to configure the startup behavior of system services using chkconfig, including enabling, disabling, and managing service startup levels.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like