How to Enable Firewall Rules: A Practical Step-by-Step Guide to Securing Your Network

How to Enable Firewall Rules: A Practical Step-by-Step Guide to Securing Your Network

Take the guesswork out of network security with clear, actionable instructions that help you enable firewall rules safely and efficiently. Youll find conceptual guidance and concrete CLI examples for nftables, UFW, iptables, and firewalld so you can lock down servers with confidence.

Properly enabling firewall rules is a foundational step in securing any networked system — from a single VPS to a multi-tier enterprise architecture. This guide offers a practical, technical walkthrough targeted at webmasters, enterprise administrators, and developers. You will find conceptual explanations, concrete CLI examples, configuration patterns, and deployment considerations so you can implement robust, maintainable firewall policies across Linux servers, cloud platforms, and edge devices.

Why firewall rules matter: core principles

At its simplest, a firewall enforces an access control policy that decides which network traffic is allowed or denied. Modern firewalls can be host-based (running on a server), network-based (appliance or virtual appliance), or cloud-native (security groups or network security groups). Key principles to follow when enabling firewall rules:

  • Default deny: Permit only the traffic you explicitly need; block everything else.
  • Least privilege: Restrict access to the minimal set of ports, protocols, and IP ranges.
  • Stateful inspection: Prefer stateful firewalls that track connection state (ESTABLISHED, RELATED) rather than stateless packet filters.
  • Layered defenses: Combine host firewall, network firewall, and application-level controls (e.g., WAF) for defense in depth.
  • Logging and monitoring: Log denied traffic and use rate-limiting or alerting to detect attacks early.

Practical step-by-step: enabling firewall rules on Linux servers

Below are step-by-step examples for common Linux firewall tools: iptables, nftables, UFW (Ubuntu-friendly), and firewalld (RHEL/CentOS). Each example demonstrates a safe pattern: set a default policy, add allow rules for required services, enable logging, then persist and verify.

1) nftables (modern Linux)

nftables replaces iptables on many modern distributions. A minimal, secure setup for a web server (SSH + HTTP/S) looks like:

# create table and base chains
sudo nft add table inet filter
sudo nft 'add chain inet filter input { type filter hook input priority 0 ; policy drop ; }'
sudo nft 'add chain inet filter forward { type filter hook forward priority 0 ; policy drop ; }'
sudo nft 'add chain inet filter output { type filter hook output priority 0 ; policy accept ; }'

allow loopback and established/related

sudo nft add rule inet filter input iif lo accept sudo nft add rule inet filter input ct state established,related accept

allow SSH from specific admin CIDR

sudo nft add rule inet filter input ip saddr 203.0.113.0/24 tcp dport 22 ct state new accept

allow HTTP/HTTPS from anywhere

sudo nft add rule inet filter input tcp dport { 80, 443 } ct state new accept

log and drop everything else

sudo nft add rule inet filter input log prefix "nft-drop: " counter

policy already drop, ensure rules saved to /etc/nftables.conf and enable systemd service

Tips: Use an admin CIDR for SSH to reduce attack surface. Test rules in a separate session before closing an existing SSH connection to avoid lockout.

2) iptables (legacy)

A legacy iptables approach for backward compatibility:

sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

allow loopback and established connections

sudo iptables -A INPUT -i lo -j ACCEPT sudo iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

allow SSH from admin CIDR

sudo iptables -A INPUT -p tcp -s 203.0.113.0/24 --dport 22 -m conntrack --ctstate NEW -j ACCEPT

allow HTTP and HTTPS

sudo iptables -A INPUT -p tcp --dport 80 -m conntrack --ctstate NEW -j ACCEPT sudo iptables -A INPUT -p tcp --dport 443 -m conntrack --ctstate NEW -j ACCEPT

logging and persist

sudo iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 4

save with iptables-save > /etc/iptables/rules.v4 (Debian/Ubuntu) or use a persistent service

3) UFW (Uncomplicated Firewall)

UFW abstracts iptables for easier management on Ubuntu and Debian:

sudo ufw default deny incoming
sudo ufw default allow outgoing

allow SSH only from admin subnet

sudo ufw allow from 203.0.113.0/24 to any port 22 proto tcp

allow HTTP/HTTPS

sudo ufw allow 80/tcp sudo ufw allow 443/tcp

enable and check status

sudo ufw enable sudo ufw status verbose

UFW is ideal for straightforward host protection. For advanced filtering use nftables/iptables.

4) firewalld (RHEL family)

firewalld uses zones and rich rules. Example to restrict SSH and allow web:

# set default zone (public)
sudo firewall-cmd --set-default-zone=public

allow web services

sudo firewall-cmd --zone=public --add-service=http --permanent sudo firewall-cmd --zone=public --add-service=https --permanent

create a zone for admin and allow SSH only from admin CIDR

sudo firewall-cmd --permanent --new-zone=admin sudo firewall-cmd --permanent --zone=admin --add-source=203.0.113.0/24 sudo firewall-cmd --permanent --zone=admin --add-service=ssh

reload to apply

sudo firewall-cmd --reload

Cloud and virtualization: enabling firewall rules at higher layers

Cloud providers expose network-level controls that complement host firewalls. Always apply both cloud and host-level rules for defense-in-depth.

