Linux update-rc.d Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the update-rc.d command in Linux to manage the automatic startup of services at system boot. You will understand the purpose of the update-rc.d command, configure services to start automatically at boot, and manage service startup priorities. The lab covers practical examples and the basic syntax of the update-rc.d command, which is particularly useful when you need to ensure that a service starts automatically when the system boots up or when you want to change the order in which services are started.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") subgraph Lab Skills linux/grep -.-> lab-422982{{"`Linux update-rc.d Command with Practical Examples`"}} linux/ls -.-> lab-422982{{"`Linux update-rc.d Command with Practical Examples`"}} linux/service -.-> lab-422982{{"`Linux update-rc.d Command with Practical Examples`"}} end

Understand the Purpose of update-rc.d Command

In this step, you will learn about the purpose and usage of the update-rc.d command in Linux. The update-rc.d command is used to manage the automatic startup of services at system boot. It allows you to enable, disable, or modify the startup priorities of system services.

The update-rc.d command is particularly useful when you need to ensure that a service starts automatically when the system boots up, or when you want to change the order in which services are started.

Let's start by understanding the basic syntax of the update-rc.d command:

sudo update-rc.d [service_name] defaults [priority]

Here, [service_name] is the name of the service you want to manage, and [priority] is the startup priority for the service. The defaults argument tells update-rc.d to use the default runlevels (2, 3, 4, and 5) for the service.

Example output:

sudo update-rc.d nginx defaults

This command will enable the nginx service to start automatically at system boot, using the default runlevels and startup priority.

Configure Services to Start Automatically at Boot

In this step, you will learn how to use the update-rc.d command to configure services to start automatically at system boot.

Let's start by checking the current status of the nginx service. We can use the service command to do this:

sudo service nginx status

Example output:

nginx is not running

As you can see, the nginx service is not currently running. Let's enable it to start automatically at boot using the update-rc.d command:

sudo update-rc.d nginx defaults

This command will create the necessary symbolic links in the /etc/rc*.d/ directories to ensure that the nginx service starts automatically when the system boots up.

To verify that the nginx service is now enabled to start at boot, we can check the contents of the /etc/rc2.d/ directory:

ls -l /etc/rc2.d/ | grep nginx

Example output:

lrwxrwxrwx 1 root root 16 Apr 11 09:42 S20nginx -> ../init.d/nginx

The output shows that the nginx service has been added to the /etc/rc2.d/ directory with a startup priority of 20.

Now, let's try rebooting the system and checking the status of the nginx service again:

sudo reboot

After the system has rebooted, log back in and check the nginx service status:

sudo service nginx status

Example output:

nginx is running

As you can see, the nginx service is now running and has been configured to start automatically at system boot.

Manage Service Startup Priorities with update-rc.d

In this step, you will learn how to manage the startup priorities of services using the update-rc.d command.

The startup priority of a service determines the order in which services are started during the boot process. This is important when you have multiple services that depend on each other or need to be started in a specific order.

Let's start by adding another service to our system, the syslog service. We can enable it to start automatically at boot using the update-rc.d command:

sudo update-rc.d rsyslog defaults

Now, let's check the startup priorities of the nginx and rsyslog services:

ls -l /etc/rc2.d/ | grep -E 'nginx|rsyslog'

Example output:

lrwxrwxrwx 1 root root 16 Apr 11 09:42 S20nginx -> ../init.d/nginx
lrwxrwxrwx 1 root root 16 Apr 11 09:42 S19rsyslog -> ../init.d/rsyslog

As you can see, the nginx service has a higher startup priority (20) than the rsyslog service (19). This means that the nginx service will start before the rsyslog service during the boot process.

Let's change the startup priority of the rsyslog service to make it start before the nginx service:

sudo update-rc.d rsyslog defaults 18

The 18 at the end of the command sets the startup priority for the rsyslog service to 18, which is lower than the nginx service's priority of 20.

Now, let's check the startup priorities again:

ls -l /etc/rc2.d/ | grep -E 'nginx|rsyslog'

Example output:

lrwxrwxrwx 1 root root 16 Apr 11 09:42 S18rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root 16 Apr 11 09:42 S20nginx -> ../init.d/nginx

As you can see, the rsyslog service now has a lower startup priority (18) than the nginx service (20), ensuring that the rsyslog service starts before the nginx service during the boot process.

Summary

In this lab, you learned about the purpose and usage of the update-rc.d command in Linux, which is used to manage the automatic startup of services at system boot. You explored how to enable services to start automatically at boot, as well as how to manage the startup priorities of services using the update-rc.d command. The lab provided practical examples and step-by-step instructions to help you understand and apply these concepts effectively.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like