Rocky Linux 10 Tutorial / systemd services, drop-ins and socket activation
When changing systemd service settings on Rocky Linux 10, distinguish reloading unit definitions from applying new settings to a running process . Use a drop-in for the required overrides rather than editing a packaged unit directly. Verify the manager's effective values, the actual process and new connections together.
Distinguish units, services, sockets, and startup status
systemd manages services and other system resources. Each managed definition is a unit . A .service defines how to run a process; a .socket defines listening sockets and the service to activate on demand. A socket is an endpoint for communication.
The units provided by the package are usually checked in /usr/lib/systemd/system, and the settings added by the administrator can be found in /etc/systemd/system. The loaded unit path and drop-in files can be queried in FragmentPath and DropInPaths. The rules for the full path are explained in systemd.unit manual .
| Command | Main Role | Points to Distinguish |
|---|---|---|
| start / stop | Start/stop the current unit | It is separate from the boot-time start settings. |
| enable / disable | Create or remove enablement symlinks according to [Install] | Running only enable does not start the current process. |
| daemon-reload | Reload the unit settings managed by systemd | It does not automatically replace the command or environment variables of running processes. |
| reload | Request to reload custom settings in the service | The service must support this. |
| restart | Stop the unit and then start it again | The process will be replaced and service downtime may occur. |
The official systemctl manual distinguishes between reload of the service and daemon-reload of the systemd manager. enable --now and disable --now are forms that request start and stop simultaneously, so it is necessary to get into the habit of checking the two states separately.
Create a practice unit that does not overlap with existing services
The source lab used a dedicated Rocky Linux 10.2 VM. systemd was 257-23.el10_2.2, GNU coreutils was 9.5, and Python was 3.12.15, with SELinux enforcing maintained. Do not modify SSH or web service units that are actually in operation.
Check whether a general account lfstudent exists for the lecture. If you use a different practice account, replace User= in the unit below with that account. If there are existing units or supplementary files with the same name, stop and examine the contents first.
id lfstudent
systemctl --version
systemctl list-unit-files 'lf-course20*'
sudo ls -ld /etc/systemd/system/lf-course20* 2>/dev/null
sudo ss -lntp '( sport = :19020 )'
In this VM, the specific unit and path did not exist, and port 19020 was also free. The absence of a path for ls is an outcome that can be expected during this pre-check. The procedure is not to delete files when matching names are found and then continue.
Create a new file named sudoedit /etc/systemd/system/lf-course20.service and save the entire content below.
[Unit]
Description=Course20 isolated process lifecycle lab
[Service]
Type=simple
User=lfstudent
Environment=COURSE20_VALUE=base20
ExecStart=/usr/bin/sleep 300
[Install]
WantedBy=multi-user.target
This service runs sleep under a general account and waits for 300 seconds. COURSE20_VALUE is a harmless environment variable used for comparison. After 300 seconds, it may become inactive if it terminates naturally, so when comparing processes, check if it is running and restart if necessary for immediate comparison.
sudo restorecon -v /etc/systemd/system/lf-course20.service
sudo systemd-analyze verify /etc/systemd/system/lf-course20.service
sudo systemctl daemon-reload
systemctl is-enabled lf-course20.service
systemctl is-active lf-course20.service
sudo systemctl enable lf-course20.service
systemctl is-enabled lf-course20.service
systemctl is-active lf-course20.service
sudo systemctl start lf-course20.service
systemctl show lf-course20.service -p FragmentPath -p DropInPaths \
-p MainPID -p ActiveState -p ExecStart -p Environment
In the source verification, the unit was initially disabled / inactive, after enable it was enabled / inactive, and after start it was active. enable created a link under multi-user.target.wants according to this unit’s [Install]. This is not interpreted as the result of a boot test, and in this exercise, the link is removed during cleanup. If there are syntax check errors, resolve them before activation and start.
Add the drop-in and check the results of the manager reload.
A drop-in is a small supplementary file that adds or overrides some settings of the original unit. It is usually written in unit-name.d/*.conf and is affected by the order and priority of multiple files. In actual operation, systemctl edit can also be used, but this time, a new file is created directly to compare the timing of file saving and manager reload.
sudo mkdir /etc/systemd/system/lf-course20.service.d
sudoedit /etc/systemd/system/lf-course20.service.d/10-course.conf
The full content of the new supplementary file is as follows. It is created only in paths where it is confirmed that the original file does not exist.
[Service]
Environment=COURSE20_VALUE=override20
ExecStart=
ExecStart=/usr/bin/sleep 600
After first initializing the existing execution command list with an empty ExecStart=, a new command was specified. To replace execution commands in a typical simple service, you need to understand this list initialization rule. Refer to the systemd.service manual and ExecStart rules.
systemctl show lf-course20.service -p NeedDaemonReload -p MainPID
sudo systemctl daemon-reload
systemctl cat lf-course20.service
systemctl show lf-course20.service -p DropInPaths -p MainPID \
-p ExecStart -p Environment
After the file was added, the source lab observed NeedDaemonReload=yes. After reloading unit definitions, DropInPaths listed the new file, the manager's execution command became sleep 600 and the environment value became override20. However, the running process ID (PID) remained unchanged .
systemctl cat Exercise caution even when just viewing the output. At the point when the systemd manager has not yet recognized the new drop-in, the output might be missing the new file along with a change warning. After re-reading, compare the path, file contents, and the effective values recognized by the systemd manager again.
Compare the actual process with the results after restart.
The following command reads only the current process' command line and one practical variable, not the manager settings. Other environment variables may contain confidential information, so the full environment is not dumped to the screen. If the process has terminated or the PID is 0, start it anew and then compare.
course_pid=$(systemctl show lf-course20.service -p MainPID --value)
sudo python3 - "$course_pid" <<'PY'
from pathlib import Path
import sys
proc = Path('/proc') / sys.argv[1]
print(proc.joinpath('cmdline').read_bytes().replace(bytes([0]), b' ').decode())
for item in proc.joinpath('environ').read_bytes().split(bytes([0])):
if item.startswith(b'COURSE20_VALUE='):
print(item.decode())
PY
The source result after only daemon-reload was as follows. The manager settings are new values, but the already running processes retained their original command and environment.
/usr/bin/sleep 300
COURSE20_VALUE=base20
This practice service did not define ExecReload, which has its own re-read behavior. The following reload request is a comparison to verify that this operation is not supported.
sudo systemctl reload lf-course20.service
systemctl is-active lf-course20.service
sudo systemctl restart lf-course20.service
systemctl show lf-course20.service -p MainPID -p ExecStart -p Environment
The actual reload failed with termination state 3 and Job type reload is not applicable, but the existing service was active. After the restart, the PID changed, and if you run the actual process check block above again, you can see sleep 600 and COURSE20_VALUE=override20. Support for reload varies by service, so this result is not generalized to all services.
Check whether removing the supplementary file restores it to the original. After removing only the supplemental file created this time, reload and restart. Do not delete any other drop-ins if they exist.
sudo rm -- /etc/systemd/system/lf-course20.service.d/10-course.conf
sudo rmdir /etc/systemd/system/lf-course20.service.d
sudo systemctl daemon-reload
sudo systemctl restart lf-course20.service
systemctl show lf-course20.service -p DropInPaths -p ExecStart -p Environment
sudo systemctl disable --now lf-course20.service
In the verification, DropInPaths were empty, and the actual process also returned to sleep 300 / base20. The last command cleaned up both the current execution and the automatic start link. Do not use a broad operation such as systemctl revert merely to remove this one drop-in: it can remove other custom unit files or overrides too.
Check whether socket requests run the service.
Socket activation is a way in which systemd waits for a connection and starts the service when needed. The next exercise is a simple echo service that receives requests only from 127.0.0.1:19020 on the same VM. It echoes back the received bytes and does not add any external firewall ports.
Create a new file /etc/systemd/system/lf-course20-echo.socket and save the following entire content.
[Unit]
Description=Course20 loopback echo socket lab
[Socket]
ListenStream=127.0.0.1:19020
Accept=yes
[Install]
WantedBy=sockets.target
Save the following content in a new file /etc/systemd/system/lf-course20-echo@.service. A name that has @ is a template that creates an instance for each connection.
[Unit]
Description=Course20 one-connection echo worker
[Service]
User=lfstudent
ExecStart=/usr/bin/cat
StandardInput=socket
StandardOutput=socket
Accept=yes is a configuration that runs a service from a template for each connection. Since the socket is passed to standard input and output, cat can return exactly what it receives. Not all programs support socket activation. The relationship between the delivery method and names can be found in the systemd.socket manual .
sudo restorecon -v /etc/systemd/system/lf-course20-echo.socket \
/etc/systemd/system/lf-course20-echo@.service
sudo systemd-analyze verify /etc/systemd/system/lf-course20-echo.socket \
/etc/systemd/system/lf-course20-echo@.service
sudo systemctl daemon-reload
sudo systemctl start lf-course20-echo.socket
systemctl show lf-course20-echo.socket -p ActiveState -p Listen -p Accept
systemctl list-units --all 'lf-course20-echo@*' --no-pager
At this point, the socket is active and waiting on the loopback address, but there may not yet be a per-connection service. Again, before the first request, the number of instances was 0. It is not assumed that the states of the socket and the worker process must be the same.
Run the following complete Python client inside the VM. Send one line and finish the sending side, then collect responses until the peer closes. TCP (Transmission Control Protocol) does not guarantee that the entire message will arrive in a single recv, so read repeatedly.
python3 - <<'PY'
import socket
message = b'echo20-ok' + bytes([10])
with socket.create_connection(('127.0.0.1', 19020), timeout=3) as connection:
connection.sendall(message)
connection.shutdown(socket.SHUT_WR)
received = b''
while True:
part = connection.recv(1024)
if not part:
break
received += part
if received != message:
raise RuntimeError('echo response mismatch')
print(received.decode(), end='')
PY
systemctl is-active lf-course20-echo.socket
sudo journalctl -u 'lf-course20-echo@*' --since '5 minutes ago' --no-pager
The expected output is echo20-ok, and the socket remains active. In the source lab, the returned bytes from two different connections matched the requests, and the journal recorded that different service instances started and terminated normally for each connection. The source lab also separately verified the complete client example above. If the socket is still alive after the work process ends, it can be restarted with the next request.
Verify stopping the socket and removing the practice file.
Ending just one service instance does not mean that the entire functionality has stopped. To prevent new connections, also check the active socket path. This practice socket did not have automatic start enabled, so you can stop the current socket.
sudo systemctl stop lf-course20-echo.socket
sudo ss -lntp '( sport = :19020 )'
systemctl list-units --all 'lf-course20-echo@*' --no-pager
There should be no reception on port 19020. After shutdown in the source lab, a new TCP connection failed with error number 111 corresponding to Connection refused. If the socket is enabled in another service, the activation conditions for the next boot must also be separately organized, and the practice command should not be applied directly to the existing operational socket.
Check the three files created this time, which were not present initially, for their content and names, and then remove them. Drop-in and automatic start connections have already been cleared in the previous step. If a connection handling instance is still running in the template, proceed after verifying the request has ended.
sudo systemctl disable --now lf-course20.service
sudo rm -- /etc/systemd/system/lf-course20.service \
/etc/systemd/system/lf-course20-echo.socket \
/etc/systemd/system/lf-course20-echo@.service
sudo systemctl daemon-reload
systemctl show lf-course20.service lf-course20-echo.socket -p LoadState -p ActiveState
sudo ss -lntp '( sport = :19020 )'
getenforce
After cleanup, the unit was LoadState=not-found and ActiveState=inactive, and its listening port was absent. The original SSH unit content and PID, firewall files and runtime policy matched the pre-lab state. Check the saved unit files, manager values, running process and connection results separately to distinguish saving a definition from changing a running service.




