• Home
  • Disclaimer
  • Contact
  • Archives
  • About
  • Subscribe
  • Support
  • Advertise

Kernel Talks

Unix, Linux & scripts.

  • How-to guides
    • Howto
    • Disk management
    • Configurations
  • OS
    • HPUX
    • Linux
  • Commands & tools
    • Commands
    • Software & Tools
    • System services
  • Cloud computing
    • AWS CSA associate quiz
    • AWS CSA preparation guide!
    • Cloud Services
  • Tips & Tricks
  • Linux commands
You are here: Home / Networking

Configuration of iptables policies

Published: June 13, 2017 | Modified: June 13, 2017 | 2029 views




Configuring iptables policies in Linux
Configuring iptables policies in Linux

Learn configuration of iptables policies in Linux. Know how to add, delete, save Linux native firewall rules in iptables.

 


In our last article about of iptables, we have seen basics of iptables, iptables chains and chain policy strategy. In this article we will walk through how to define iptables policies.

Defining iptables policies means allowing or blocking connections based on their direction of travel (incoming, outgoing or forward), IP address, range of IP addresses and ports. Rules are scanned in order for all connections until iptables gets a match. Hence you need to decide and accordingly define rule numerically so that its gets match first or later than other rules.

In newer version like RHEL7, firewall is still powered by iptables only management part is being handled by new daemon called firewalld.

iptables is the command you need to use to define policies. With below switches –

  • -A : To append rule in existing chain
  • -s : Source
  • -p : Protocol
  • –dport : service port
  • -j : action to be taken

Lets start with examples with commands.

Block/Allow single IP address

To block or allow single IP address follow below command where we are adding rule (-A) to input chain  (INPUT) for blocking (-j REJECT).

Shell
1
2
3
4
5
6
7
8
9
10
 
# iptables -A INPUT -s 172.31.1.122 -j REJECT
 
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  ip-172-31-1-122  anywhere            reject-with icmp-port-unreachable
 
----- output clipped -----
 

In above command we are blocking incoming connections from IP 172.31.1.122. If you see output of rules listing, you can see our rule is defined properly in iptables. Sine we havnt mentioned protocol, all protocols are rejected in rule.

Here chain can be any of the three : input (incoming connection), output (outgoing connection) or forward (forwarding connection). Also, action can be accept, reject or drop.


Block/Allow single IP address range

Same as single IP address, whole address range can be defined in rule too. Above command can be used only instead of IP address you need to define range there.

Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
# iptables -A INPUT -s 172.31.1.122/22 -j REJECT
 
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  172.31.0.0/22        anywhere            reject-with icmp-port-unreachable
 
# iptables -A INPUT -s 172.31.1.122/255.255.254.0 -j REJECT
 
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  172.31.0.0/22        anywhere            reject-with icmp-port-unreachable
REJECT     all  --  172.31.0.0/23        anywhere            reject-with icmp-port-unreachable
 

I have shown two different notation types to define IP address range/subnet. But if you observe while displaying rules iptables shows you in /X notation only.

Again action and chain can be any of the three of their types as explained in previous part.


 Block/Allow specific port

Now, if you want to allow/block specific port then you need to specify protocol and port as shown below :

Shell
1
2
3
4
5
6
7
8
 
# iptables -A INPUT -p tcp --dport telnet -s 172.31.1.122 -j DROP
 
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  172.31.1.122         anywhere            tcp dpt:telnet
 

Here in this example we blocked telnet port using tcp protocol from specified source ip. You can choose chain and action of your choice depending on which rule you want to configure.


Saving iptables policies

All the configuration done above is not permanent and will be washed away when iptable services restarted or server reboots. To make all these configured rules permanent you need to write these rules.  This can be done by supplying save argument to iptables service (not command!)

Shell
1
2
3
4
 
# /sbin/service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
 

If you open up /etc/sysconfig/iptables file you will see all your rules saved there.

Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.7 on Tue Jun 13 01:06:01 2017
*filter
:INPUT ACCEPT [32:2576]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [48:6358]
-A INPUT -s 172.31.1.122/32 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 172.31.0.0/22 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 172.31.0.0/23 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -s 172.31.1.122/32 -p tcp -m tcp --dport 23 -j DROP
COMMIT
# Completed on Tue Jun 13 01:06:01 2017
 


Deleting rule in iptables

We have seen how to add rule, now here learn how to delete existing rule. You can use same commands used above only change is to add -D switch instead of -A!

Shell
1
2
3
 
# iptables -D INPUT -s 172.31.1.122 -j REJECT
 

Above command will remove the very first rule we added in iptables in this post.

Also, if you havnt saved your iptables you can flush all currently configured rules by using -F.

Shell
1
2
3
 
# iptables -F
 


 

⇠ Previous article
Basics of iptables – Linux firewall
Next article ⇢
Command alias in Linux, Unix

Related stuff:

  • 4 step Network bonding / teaming configuration in Linux
  • Basics of iptables – Linux firewall
  • How Docker container DNS works
  • Network routes in Linux

Filed Under: Networking Tagged With: how to configure iptables, how to define iptables policies, how to define iptables rule, how to save iptables, iptables rules, my iptables rules are not saved

If you like my tutorials and if they helped you in any way, then

  • Consider buying me a cup of coffee via paypal!
  • Subscribe to our newsletter here!
  • Like KernelTalks Facebook page.
  • Follow us on Twitter.
  • Follow our Google+ page.
  • Add our RSS feed to your feed reader.

Share Your Comments & Feedback: Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get fresh content from KernelTalks

  • Email
  • Facebook
  • Google+
  • RSS
  • Twitter

Popular posts

  • How to rescan disk in Linux after extending vmware disk
  • mount.nfs: requested NFS version or transport protocol is not supported
  • 5 steps guide for SMTP configuration in Linux
  • How to configure NTP client in Linux
  • 4 ways to check size of physical memory (RAM) in Linux
  • How to reset iptables to default settings
  • How to resolve mount.nfs: Stale file handle error
  • How to setup domain name in Linux server
  • How to enable repository using subscription-manager in RHEL
  • Enable debugging to log NFS logs in Linux

Get Linux & Unix stuff right into your mailbox

Join with other 445 subscribers to get our weekly newsletter in your inbox. Its FREE and you can unsubscribe at anytime!

This work is licensed under a CC-BY-NC license · Privacy Policy
© Copyright 2016-2017 KernelTalks · All Rights Reserved.
The content is copyrighted to Shrikant Lavhate & can not be reproduced either online or offline without prior permission.

  • check_mk error Cannot fetch deployment URL via curl error
  • Understanding /etc/passwd file
  • How to download package using YUM or APT
  • Dynamic Root Disk DRD configuration in HPUX
  • Cowsay : Fun in Linux terminal
  • Nginx installation on Linux server
  • Basics of iptables – Linux firewall
  • How to install EC2 Linux server in AWS with screenshots
  • How to zip, unzip files and directories in Linux / Unix
  • xsos : a tool to read sosreport in RHEL/CentOS