Add the http Service to the public Zone and Test with a Web Server
In this step, you will add a rule to your firewall to allow web traffic and then demonstrate the practical impact by running a simple web server. By default, firewalld blocks most incoming traffic to protect your system. To allow specific connections, you must add rules for the services you want to expose. firewalld uses predefined "services" which are easy-to-remember names for standard network ports. For example, the http service corresponds to TCP port 80, the standard port for unencrypted web traffic.
First, let's check the current firewall status for the public zone before making changes:
sudo firewall-cmd --zone=public --list-services
You'll notice that http is not in the list initially, which means external connections to port 80 are blocked.
Now, let's add the http service to the public zone. You will use the firewall-cmd command with the --add-service option, specifying the zone you want to modify:
sudo firewall-cmd --zone=public --add-service=http
This command tells firewalld to modify the public zone by adding the http service rule. The change is applied to the runtime configuration, meaning it takes effect immediately. You should see a confirmation message:
success
Let's verify the service was added by checking the services list again:
sudo firewall-cmd --zone=public --list-services
You should now see http in the list of allowed services.
To demonstrate the practical impact, let's start a simple web server. We'll use Python's built-in HTTP server on port 80:
cd /tmp
echo "<h1>Welcome to LabEx Firewall Demo</h1><p>This server is running on port 80</p>" > index.html
sudo python3 -m http.server 80
Note that we need sudo because port 80 is a privileged port (below 1024). Keep this server running in a separate terminal.
Now, from another terminal, test the connection:
curl http://localhost
The connection works because the http service is now allowed through the firewall. Note that localhost connections may bypass firewall rules, but the key point is that external connections to port 80 would now be permitted by the firewall.
To further illustrate the firewall's management capabilities, let's temporarily remove the HTTP service:
sudo firewall-cmd --zone=public --remove-service=http
Check the services list to confirm it's removed:
sudo firewall-cmd --zone=public --list-services
You should notice that http is no longer in the list. Now add the service back:
sudo firewall-cmd --zone=public --add-service=http
Verify it's added again:
sudo firewall-cmd --zone=public --list-services
You have now successfully learned how to manage the HTTP service in the firewall and demonstrated the configuration changes. The key concept is that without the http service rule, external connections to port 80 would be blocked by the firewall, even if a web server is running. You can stop the web server with Ctrl+C when you're done with the demonstration.