Several days ago, I encountered a 32-bit CentOS 8 distribution and decided to test it on an older 32-bit system. Post-boot, I discovered a network connectivity issue; the connection would drop, requiring manual restoration after each reboot. This prompted me to explore automated solutions. This article details how to achieve this using systemd service units.
Before diving into the specifics, let's briefly examine systemd service units and their functionality. We'll cover the fundamentals of systemd service units, their interaction with "targets," and the process of configuring a service unit to execute a script on boot. The focus will be on practical steps.
What are Systemd Service Units?
A systemd service unit is a configuration file defining a service's system behavior. This could encompass network services, applications, or scripts designed to run during boot or at specific boot stages.
These units are organized into targets, representing milestones in the boot process. For instance, upon reaching the multi-user target (runlevel 3), specific services initiate. Consider targets as service "groups" collaborating at various boot phases.
To view services active within a target (e.g., graphical.target), utilize the systemctl
command:
systemctl --type=service
This displays all active services in the current target. Some services run continuously; others execute once and terminate.
Monitoring Service Status
To check a service's status (active or inactive), use systemctl status
:
systemctl status firewalld.service
This command checks firewalld's status. Observe its active (running) and enabled (auto-starts on reboot) states.
Temporarily stop a service (until the next boot) with:
systemctl stop firewalld.service systemctl status firewalld.service
This halts firewalld for the current session but doesn't prevent future restarts.
Enabling and Disabling Services
To ensure a service auto-starts on boot, enable it (creating a symbolic link in the target's "wants" folder):
systemctl enable firewalld.service
Disabling is achieved via:
systemctl disable firewalld.service
Creating a Custom Service Unit
To create a boot-time script service, create a new service unit in /etc/systemd/system
. This directory houses existing service unit files and target folders.
cd /etc/systemd/system ls -l
Create connection.service
using Vim or a similar editor:
vim connection.service
Add the following:
[Unit] Description=Bring up network connection After=network.target [Service] ExecStart=/root/scripts/conup.sh [Install] WantedBy=multi-user.target
Explanation:
-
[Unit]
: Metadata; describes the unit and specifies execution afternetwork.target
(ensuring network initialization). -
[Service]
: Defines the command (conup.sh
script execution). -
[Install]
: Specifies loading at themulti-user.target
.
Enable the service for auto-start on reboot:
systemctl enable connection.service
Verify enabling by checking multi-user.target.wants
:
ls -l multi-user.target.wants/
The connection.service
symbolic link should be present. Now, create the script.
Creating the Script
Create the conup.sh
script to restore the network connection:
cd /root mkdir scripts cd scripts vi conup.sh
Add this line (assuming the interface is enp0s3
):
#!/bin/bash nmcli connection up enp0s3
Make the script executable:
chmod x conup.sh
The service is now ready.
SELinux Considerations (RHEL/CentOS)
On RHEL-based systems (CentOS, Rocky Linux), SELinux might block script execution unless the correct security context is applied.
Temporarily set the context:
chcon -t bin_t /root/scripts/conup.sh
For a permanent solution:
semanage fcontext -a -t bin_t "/root/scripts/conup.sh" restorecon -v /root/scripts/conup.sh
This ensures continued script execution after reboots or SELinux policy updates.
Testing the Service
Test without rebooting by manually starting the service:
systemctl start connection.service
If successful, the network connection should restore. For simpler scripts (e.g., touch /tmp/testbootfile
), check for /tmp/testbootfile
creation to confirm service execution.
Conclusion
This guide provides a comprehensive understanding of systemd service units, their creation, management, and application in automating tasks such as network connection restoration on boot. This enhances system automation and efficiency.
The above is the detailed content of How to Create and Run New Service Units in Systemd. For more information, please follow other related articles on the PHP Chinese website!

The open source nature of Linux makes it better than Windows in terms of community participation, performance, security, etc., but Windows is better in user-friendliness and software ecosystem. 1) Linux encourages community contribution and has a fast development speed; 2) Better performance in servers and embedded systems; 3) Open source nature makes it safer; 4) Windows user interface is friendly and the software ecosystem is extensive.

This guide explores essential Linux tools for monitoring and troubleshooting disk I/O performance, a crucial metric impacting server speed and application responsiveness. Disk I/O performance directly affects how quickly data is read from and written

For new Linux users, identifying connected devices is crucial, especially USB drives. This guide provides several command-line methods to determine a USB device's name, essential for tasks like formatting. While USB drives often auto-mount (e.g., /

One of the most common problems with Linux systems, especially those with limited disk space, is the exhaustion of root partition (/) space. When this problem occurs, you may encounter the following error: No space left on device Don’t panic! This just means that your root directory (/partition) is full, which is a common problem, especially on systems with limited disk space or servers running 24/7. When this happens, you may encounter the following problems: The package cannot be installed or upgraded. System startup failed. The service cannot be started. Unable to write to logs or temporary files. This article walks you through practical steps to identify problems, clean up space safely, and prevent them from happening again. These instructions are suitable for beginners

This article explores top-notch Notepad alternatives for Linux users. Notepad , while excellent on Windows, lacks a Linux version. This guide offers a diverse range of options to suit various needs and preferences. Top Notepad Alternatives for

Several days ago, I encountered a 32-bit CentOS 8 distribution and decided to test it on an older 32-bit system. Post-boot, I discovered a network connectivity issue; the connection would drop, requiring manual restoration after each reboot. This pr

Let's clarify what constitutes a bad sector or bad block: it's a portion of a hard drive or flash memory that's become unreadable or unwritable, typically due to physical damage to the disk surface or malfunctioning flash memory transistors. Accumul

The cp command, short for "copy," is a fundamental tool in Linux and other Unix-like systems for duplicating files and directories. While efficient for local file transfers, for network-based copies, scp (secure copy) is preferred due to i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
