Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Iptables use examples     - Linux formatted partition error Could not stat / dev / sda No such file or directory Solution (Linux)

- To create a full command line Android Build System (Linux)

- Access.log Apache access log analysis and how to import it into MySQL (Server)

- Neo4j map data processing tab (Database)

- Using RAID in Linux: Create a RAID 5 (Linux)

- Lua regex (string function) (Programming)

- Linux see whether there is a hacker program (Linux)

- Linux System Getting Started Learning: The Linux ac command (Linux)

- Struts2 study notes -Valuestack (value stack) and OGNL expression (Programming)

- The Linux role of each directory contents (Linux)

- How to use SHA256 checksum files download (Linux)

- Ubuntu Server security risk checks (Linux)

- Cygwin install Python MySQLdb (Linux)

- Boost - Memory Management - smart pointers (Programming)

- Ubuntu 15.10 install the latest Arduino IDE 1.6.7 (Linux)

- OGG-03510 Problem (Database)

- Run two MySQL service on one server (Database)

- Ubuntu apt-mirror established local private sources (Linux)

- KVM virtualization nested configuration (Server)

- Perl loop (Programming)

  Iptables use examples
  Add Date : 2017-08-31      
  This article only writes iptables using the format method;

Iptables use the format;

Iptables -L -n -v --line-numbers Displays the rule serial number. If you need to delete the rule, simply delete the number.

Iptables -t filter -L -n Show the current default rule chain
[Root @ www ~] # iptables-t filter-L -n
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
Iptables [-t table] -N chain Creates a custom rule chain
Example; iptables -N test
Note: [- t table] is the meaning of the table, the new rules in the creation of the time can not refer to, if that means that the creation of a new chain of rules only in the table this table some rules are cited or Jump

Iptables [-t table] -X Delete the custom chain of rules
Example; iptables -X test
Iptables [-t table] -E old custom chain name The new defined chain name
Example; iptables -E test newtest
Iptables [-t table] -P chain target Specifies the default policy for the chain, specifying default rules
Example; the system is currently the default chain Chain FORWARD (policy ACCEPT) modified to DROP

Iptables [-t table] {-F | -L | -Z} [chain [rulenum]] [options ...]

[Root @ www ~] # iptables-t filter-L -n -v
Chain INPUT (policy ACCEPT 774 packets, 105K bytes) Inbound is allowed by default
Pkts bytes target opt opt in out source destination
Chain Forwarding (policy DROP 0 packets, 0 bytes) Forward the default deny
Pkts bytes target opt opt in out source destination
Chain OUTPUT (policy ACCEPT 65 packets, 5404 bytes) Outbound is allowed by default
Pkts bytes target opt opt in out source destination
-F: Clear the rules in the chain
Rules are numbered, starting from the top in the chain, starting at 1;
-L: list, lists all the rules in the table;
-n: Numeric format displays IP and Port
-v: Displays in verbose format

[Root @ www ~] #iptables -L -n -a --line-numbers

Pkts bytes target opt opt in out source destination
Pkts: packets, the number of packets matching the rule.
Bytes: The sum of the sizes of all packets matched by this rule, unit conversion is performed.
Target: the target, that is, processing mechanism;
Prot: Protocol, generally {TCP | UDP | ICMP};
Opt: Optional
In: The incoming interface of the packet.
Out: Outgoing interface of the packet.
Source: Source address;
Destination: Destination address;

Ptables [-t table] {-A | -D} chain rule-specification
-A: append, attach a rule
Matching conditions - j processing mechanism

Matching conditions: Instance demonstration

Note: iptables from the top-to-back match, if you open a port service in advance, and then want to close, you need to write the closure rules written to open in front of

-s: match the original address, you can IP, you can also network address; you can use the! Operator inversion,!; -s equivalent to - src, or - source
D: matches the destination address
Example; iptables -t filter -A INPUT -s -d -j ACCEPT
Iptables -t filter -A OUTPUT -s -d -j ACCEPT
-p: match the agreement, usually only use {TCP | UDP | ICMP} one of three;
Iptables -A INPUT -i eth1 -d -p icmp -j REJECT

