• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Sunday, June 1, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System CentOS

Setup IPTables Firewall on CentOS 6

How VPS by How VPS
October 1, 2019
in CentOS
0
0
SHARES
31
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Introduction
  2. Prerequisites
  3. Step 1: Determine the services and ports used on your server
  4. Step 2: Configure iptables rules
  5. Step 3: Save the configurations
  6. Workarounds for accidental blockout
  7. Want to contribute?


Introduction

A firewall is a type of network security tool that controls the inbound and outbound network traffic according to its predefined rule set. We can use a firewall along with other safety measures to protect our servers from hackers’ pries and attacks.

The design of a firewall can be either dedicated hardware or a software program running on our machine. On CentOS 6, the default firewall program is iptables.

In this article, I will show you how to set up a basic iptables firewall based on the Vultr “WordPress on CentOS 6 x64” app, which will block all traffic except for web, SSH, NTP, DNS, and ping services. However, this is only a preliminary configuration which satisfies common security needs. You would need a more sophisticated iptables configuration if you have further requirements.

Note:

If you add an IPv6 address to your server, you should also set up the ip6tables service. Configuring ip6tables is outside of the scope of this article.

Unlike CentOS 6, iptables is no longer the default firewall program on CentOS 7, and has been replaced with a program called firewalld. If you are planning to use CentOS 7, you will need to set up your firewall using firewalld.

Prerequisites

Freshly deploy a server instance with the Vultr “WordPress on CentOS 6 x64” app, then log in as root.

Step 1: Determine the services and ports used on your server

I assume that this server will only host a WordPress blog, and it will not be used as a router or provide other services (for example, mail, FTP, IRC, etc.).

Here, we need the following services:

  • HTTP (TCP on port 80)
  • HTTPS (TCP on port 443)
  • SSH (TCP on port 22 by default, can be changed for security purposes)
  • NTP (UDP on port 123)
  • DNS (TCP and UDP on port 53)
  • ping (ICMP)

All other unnecessary ports will be blocked.

Step 2: Configure iptables rules

Iptables controls traffic with a list of rules. When network packets are sent to our server, iptables will inspect them using each rule in sequence and take actions accordingly. If a rule is met, the other rules will be ignored. If no rules are met, iptables will use the default policy.

All of the traffic can be categorized as INPUT, OUTPUT, and FORWARD.

  • INPUT traffic can be either normal or malicious, should be allowed selectively.
  • OUTPUT traffic is normally considered safe and should be allowed.
  • FORWARD traffic is useless and should be blocked.

Now, let’s configure the iptables rules according to our needs. All the following commands should be input from your SSH terminal as root.

Check the existing rules:

iptables -L -n

Flush all existing rules:

iptables -F; iptables -X; iptables -Z

Since changes to iptables configuration will take effect immediately, if you misconfigure the iptables rules, you may become blocked out of your server. You can prevent accidental blockouts with the following command. Remember to replace [Your-IP-Address] with your own public IP address or IP address range (for example, 201.55.119.43 or 201.55.119.0/24).

iptables -A INPUT -s [Your-IP-Address] -p tcp --dport 22 -j ACCEPT

Allow all loopback (lo) traffic and drop all traffic to 127.0.0.0/8 other than lo:

iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -d 127.0.0.0/8 -j REJECT

Block some common attacks:

iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

Accept all established inbound connections:

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Allow HTTP and HTTPS inbound traffic:

iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Allow SSH connections:

iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Allow NTP connections:

iptables -A INPUT -p udp --dport 123 -j ACCEPT

Allow DNS queries:

iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT

Allow ping:

iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT

At last, set the default policies:

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

Step 3: Save the configurations

Each of the changes that we made above have taken effect, but they are not permanent. If we don’t save them to hard disk, they will be lost once the system reboots.

Save the iptables configuration with the following command:

service iptables save

Our changes will be saved in the file /etc/sysconfig/iptables. You can review or modify the rules by editing that file.

Workarounds for accidental blockout

If you are blocked out of your server due to a configuration mistake, you can still regain your access with some workarounds.

  • If you haven’t saved your modifications to iptables rules yet, you can restart your server from the Vultr website interface, then your changes will be dropped.
  • If you have saved your changes, you can log in your server through the console from the Vultr website interface, and input iptables -F to flush all of the iptables rules. Then you can set up the rules again.

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article
Previous Post

How to Install Wiki.js on CentOS 7

Next Post

Let’s Encrypt on cPanel

Next Post

Let's Encrypt on cPanel

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet