Sub topic 9
Removing and Disabling Unneeded Services
Assume you are comfortable with systemctl, journalctl, package managers, and basic network inspection. You also need sudo access to a test server or a maintenance window.
Service minimization reduces the number of programs that can fail, expose network ports, or be abused. A CIS recommendation usually asks you to remove or disable services that the server does not need. “Unneeded” depends on the server’s role. A database server may need a database service but not a web server. A monitoring agent may be required even though it opens a port.
Inventory Before Changing Anything
Start by recording running services, enabled services, listening sockets, and installed packages:
sudo systemctl list-units --type=service --state=running
sudo systemctl list-unit-files --type=service --state=enabled
sudo ss -lntup
rpm -qa | sort # RHEL, Fedora, or Rocky Linux
dpkg-query -W -f='${binary:Package}\n' | sort # Debian or UbuntuA running service is active now. An enabled service starts during boot. These states are independent. A service can be stopped but enabled, or running but disabled.
Check a candidate before changing it:
systemctl status cups.service
systemctl cat cups.service
systemctl list-dependencies cups.serviceThe unit file shows how systemd starts the service. Dependencies reveal whether another required unit relies on it.
Stop, Disable, or Mask
Use the least disruptive action that meets the requirement:
stopends the service now but does not change boot behavior.disableprevents the service from starting through its normal boot target.maskprevents manual or dependency-based starts by linking the unit to/dev/null.- Removing a package deletes the software and may remove configuration or dependencies.
For example, if a server has no printing role:
sudo systemctl disable --now cups.service
sudo systemctl mask cups.service
systemctl is-enabled cups.service
systemctl is-active cups.serviceDo not mask a service until you understand its dependencies. Socket-activated services can also start when a client connects, even if the main service was not running at boot. Inspect related sockets:
systemctl list-unit-files --type=socket
systemctl status cups.socketTo reverse the change:
sudo systemctl unmask cups.service
sudo systemctl enable --now cups.serviceVerify the Result
After each change, verify both the service state and the network exposure:
systemctl is-enabled cups.service
systemctl is-active cups.service
sudo ss -lntup
sudo journalctl -u cups.service --since "10 minutes ago"Run the application workload and check monitoring after the change. Record the service name, reason, owner, change time, validation result, and rollback command. This record makes future CIS scans explainable and prevents an administrator from restoring an intentionally disabled service without review.
Exercise
Why might disabling a service fail to prevent it from starting later?
Show answer
A related socket, timer, dependency, administrator, or package update may start it. Inspect related units and use masking when preventing all starts is required.