A brief to various iptables commands
We will use an example based approach to examine the various iptables commands. In this first example, we will create a very simple set of rules to set up a Stateful Packet Inspection (SPI) firewall that will allow all outgoing connections but block all unwanted incoming connections:
# iptables -F # iptables -A INPUT -p tcp --dport 22 -j ACCEPT # iptables -P INPUT DROP # iptables -P FORWARD DROP # iptables -P OUTPUT ACCEPT # iptables -A INPUT -i lo -j ACCEPT # iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # iptables -L -v
which should give the following output:
Â
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- lo any anywhere anywhere
0 0 ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destinationNow lets look at each of the 8 commands above in turn and understand exactly what we’ve just done:
iptables -FÂ First of all we’ve used the -F switch to flush all existing rules so we start with a clean state from which to add new rules.
iptables -A INPUT -p tcp –dport 22 -j ACCEPTÂ Here we add a rule allowing SSH connections over tcp port 22. This is to prevent accidental lockouts when working on remote systems over an SSH connection. We will explain this rule in more detail later.
iptables -P INPUT DROPÂ The -P switch sets the default policy on the specified chain. So here we’ve set the default policy on the INPUT chain to DROP. This means that if an incoming packet does not match one of the following rules it will be dropped. If we were connecting remotely via SSH and had not added the rule above, we would have just locked ourself out of the system at this point.
iptables -P FORWARD DROPÂ Similarly, here we’ve set the default policy on the FORWARD chain to DROP as we’re not using our computer as a router so there should not be any packets passing through our computer.
iptables -P OUTPUT ACCEPTÂ and finally, we’ve set the default policy on the OUTPUT chain to ACCEPT as we want to allow all outgoing traffic (as we trust our users).
iptables -A INPUT -i lo -j ACCEPTÂ Now it’s time to start adding some rules. We use the -A switch to append (or add) a rule to a specific chain, the INPUT chain in this instance. Then we use the -i switch (for interface) to specify packets matching or destined for the lo (localhost, 127.0.0.1) interface and finally -j (jump) to the target action for packets matching the rule – in this case ACCEPT. So this rule will allow all incoming packets destined for the localhost interface to be accepted. This is generally required as many software applications expect to be able to communicate with the localhost adaptor.
iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPTÂ This is the rule that does most of the work, and again we are adding (-A) it to the INPUT chain. Here we’re using the -m switch to load a module (state). The state module is able to examine the state of a packet and determine if it is NEW, ESTABLISHED or RELATED. NEW refers to incoming packets that are new incoming connections that weren’t initiated by the host system. ESTABLISHED and RELATED refers to incoming packets that are part of an already established connection or related to and already established connection.
iptables -L -v Finally, we can list (-L) the rules we’ve just added to check they’ve been loaded correctly.
Finally, the last thing we need to do is save our rules so that next time we reboot our computer our rules are automatically reloaded:
Â
# /sbin/service iptables save
This executes the iptables init script, which runs /sbin/iptables-save and writes the current iptables configuration to /etc/sysconfig/iptables. Upon reboot, the iptables init script reapplies the rules saved in /etc/sysconfig/iptables by using the /sbin/iptables-restore command.
Obviously typing all these commands at the shell can become tedious, so by far the easiest way to work with iptables is to create a simple script to do it all for you. The above commands may be entered into your favourite text editor and saved as myfirewall, for example:
Â
#!/bin/bash # # iptables example configuration script # # Flush all current rules from iptables # iptables -F # # Allow SSH connections on tcp port 22 # This is essential when working on remote servers via SSH to prevent locking yourself out of the system # iptables -A INPUT -p tcp --dport 22 -j ACCEPT # # Set default policies for INPUT, FORWARD and OUTPUT chains # iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # # Set access for localhost # iptables -A INPUT -i lo -j ACCEPT # # Accept packets belonging to established and related connections # iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # # Save settings # /sbin/service iptables save # # List rules # iptables -L -v
Note:Â We can also comment our script to remind us what were doing.
now make the script executable:
Â
# chmod +x myfirewall
We can now simply edit our script and run it from the shell with the following command:
Â
# ./myfirewall
This Article has been referred from http://wiki.centos.org
accesses, base, bases, block, byte, check, command, community, computer, configuration, configure, configured, connection, create, determine, edit, edition, execution, firewall, Generator, hosts, incoming, interface, listing, policy, port, ports, prevent, remote, router, sbin, scripts, server, servers, service, services, settings, shell, source, ssh, switch, system, systems, targeted, traffic, understanding, user, what
4 Responses to “A brief to various iptables commands”
Leave a Reply Cancel reply
Categories
- Alerts & News
- Android
- Anti-Malware
- Anti-Spam/Virus
- Anti-Spyware
- Anti-Virus
- Apache
- Archiving
- Articles
- Audio and Video
- AWS
- Backup
- Backup
- Braindumps
- Browsers
- Browsers and Plugins
- Build Tools
- Cacti
- CentOS
- Cisco
- Cisco IOS Command Reference
- Citrix NetScaler
- Cleaning and Tweaking
- Clustering
- Clustering
- Compilers
- Data recovery
- Database
- Database
- Databases
- Debuggers
- Dell
- Dell
- Desktops
- Developer
- DHCP
- DNS
- Drivers Download
- Drupal
- Editors
- Encryption Software
- Exchange Server
- Fedora
- File Management
- firefox
- Firefox Add-ons
- Firewall
- Free ebooks
- Free software downloads
- Free Tools
- FTP
- FTP
- Games
- Google Plus
- Graphics
- Hacking
- Hardware
- Help Desk software
- High-Availability
- HP
- HP
- IIS
- Interpreters
- iphone
- IT-Tips
- LAMP
- Languages
- Laptops
- Latest Product & Technologies
- Linux
- Linux Commands
- Load Balancer
- Mac
- Memcached
- Messaging and Chat
- miscellaneous
- Mobile Development
- Mobiles
- Monitoring
- MySQL
- NAS
- Networking
- Networking
- Networking Software
- Office and News
- Open Source
- OpenSource Applications
- openSUSE
- Oracle
- PHP
- Popular downloads
- PostgreSQL
- Project Management Software
- Qmail
- Red Hat
- Remote Access
- Repository
- Sci-Tech Education
- Security
- Security
- Sendmail
- SEO
- SEO Tools
- Server OS
- Service Pack
- social networking
- Software Development
- solr
- SQL
- Squid
- Storage
- Sun Solaris
- System Tuning
- Tech Guru
- Technology News
- Testing
- Ubuntu
- Utilities
- Version Control
- Video Tutorial
- Virtual Machine Applications
- Virtualization
- VoIP
- VPN
- Web-related
- web-related
- Websense
- Website Promotion
- Windows
- Windows Commands
- Zend Studio
Recent Posts
- The best memcached Monitoring tool
- Is Thin Clients are Useful for Call center ?
- A new Email Service from Microsoft called Outlook.com
- How to Detect Back Links from spam websites
- How to Ignore Replication errors in MySQL

Wow. Thank you for such a great text!! It is very informative. Na Mcginity
Great post. I was checking continuously this weblog and I am inspired!
Extremely useful info particularly the final section :
) I handle such information a lot. I used to be seeking this certain information for a very lengthy time.
Thanks and good luck.
Wow, wonderful blog format! How lengthy have you ever been blogging for?
you made running a blog glance easy. The total look of your website is excellent, let alone the content!
Thanks for sharing this text and resources its really help full for my toughts. Thu Deldonno