-i: Data packets into the interface; usually only used for INPUT, FORWARD and PREROUTING

-o: outflow interface; usually only used for OUTPUT, FORWARD and POSTROUTING

Iptables -A INPUT -i eth1 -d -j ACCEPT
Iptables -A OUTPUT -o eth1 -s -j ACCEPT
Udp | icmp} to specify a specific protocol, the protocol can be extended automatically; -p {tcp | udp |
-p tcp
- port; Match the target port, can be a number of consecutive ports;
- port; Match the source port, but also is a number of consecutive ports;

Example; release the host from the network ssh service on the local request; The following example is a pair of appear; allows all requests from the network segment to send ssh services
Iptables -A INPUT -s -d -p tcp -dport 22 -j ACCEPT host ssh service, allowing only the response to the this segment
Iptables -A OUPOUT -s -d -p tcp -sport 22 -j ACCEPT
Set the default policy, deny all unknown services, only release rules match the service
Iptables -P INPUT DROP
The importation of these two conditions I have not interrupted the host, because the above written in advance to allow the machine ssh two service rules, there is no interruption, but the other services of the plane, other hosts can not visit.
80 port of the machine to visit all the address (then clear all the addresses, so write the rules when the source address is omitted write, write directly to the destination address)
Iptables-A INPUT-d tcp --dport 80-j ACCEPT only write this one, access to the message can come in, but can not respond to the customer, so there is a ring to write to the client host rule
Iptables -A OUTPUT -s -p tcp -sport 80 -j ACCEPT At this time the source address is is to the client host response


-p icmp

- icmp-type
8: ping request 0: ping response
Example: Freeing other hosts
Iptables -A OUTPUT -s -p icmp -icmp-type 8 -j ACCEPT
Iptables -A INPUT -d -p icmp -icmp-type 0 -j ACCEPT
Release other hosts to ping the machine
Iptables -A INPUT -d -P icmp -icmp-type 8 -j ACCEPT
Iptables -A OUTPUT -s -p icmp -icmp-type 0 -j ACCEPT

-p udp

Tftp release of the machine services: (There are two tcp not write)
Iptables -A INPUT -s -d -p udp -dport 69 -j ACCEPT
Iptables -A OUTPUT -s -d -p udp -sport 69 -j ACCEPT
Dns local services: (DNS total of eight rules, there are four tcp not write)
Iptables -A INPUT -s -d -p udp -dport 53 -j ACCEPT
Iptables -A OUTPUT -s -d -p udp -sport 53 -j ACCEPT
Iptables -A OUTPUT -s -p udp --dport 53 -j ACCEPT
Iptables -A INPUT -d -p udp --sport 53 -j ACCEPT



Show extensions

Rule command
Iptables-D INPUT 2 that delete the second rule on the input chain Remember that if the deletion of the second article and the original article will automatically become the second article
Iptables -I Insert rule
Iptables -I INPUT 2 -i lo -j ACCEPT Indicates that the rule is inserted into the second line
Iptables -R INPUT 1 -s -d -i eth0 -p tcp --dport 22 -j ACCEPT Completely replace the first rule
Clearly specify the 172.16 network segment to connect the target address of the 22 services must come from the eth0 card
Iptables -S INPUT Displays the rules for the specified chain



Display extensions; extensions that must be explicitly specified;

-m Extension module name - Private option 1 - Private option 2
Multiport; Multi-port matching, more than one designated (15 or less) discrete port
--source-ports, the source port
--destinatil-ports The destination port
Example; open the machine's 22,80 two ports; (because here one-time open multiple ports all here to use the-m option)
Iptables -I INPUT -d -p tcp-m multiport -dports 22,80 -j ACCEPT Here is the input so that the user may send a request and the target host does not get a response, all write a output rule outbound, To respond to the client port. (Write the two rules written in front of the two separate written rules can be deleted (this rule with the "-I" option to insert the rules), this also reached the effect of rule optimization)

Iptables -I OUTPUT -s -p tcp -m multiport --sports 22,80 -j ACCEPT
Iptables-A INPUT -i eth0-m multiport-p tcp --dports 53,113,135,137,139,445-j DROP This plane through the eth0 NIC port have been rejected
Iptables -A INPUT -i eth0 -m multiport -p udp --dports 53,113,135,137,139,445 -j DROP
Iptables -A INPUT -i eth0 -p udp --dport 1026 -j DROPiptables -A INPUT -i eth0 -m multiport -p tcp --dports 3389,4899 -j DROP
Examples; allow the machine's telnet service to the specified host ip
Iptables -A INPUT -d -p tcp -dport 23 -m iprange --src-range -j ACCEPT
Iptables -A OUTPUT -s -p tcp -sport 23 -m iprange -dst-range -j ACCEPT

Matches the specified time range End time is greater than the start time The end date is on the start date
--datestart Start Date YYYY [-MM [-DD [Thh [: mm [: ss]]]]]
--datestop End date YYYY [-MM [-DD [Thh [: mm [: ss]]]]]
--timestart hh: mm [: ss] start time
--timestop hh: mm [: ss] End time
--weekdaysday [Mon, Tue, Wed, Thu, Fri, Sat, Sun] can also be 1-7 weeks for the date format rules
Example: Restricting Local Port 901 is only allowed from 08:00 to 18:00 during business hours (Monday to Friday);
Iptables -A INPUT -d -p tcp -dport 901 -m time --weekdays Mon, Tue, Wed, Thu, Fri --timestart 08:00:00 -timestop 18:00:00 -j ACCEPT
The following rule can be abbreviated because the customer has restricted access rules to the customer so that the customer only has access to the specified working day.
(The server can release the user's request at any time, but the client is not always available.)
Iptables -A OUTPUT -s -p tcp -sport 901 -j ACCEPT

String; Match the string
--algo {bm | kmp}; The algorithm used for character matching lookups
--string "STRING"; String to find
Example; if the client port to access the web page where there are rules defined in the string is not displayed to the customer
Install the httpd service, the new two home page file, write a different content, one of the contents of the home page file you need to match the string, such as "hello." Test the two pages of the web service without writing rules before Can be normal access. On one web page to do a string matching.

Iptables -I OUTPUT -s -p tcp -sport 80 -m string -algo kmp --string "hello" -f DROP

After the completion of the rules to match the string again after the visit is displayed can not be opened to match the page.
- Btrfs file system repair techniques (Linux)
- Linux platform NTOP Installation and Configuration (Linux)
- 256 with rich colors decorate your terminal (Linux)
- Ubuntu 14.04 LTS installed Hadoop 1.2.1 (distributed cluster mode) (Server)
- Installation and Configuration ISC DHCP server on Debian Linux (Server)
- High-performance Linux system firewall detailed analysis of double-effect (Linux)
- Number JavaScript type system (Programming)
- The Linux OOM Terminator (Server)
- Android View event delivery (Programming)
- How to properly set up a Linux swap partition (Linux)
- Linux System Getting Started Learning: the curl set custom HTTP header (Linux)
- Linux server network penetration testing (Linux)
- How to test your MongoDB application upgrade? (Database)
- SSH service configuration to use a certificate login Linux server (Server)
- Linux excellent text editor (Markdown, LaTeX, MathJax) (Linux)
- Import and export myloader accelerate mydumper (Database)
- The ORA-01113 error is handled with BBED without archiving (Database)
- Lua study notes under ubuntu (Linux)
- Oracle ORA-01089 failure analysis (Database)
- Redis is installed and set up Ubuntu 14.04 from the environment under the main ssdb (Server)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.