AWS Security Groups and NACLs

  • Security Groups are stateful and act as virtual firewalls for EC2 instances. Use SGs to allow specific ports and source CIDRs. Example: allow TCP 443 from 0.0.0.0/0 and TCP 22 from your office IP.
  • Network ACLs (NACLs) are stateless and apply at the subnet level—useful for broader egress/ingress policies or to block specific IP ranges. Remember to add return rules for stateless NACLs.

Azure Network Security Groups (NSGs)

NSGs provide stateful packet filtering at subnet or NIC level. Recommended pattern: create NSG rules with priority numbers where lower numbers are evaluated first, set defaults to deny, and allow management ports only from trusted IPs.

GCP Firewall Rules

Google Cloud uses tag-based or service account–based rules. Create firewall rules bound to instances by target tags to minimize accidental exposure.

Common application scenarios and example policies

Below are practical, scenario-specific rule sets you can adapt.

Public web server (single VPS)

  • Allow inbound TCP 80 and 443 from any source.
  • Allow inbound TCP 22 only from admin CIDRs (or use SSH bastion with port forwarding).
  • Allow local loopback and established connections.
  • Block ICMP echo requests if you want to hide host availability (optional).
  • Implement rate-limiting rules for SSH and HTTP to mitigate brute force and DDoS (e.g., iptables recent module or nftables rate limits).

Database server in private subnet

  • Deny all inbound traffic by default.
  • Allow TCP 3306 (MySQL) or 5432 (Postgres) only from application server subnets or specific host IPs.
  • Use service accounts and cloud IAM to further restrict access where supported.

Management plane and bastion host

  • Expose SSH only on a bastion host with MFA and logging.
  • Restrict bastion access to a small set of admin IPs and require jump-host workflows rather than exposing database or application ports.

Advanced techniques: state tracking, rate limiting, and connection filtering

To harden firewalls beyond simple allow/deny:

  • Connection tracking (conntrack): Allow RELATED,ESTABLISHED to avoid opening extra ephemeral ports manually.
  • Rate limiting: Use nftables limit or iptables recent to limit new connections per IP, e.g., to mitigate brute-force SSH attempts.
  • Geo-blocking: Block entire countries by dropping CIDR ranges (use carefully; can be large lists).
  • Deep packet inspection/WAF: Combine with application-layer filters for SQLi/XSS protection.
  • Segmentation: Use VLANs, subnets, and separate security groups to isolate tiers.

Advantages comparison: host firewall vs network appliance vs cloud rules

Understanding trade-offs helps you choose the right combination:

  • Host-based firewall (iptables/nftables/UFW/firewalld)
    • Pros: Granular per-host control, easy for custom rules, local logging.
    • Cons: Management overhead across many hosts; less effective against volumetric DDoS.
  • Network appliance / virtual firewall (pfSense, FortiGate)
    • Pros: Centralized management, advanced features (IPS/IDS, VPN), suitable for on-prem and colo.
    • Cons: Cost, potential single point of failure if not architected highly available.
  • Cloud-native controls (Security Groups, NSGs)
    • Pros: Easy to apply at scale, integrated with cloud IAM and orchestration.
    • Cons: Limited deep-packet inspection; must be combined with host protections.

Operational best practices

  • Staged rollout: Test rules in a staging environment, use a maintenance window, and retain a recovery path (console access or cloud serial console) when changing firewall rules that may cut off management access.
  • Change management: Track rule changes in version control (store nftables/iptables configs) and document rationale and expiration for temporary rules.
  • Monitoring and alerting: Forward firewall logs (syslog/journald) to a central SIEM, correlate denied traffic spikes with other telemetry, and set alerts for anomalous patterns.
  • Automate: Use configuration management (Ansible, Terraform) to enforce consistent firewall policies across fleets and cloud environments.
  • Regular audits: Periodically review rules to remove stale exceptions and ensure principle of least privilege.

How to choose firewall tooling and products

Selection should be guided by scale, compliance, and operational model:

  • For single or a few VPS instances: host-based firewalls (UFW, nftables) are sufficient and lightweight.
  • For multi-host, multi-tier deployments: combine cloud-native security groups/NSGs with host firewalls and consider a central management plane.
  • For enterprise/regulated environments: look for features such as centralized logging, role-based access, HA appliances, IPS/IDS, and compliance reporting.
  • When selecting cloud providers or VPS vendors, verify the ability to configure security groups, private networking, and DDoS protections.

Performance note: Firewall rules are evaluated in order; avoid overly complex per-packet processing on high-throughput paths. Offload TLS termination and caching to reverse proxies to reduce load on origin servers and firewall appliances.

Summary

Enabling firewall rules is a practical balance between security and accessibility. Start with a default-deny posture, allow only required services and CIDRs, leverage stateful inspection, and combine host-level rules with cloud or network-layer protections for layered defense. Use structured change management, logging, and automation to maintain consistent, auditable policies.

For those running public-facing sites or services that need reliable, secure infrastructure, consider hosting on a VPS provider that offers flexible networking and control panels to manage firewall settings easily. Learn more about the hosting options at VPS.DO, including their US-based instances at USA VPS, which can be provisioned to work seamlessly with the firewall strategies described above.

Fast • Reliable • Affordable VPS - DO It Now!

Get top VPS hosting with VPS.DO’s fast, low-cost plans. Try risk-free with our 7-day no-questions-asked refund and start today!