Managing Service Boot Behavior: Enable and Disable
In this step, you'll learn how to configure whether services start automatically when the system boots. This is important for ensuring that required services are available without manual intervention after a system restart.
Understanding Service Boot Configuration
Services can be configured to start automatically at boot time (enabled) or to require manual starting (disabled). This configuration is separate from the current running state of the service.
Checking If a Service Is Enabled
-
To check if the SSH service is configured to start at boot:
systemctl is-enabled sshd
The output will be either "enabled" (starts at boot) or "disabled" (doesn't start at boot).
Disabling a Service
When you disable a service, you're configuring it not to start automatically at boot time:
-
Disable the SSH service:
sudo systemctl disable sshd
You should see a message indicating that the symlink was removed:
Removed /etc/systemd/system/multi-user.target.wants/ssh.service.
-
Verify that the service is now disabled:
systemctl is-enabled sshd
The output should be "disabled".
-
Note that disabling a service doesn't stop it if it's currently running. Check the current status:
systemctl status sshd
Even though the service is now disabled for the next boot, it may still be active.
Enabling a Service
When you enable a service, you're configuring it to start automatically at boot time:
-
Re-enable the SSH service:
sudo systemctl enable sshd
You should see a message indicating that the symlink was created:
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service.
-
Verify that the service is now enabled:
systemctl is-enabled sshd
The output should be "enabled".
Combined Commands
You can also combine enabling/disabling with starting/stopping in a single command:
-
To disable and stop a service in one command:
sudo systemctl disable --now sshd
-
To enable and start a service in one command:
sudo systemctl enable --now sshd
Creating a Reference File for Service Boot Configuration
Let's create a reference file with the commands you've learned:
-
Create a file named service_boot.txt
in your project directory:
echo "Service Boot Configuration Commands:" > ~/project/service_boot.txt
echo "Check if a service is enabled: systemctl is-enabled <service_name>" >> ~/project/service_boot.txt
echo "Enable a service to start at boot: sudo systemctl enable <service_name>" >> ~/project/service_boot.txt
echo "Disable a service from starting at boot: sudo systemctl disable <service_name>" >> ~/project/service_boot.txt
echo "Enable and immediately start a service: sudo systemctl enable --now <service_name>" >> ~/project/service_boot.txt
echo "Disable and immediately stop a service: sudo systemctl disable --now <service_name>" >> ~/project/service_boot.txt
-
Verify the contents of the file:
cat ~/project/service_boot.txt
In this step, you've learned how to manage service boot behavior by enabling and disabling services. This is crucial for configuring which services start automatically at system boot, ensuring that necessary services are available while unnecessary ones don't consume resources.