This guide explains how to secure a CentOS 7 server by reducing its attack surface, enforcing strong authentication, and keeping software patched. You will learn concrete steps for user access, firewall configuration, SSH hardening, SELinux, and system auditing, along with common pitfalls and verification commands.
Why CentOS 7 Needs Hardening
CentOS 7 reached end-of-life on June 30, 2024, meaning it no longer receives security updates from the CentOS Project. Any system still running it is exposed to known, unpatched vulnerabilities. Hardening reduces risk by limiting what attackers can reach, but it does not replace the need to migrate to a supported OS like CentOS Stream, AlmaLinux, or Rocky Linux. For systems that must remain on CentOS 7 temporarily, hardening is a mitigation, not a cure.
Essential Hardening Steps
Start with the basics: update the system, create a non-root user, and configure the firewall. These three steps close the most common entry points.
- Update packages: Run
yum update -yto apply all available patches. If you use a third-party repository like EPEL, update it too. - Create a regular user: Use
useradd usernamethenpasswd username. Add them to thewheelgroup for sudo access:usermod -aG wheel username. - Enable firewalld:
systemctl enable firewalldandsystemctl start firewalld. Then allow only needed services:firewall-cmd --permanent --add-service=sshandfirewall-cmd --reload.
These steps alone block many automated scans. The next sections go deeper into each area.
SSH Hardening
SSH is the most attacked service on a public server. Disable password authentication and root login, and use key-based authentication instead.
- Generate a key pair on your local machine:
ssh-keygen -t ed25519(or RSA with 4096 bits). - Copy the public key to the server:
ssh-copy-id username@server_ip. - Edit
/etc/ssh/sshd_configand set:
PermitRootLogin no PasswordAuthentication no PubkeyAuthentication yes AllowUsers username
Then restart SSH with systemctl restart sshd. Always test in a second terminal before closing the current session. Also consider changing the default port from 22 to a non-standard one, though this is security by obscurity and not a substitute for proper authentication.
User Accounts and Password Policies
Enforce strong passwords and lock out accounts after repeated failures. Install pam_pwquality and configure it in /etc/security/pwquality.conf:
minlen = 12 minclass = 4 maxrepeat = 3
Set password aging in /etc/login.defs:
PASS_MAX_DAYS 90 PASS_MIN_DAYS 7 PASS_WARN_AGE 14
For existing users, run chage -M 90 username. To lock an account after five failed attempts, add to /etc/pam.d/system-auth:
auth required pam_faillock.so preauth audit silent deny=5 unlock_time=900
This locks the account for 15 minutes after five failures. Be careful not to lock yourself out; always keep a root session open.
SELinux and File Permissions
SELinux is enabled by default in CentOS 7 and provides mandatory access control. Verify it is enforcing:
getenforce
If it shows Permissive or Disabled, enable it in /etc/selinux/config and reboot. Do not disable SELinux to fix a problem; instead, use audit2why to see what is blocked and adjust policies correctly.
For file permissions, ensure sensitive files like /etc/shadow are readable only by root:
ls -l /etc/shadow
It should show -rw-------. Use chmod 600 if needed. Also check for world-writable files with find / -perm -002 -type f and remove write permissions where appropriate.
System Auditing and Monitoring
Auditd records security-relevant events. Install and start it:
yum install audit systemctl enable auditd systemctl start auditd
Add rules to watch critical files, for example:
auditctl -w /etc/passwd -p wa -k passwd_changes auditctl -w /etc/shadow -p wa -k shadow_changes
Review logs with ausearch -k passwd_changes. Also check system logs for unusual activity:
journalctl -u sshd last -f /var/log/btmp | head -20
The last command shows failed login attempts. If you see many, your SSH hardening is working; if not, revisit the SSH section.
Kernel and Network Hardening
Disable unused network protocols and restrict ICMP redirects. Edit /etc/sysctl.conf and add:
net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv4.tcp_syncookies = 1 net.ipv4.icmp_echo_ignore_broadcasts = 1
Apply with sysctl -p. Disable unused kernel modules by creating files in /etc/modprobe.d/, for example:
echo "install dccp /bin/false" > /etc/modprobe.d/dccp.conf
This blocks the DCCP protocol, which is rarely needed. Only disable protocols you are sure you do not use; research each one first.
Frequently Asked Questions
Is CentOS 7 still safe to use?
No, CentOS 7 is end-of-life and receives no security updates. Running it publicly is risky. If you must, harden it as described, but plan to migrate to a supported distribution soon.
What is the difference between firewalld and iptables?
firewalld is the front-end service in CentOS 7 that manages iptables rules dynamically. It uses zones and services, making it easier to manage than raw iptables commands. For most users, firewalld is sufficient.
How do I check if SELinux is blocking something?
Run ausearch -m avc -ts recent to see recent denials. Then use audit2why -a to get a human-readable explanation. You can then create a custom policy with audit2allow -M mypolicy instead of disabling SELinux.
Can I harden CentOS 7 without breaking my applications?
Yes, if you test each change. Start with non-disruptive steps like updating packages and creating a non-root user. For SELinux and sysctl changes, apply them in a test environment first. Always have a rollback plan, such as a snapshot or backup.
What should I do if I get locked out of SSH?
If you have physical or console access, log in as root or via the console and revert your changes. If not, use your hosting provider’s recovery console. Always test SSH changes in a separate session before closing the current one.
Is changing the SSH port worth it?
It reduces automated scan noise but does not stop a determined attacker. It is a minor deterrent. Focus on key-based authentication and disabling root login, which are far more effective.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Gadgets After Support Ends: Do They Improve?
- Delete Google Search Results About You
- OnePlus Buds Pro 3 Price Drop: Are They Worth It?
- Cloud Migration Ways to Prepare
- Remote Work Productivity Hacks That Actually Work
- Epic Games Removes Dark and Darker? Fact Check