Sub topic 9
Removing and Disabling Unneeded Services
Every running service is a possible way in. A daemon that listens on a port, reads files, or runs as root adds code an attacker can probe, and it adds patches you have to track. The CIS Benchmarks cover this in the "Services" section. The rule is simple: if the server's role does not need a service, remove it. If you can't remove it, disable it. If you must keep it, restrict it.
This chapter assumes you can use systemctl and ss, you know how your distribution's package manager works, and you have a test server or VM with root or sudo access. You should also have a baseline scan and a snapshot, as covered in the earlier chapters on the hardening workflow and OpenSCAP.
How CIS Treats Services
CIS recommendations in this area share a pattern. Most read "Ensure X is not installed" or "Ensure X services are not in use". Which one you see depends on the benchmark version and the profile.
- Not installed is the strongest option. A package that isn't on disk can't be started by mistake, by a dependency, or by an attacker with root.
- Stopped and masked is the fallback when the package is required by another package, or when removing it would pull out something you need.
- In use but restricted applies to services the server really provides, such as a time client, a mail transfer agent set to local delivery only, or a web server.
Benchmarks usually list the same families of services. Typical examples are:
| Service family | Examples | Why CIS flags it |
|---|---|---|
| Remote file and print | Samba, NFS server, CUPS | Large attack surface and often unauthenticated protocols |
| Legacy remote access | telnet-server, rsh-server, tftp-server, xinetd | Cleartext credentials or no authentication |
| Directory and network infrastructure | DHCP server, DNS server, LDAP server, NIS | Server roles, not default needs |
| Postfix or Exim listening on all interfaces | Should accept local mail only unless the host is a mail server | |
| Web and proxy | httpd, nginx, squid | Only for web servers |
| Desktop leftovers | Avahi, X Window System | Network discovery and graphics have no place on a server |
Many of these items are Level 1 on servers. Some, such as removing the X Window System, are Level 2 or differ between Server and Workstation profiles. Always check the profile in your benchmark version.
Step 1: Inventory What Is Running
Start by finding out what the host is doing. You need two views: what is enabled, and what is actually listening.
# Enabled and running service units
systemctl list-units --type=service --state=running
# Unit files and their enable state
systemctl list-unit-files --type=service --state=enabled
# Sockets and the processes that own them
sudo ss -tulpnRead the ss output carefully. A line with 0.0.0.0:111 or [::]:111 means the service accepts connections from any interface. A line with 127.0.0.1:25 is reachable from the host only. Each externally reachable listener should match something the server's role requires.
Also check for socket-activated services. A unit like cups.service may be inactive while cups.socket still waits for connections and starts the service on demand.
systemctl list-units --type=socket --allStep 2: Decide for Each Service
For every service you did not expect, ask three questions:
- Does the server's documented role need it?
- Does another package depend on it?
- If I remove it, what breaks?
Check dependencies before you act. Use rpm -e --test on Red Hat family systems, or simulate removal with apt-get -s remove on Debian and Ubuntu.
# Debian/Ubuntu: simulate the removal first
apt-get -s remove avahi-daemon
# RHEL/Fedora: show what would be removed
sudo dnf remove --assumeno avahiIf the simulation wants to remove a large metapackage or something you use, stop. That is the signal to stop and mask the service instead.
Step 3: Remove or Disable
Removal is the first choice.
# Debian/Ubuntu
sudo apt-get remove --purge -y avahi-daemon
# RHEL family
sudo dnf remove -y avahiWhen removal is not possible, stop the service, disable it, and mask it. Masking links the unit to /dev/null, so nothing can start it, including a dependency or a socket.
sudo systemctl stop avahi-daemon.service avahi-daemon.socket
sudo systemctl disable avahi-daemon.service avahi-daemon.socket
sudo systemctl mask avahi-daemon.service avahi-daemon.socketStopping the socket matters. If you mask only the service, the socket unit can still be active and hold the port.
The difference between the three actions is easy to confuse.
| Action | Stops it now | Survives reboot | Blocks other units from starting it |
|---|---|---|---|
| `stop` | Yes | No | No |
| `disable` | No | Yes | No |
| `mask` | No | Yes | Yes |
Restricting a Service You Must Keep
Some services stay. A common example is a mail transfer agent used only for local alerts from cron or the system. CIS asks that it listen on the loopback interface only. For Postfix, this is a single setting:
sudo postconf -e 'inet_interfaces = loopback-only'
sudo systemctl restart postfix
# Verify that nothing listens on a public address
sudo ss -tlnp | grep ':25'The same idea applies to other kept services. Bind them to the interfaces they need, and let the host firewall in a later chapter act as a second layer.
Time synchronization is a case where CIS wants exactly one service. Keep either chrony or systemd-timesyncd, and make sure only one runs. Two clients competing to set the clock cause confusing drift.
Verifying the Result
After your changes, repeat the inventory and compare. Confirm that removed packages are gone and that the listener list matches the role.
# Debian/Ubuntu
dpkg -l | grep -E 'avahi|cups|telnetd|rsh-server'
# RHEL family
rpm -qa | grep -E 'avahi|cups|telnet-server|rsh-server'
# Masked state
systemctl is-enabled avahi-daemon.serviceA masked unit prints masked. A removed unit prints an error such as "No such file or directory" or "not-found", and that is correct. Then rerun the OpenSCAP scan from the earlier chapter and confirm that the matching rules now pass.
Edge Cases and Common Errors
- Dependencies pull packages back in. Installing an unrelated package can reinstall a removed service. Masking survives this, and removal does not. Repeat the scan after every large change to find regressions.
- Cloud and container images. Minimal images may already lack these packages. A rule that reports "not installed" and passes is fine. Do not install a package only to disable it.
- Removing something that supports management. On cloud hosts, agents for the cloud provider, configuration management, or monitoring are services too. Keep them, and record them as part of the host's role.
- Rescue and recovery. Do not remove the ability to log in on the console. Test with a second session open, and keep your snapshot until the reboot test passes.
- Reboot test. A change that works until the next boot is a common failure. Reboot a test machine and run the inventory again.
Workflow Summary
The decision for each unexpected service follows a short path.
Exercise
On a test server, ss -tulpn shows cupsd listening on 0.0.0.0:631. The package cannot be removed because another package depends on it. What commands stop it, keep it from returning after a reboot, and keep a socket from restarting it? How do you confirm the result?
Show answer
Run sudo systemctl stop cups.service cups.socket cups.path, then sudo systemctl disable cups.service cups.socket cups.path, then sudo systemctl mask cups.service cups.socket cups.path. Masking prevents any unit or dependency from starting it. Confirm with systemctl is-enabled cups.service, which should print masked, and with sudo ss -tulpn | grep 631, which should return nothing. Reboot and repeat the check, then rerun the OpenSCAP scan.
Exercise
Why does CIS prefer "not installed" over "disabled", and when is masking the better choice?
Show answer
A package that is not installed cannot be started by a dependency, a socket, a mistake, or an attacker with root, and it needs no patches. Masking is better when another package depends on the one you want gone, so removal would also remove software you need. A masked unit cannot be started by anything.