InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Networking tutorials

  • DIG quick start tutorial
  • Dig - list all dns records
  • How to view wifi connection speed on Mac
  • Linux iptables - Nat port forwarding using PREROUTING
  • Linux iptables and ip6tables examples
  • Ngrep - quick start guide
  • Use nc to check if a remote port is reachable
  • Use nc to listen to tcp or udp port
  • Use nc to test HTTP url redirection
  • add more DNS servers to Wi-Fi interface on Mac
  • locally override website host to IP mapping
  • use nc to print headers from browser
 
  • Home
  • > Tutorials
  • > Networking

Linux iptables and ip6tables examples

on Aug 15, 2016

Linux iptables is a user-space application program that allows a system administrator to configure the tables provided by the Linux kernel firewall (implemented as different Netfilter modules) and the chains and rules it stores.

Few points to note about iptable rules:

  1. First matching rules applies in case multiple rules match.
  2. Rules added using iptables are not persisted automatically. These will be lost if system reboots.

Here are some handy examples using iptables for IPv4 (for IPv6 use ip6tables, iptables-save, iptables-restore)

List iptable rules

  1. List all rules
    ## -n (numeric) -v (verbose)
    $ sudo iptables -L -n -v
    Chain INPUT (policy ACCEPT 465 packets, 33446 bytes)
     pkts bytes target     prot opt in     out     source               destination         
        4   220 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    
    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
     pkts bytes target     prot opt in     out     source               destination         
    
    Chain OUTPUT (policy ACCEPT 345 packets, 74880 bytes)
     pkts bytes target     prot opt in     out     source               destination 
    
  2. List specific chain rules (INPUT/FORWARD/OUTPUT)
    ## -n (numeric) -v (verbose)
    $ sudo iptables -L INPUT -n -v
    Chain INPUT (policy ACCEPT 456 packets, 32854 bytes)
     pkts bytes target     prot opt in     out     source               destination         
        4   220 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    
  3. List all rules with line numbers
    $ sudo iptables -n -L -v --line-numbers
    

Save and restore iptables

  1. Save iptables to a file
    $ sudo iptables-save > iptable_filename
    
  2. Load iptables frm a file
    $ sudo iptables-restore < iptable_filename
    

Add (Append) iptable rules

  1. Port based rules
    Allow only connection to port 22, 80 and 443

    $ sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    $ sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    $ sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    $ sudo iptables -A INPUT -j DROP
    $ sudo iptables -n -L INPUT -v --line-numbers
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num   pkts bytes target     prot opt in     out     source               destination         
    1      257 18272 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    2        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:443
    4        0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0       
    
  2. IP based rules
    Allow a port access from an IP

    $ sudo iptables -A INPUT -p tcp --dport 8081 -s 172.30.0.211 -j ACCEPT
    

Insert a rule before a specific numbered rule

To insert a rule before rule number 4 in INPUT chain

$ sudo iptables -I INPUT 4 -p tcp --dport 8081 -j ACCEPT

Delete iptable rules

  1. Delete a specific numbered rule
    $ sudo iptables -D INPUT 4
    
  2. Delete/Flush all rules
    $ sudo iptables -F
    

Suggested posts:

  1. Install google camera on Asus m1 max pro
  2. Java – Arrays.sort custom Comparator example
  3. AngularJS ng-if vs ng-hide/ng-show
  4. DIG quick start tutorial for DNS Lookup
  5. WordPress – query to dump all categories
  6. document querySelector examples
  7. Bash check if file begins with a string
  8. How to locally override website domain (or hostname) to IP mapping
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, Networking, Tutorials, Unix
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress