How to Harden CentOS 8 against Common Attacks

How to Harden CentOS 8 against Common Attacks

What Do I Need?

  • A Dedicated or VPS Linux Server
  • CentOS

What is Operating System Hardening?

Once you’ve installed CentOS, securing it to prevent unauthorized access and intrusions comes second. As the saying goes, ‘prevention is better than the cure’, and this couldn’t be more true than now. Bad actors and hackers are continuously looking to subvert or take control of our servers. You need to prevent this with as much efficacy as possible.

  1. Set Up a Firewall
  1. As a security-conscious and conscientious administrator, you shouldn’t allow just any traffic into your web server. In fact, setting up a firewall is one of the initial server setup tasks that a systems administrator needs to perform in order to only open specific ports and allow services currently in use. By default, CentOS 8 systems operate with the firewalld firewall which can be enabled on startup by running the following commands:
sudo systemctl start firewalld
sudo systemctl enable firewalld

  1. To check the services allowed on the firewall, simply run the following commands:
sudo firewall-cmd --list all

  1. Then open a port on the firewall, i.e. port 443:
sudo firewall-cmd --add-port=443/tcp --zone=public --permanent

  1. Then enable a service, i.e. ssh, type:
sudo  firewall-cmd --add-service=ssh --zone=public --permanent

  1. To remove a port and/or a service, use the -remove-port and -remove-service attributes respectively. For the changes to take effect, always reload the firewall as shown:
sudo firewall-cmd --reload

  1. Disable Unused/Undesirable Services
  1. It’s always advised to turn off any unused or unnecessary services on your web server. This is because the higher the number of services running, the more the number of ports open on your system that can be exploited by any bad actor to gain entry to your systems. Additionally, desist from using old and insecure services like telnet, which sends traffic in plain text. To be honest, this is nothing short of moronic.
  2. Best security practices recommend disabling unused services and getting rid of all insecure services running on your system. You can use the nmap tool to scan your system and check which ports are open and being listened to.
  1. Secure Critical Files
  1. It’s essential to lock down all critical files to prevent accidental deletion or editing. Such files include the /etc/passwd and /etc/gshadow, which contain hashed passwords. To ensure the files are immutable, i.e cannot be modified or accidentally deleted, use the chattr command as shown:
sudo chattr +i /etc/passwd
sudo chattr +i /etc/shadow

  1. This ensures that a bad actor or hacker cannot change any of the users’ passwords or delete them leading to a denial of login to the system.
  1. Secure SSH Protocol
  1. SSH protocol is a popularly used protocol for remote logins. By default, the protocol has native weaknesses that can be exploited by a hacker.
  2. By default, SSH allows remote login by the root user. This is a potential loophole and if a hacker can get a hold of the root’s password to your system, your server is pretty much at their mercy. To prevent this, it’s advisable to deny remote root login and instead create a login regular user with Sudo privileges. You can alter this by modifying the SSH configuration file /etc/ssh/sshd_config and disabling the root login as shown:

  1. Another way you can secure SSH is by setting up SSH passwordless authentication by use of ssh keys. Instead of using password authentication, which is prone to brute force attacks, SSH keys are preferred as they only allow entry to users with the ssh key to log in to the remote server and block out any other user. The first step in enabling passwordless authentication is generating a key pair using the command:

  1. This generates a public and private key pair. The private key resides on the host while the public key is copied to the remote system or server. Once the ssh-key pair is copied, you can log in to the remote system without being prompted for a password. Next, disable password authentication by modifying the /etc/ssh/sshd_config configuration file and setting this value:
PasswordAuthentication no

  1. Ensure that you remember to restart the SSH service for the changes to take effect:
sudo systemctl restart sshd

Next Steps

Really, there’s still a long way to go in securing your Linux web server. I’d recommend looking at defining limits for password attempts and setting up an intrusion prevention system and, of course, remember to keep those upgrades and updates fully up to date.


Keep an eye out for future articles extending upon this and bringing you more helpful tips and tricks to making sure you’re protecting your servers and web hosting appropriately.

How To Set up a VSFTPD Server on a CentOS 7 VPS or Dedicated Server

Brief Description FTP is usually insecure exposing clear-text passwords, userna
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How To Set up a VSFTPD Server on an Ubuntu 16.04 VPS or Dedicated Server

Brief Description FTP data is usually insecure since information (usernames, pa
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to use phpMyAdmin to develop a website (without MySQL experience)

Brief description A web developer who is not well versed into coding websites f
2 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install MySQL on a Windows Web Server Running Apache

This tutorial will show you how to install the MySQL database on a Windows serve
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top