• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to configure IPtables to open Ports in CentOS / RHEL

By admin

Most Linux distributions will default to running a host-based firewall, iptables. If you want your hosts to communicate with each other, you have two options: turn off iptables or configure iptables to allow communication. I prefer to leave iptables turned on and configure access. Keeping iptables is just another layer of your defense across the network. The post describes how to open or enable some port in CentOS/RHEL using.

Configuring iptables properly is a complicated task, which requires deep knowledge of networking. The example presented here is a simplification.

1. Check status of service tables and start if it is stopped

# service iptables status
# service iptables start
# chkconfig --list iptables
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# chkconfig iptables on

2. To check current iptables rules (below output shows currently no iptables rules set).

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

3. Add firewall (iptable) rule to allow incoming tcp port (for example, 22):

# iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT

4. List iptables to verify newly added rule.

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Repeat step 3 to continue adding ports to the Linux firewall (iptables)

Procedure to load rules after every reboot

1. Make sure iptables rules added using above procedure.

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

2. Save iptables to a file. File name in below command can be anything.

# iptables-save > /root/iptable_rules

3. Edit ‘/etc/rc.local‘ file add following entry to restore iptable rules after every reboot.

# iptables-restore < /root/iptable_rules

4. Save and close the file.

Filed Under: CentOS/RHEL 5, Linux, OEL 6

Some more articles you might also be interested in …

  1. Maintaining Linux filesystems using “fsck” and “tune2fs”
  2. LVM Configuration : Physical Volume (PV) Operations/Utilities
  3. Downgrading an rpm package to a lower version (using “rpm” command)
  4. What is the difference between the -i and -U options used in rpm command in Linux
  5. Linux “shutdown”, “poweroff”, “halt”, “reboot” Commands
  6. Time goes out of sync on a node running CentOS/RHEL 7
  7. –force V/s –nodeps : rpm command options to install or uninstall a package
  8. Understanding RPM Versions and Naming Schemes
  9. How to change the NIC device name in CentOS / RHEL 6
  10. The /var/log/messages is empty, and so are the rotated log files such as messages.0, messages.1

You May Also Like

Primary Sidebar

Recent Posts

  • MySQL: how to figure out which session holds which table level or global read locks
  • Recommended Configuration of the MySQL Performance Schema
  • MySQL: Identify what user and thread are holding on to a meta data lock that is preventing other queries from running
  • MySQL: How to kill a Long Running Query using max_execution_time
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary