How to Configure Firewalls Using Command Line in CentOS 8

How to Configure Firewalls Using Command Line in CentOS 8

What Do I Need?

  • A Dedicated or VPS Linux Server
  • CentOS
  • Putty

What is Firewalld?

Firewalld is a mechanism by which your operating system handles firewall configuration. The current version of CentOS uses a version of Firewalld based on Nftables. Read on to find out how to configure firewalls using command line in CentOS 8.

  1. Update System Software
  1. Log into your server as a root user or user with superuser capability. It’s always a good start to fully update the system with the latest kernel, system security patches, software repositories, and packages.
dnf check-update
dnf update
dnf clean all

  1. Enabling Firewalld
  1. In order to enable Firewalld you must be logged in as a root user or a user with sudo privileges:
yum install firewalld

  1. Next enable it with the following command:
systemctl  start firewalld

  1. And, next check its working:
systemctl  enable firewalld

  1. Run the following command to check the FirewallD service state:
firewall-cmd --state

  1. Configure and Manage the Firewall
  1. From the beginning, the public zone will be in use.
firewall-cmd --get-default-zone

  1. To see the complete list of all the zones, use the following:
firewall-cmd --get-zones

  1. To change to another zone, get the name of the zone you want from the previous command and use the following instructions:
firewall-cmd --set-default-zone=NAME OF NEW ZONE HERE

  1. To check all the active zones and network interfaces that are assigned to them use the following:
firewall-cmd --get-active-zones

  1. If you want to change the zone target, it’s the default behavior for incoming traffic. You can use one of the following default, ACCEPT, REJECT and DROP. For example:
firewall-cmd --zone=public --set-target=ACCEPT

  1. You should assign interfaces to specific zones. First, specify the zone, in this case it’ll be ‘home’, then you use the modifier flags for changing the interface and specify the interface. For example:
firewall-cmd --zone=home --change-interface=eth1

  1. To inspect all the rules and services for a specific zone use the following command:
firewall-cmd --set-default-zone=home
firewall-cmd --get-default-zone

  1. You can also use the following command to inspect the services of the default zone:
firewall-cmd --get-services

  1. To add HTTP services use:
firewall-cmd --zone=public --add-service=http

  1. Opening port 80/tcp:
firewall-cmd --zone=public --add-port=80/tcp

  1. If you want to make the changes permanent, you just need to add ‘-permanent’ and the ‘-cmd’.
  1. Disable/Stop the Firewalld Service
  1. At times there will be a need to stop or disable the Firewalld service in the webserver. Stop the Firewalld service:
systemctl stop firewalld

  1. The disable the Firewalld service use the following command:
systemctl disable firewalld

Next Steps

Next, check your access and user logs. I can tell you this now from personal experience that it’s not unusual to see a lot of unauthorized attempts to access your server. I hate to say this but that’s normal and isn’t indicative of even a targeted attack. So, if you’re seeing lots of attempts, don’t take it personally. A lot of these attacks are the results of port-scanning and scatter-gun searches across entire IP address ranges looking for insecure web servers that can be turned into zombie machines for botnets. My own production servers receive a lot of hits. As you can see from the screenshot I wasn’t even away from my server for long. I think I was only previously logged out from its terminal for about an hour.

Don’t take it personally whatsoever, it’s essentially just the cost of doing business. The best thing to do is to keep an eye on your kernel and system resources in order to track and trace any irregular increases in consumption.

Conclusion

As always, keep an eye on those updates and upgrades. And as ever, always watch those logs.

How to Find cPanel and System Log File’s Location on CentOS 7

Introduction Have you ever experienced problems with your VPS server and tried
5 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Add Modules to the Apache Server on CentOS

The Apache web server is one of the most popular and robust web servers out ther
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How To Install Odoo on CentOS 7?

This tutorial will show you how to deploy Odoo 11 on CentOS 7 using Python Virtu
4 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Install vBulletin Connect 5 Forum Software

This Tutorial will show how to install the vBulletin Connect 5 forum software sc
4 min read
Jeffrey Scott
Jeffrey Scott
Hosting Expert
HostAdvice.com 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