Iptables add, delete, view, and modify

Source: Internet
Author: User
Iptables is a built-in firewall in linux, which is powerful and takes some time to learn. The following are some records of iptables.

Iptables is a built-in firewall in linux, which is powerful and takes some time to learn. The following are some records of iptables. If you are not familiar with iptables, you can use the apt. it is a firewall based on iptables and is quite useful. For more information, see install and configure the linux apt firewall.


1. install and start the firewall

  1. [Root @ linux ~] #/Etc/init. d/iptables start
[root@linux ~]# /etc/init.d/iptables start

When we use iptables to add and save rules, these rules are stored on the disk as files exist. for example, if centos is used, the file address is/etc/sysconfig/iptables, you can add, modify, and delete rules using commands, or directly modify the/etc/sysconfig/iptables file.

2. add firewall rules

1. add a filter table

View copy print?
  1. [Root @ linux ~] # Iptables-a input-p tcp-m tcp -- dport 21-j ACCEPT // open port 21
[Root @ linux ~] # Iptables-a input-p tcp-m tcp -- dport 21-j ACCEPT // open port 21

I have open iptables-P OUTPUT ACCEPT, so there is no need to open the port at the exit.

2. add a nat table

  1. [Root @ linux ~] # Iptables-t nat-a postrouting-s 192.168.10.0/24-j MASQUERADE
[root@linux ~]# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j MASQUERADE

Disguise the IP address of a packet whose source address is 192.168.10.0/24

3.-A is inserted to the end by default. you can insert-I to the specified position.

View copy print?
  1. [Root @ linux ~] # Iptables-I INPUT 3-p tcp-m tcp -- dport 20-j ACCEPT
  2. [Root @ linux ~] # Iptables-L-n -- line-number
  3. Chain INPUT (policy DROP)
  4. Num target prot opt source destination
  5. 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
  6. 2 DROP icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 8
  7. 3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 20 //-I
  8. 4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 22
  9. 5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 80
  10. 6 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED, ESTABLISHED
  11. 7 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID, NEW
  12. 8 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 21 //-A is inserted to the end by default
  13. Chain FORWARD (policy ACCEPT)
  14. Num target prot opt source destination
  15. Chain OUTPUT (policy ACCEPT)
  16. Num target prot opt source destination
[Root @ linux ~] # Iptables-I INPUT 3-p tcp-m tcp -- dport 20-j ACCEPT [root @ linux ~] # Iptables-L-n -- line-numberChain INPUT (policy DROP) num target prot opt source destination1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/02 DROP icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 83 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 20 //-I 4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 225 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 806 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED, ESTABLISHED7 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID, NEW8 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 21 //-A is inserted into the Chain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination by default

III. check iptable rules

1. view the filter table

View copy print?
  1. [Root @ linux ~] # Iptables-L-n -- line-number | grep 21 // -- line-number indicates the sequence number of the rule, which is convenient for deletion.
  2. 5 ACCEPT tcp -- 192.168.1.0/24 0.0.0.0/0 tcp dpt: 21
[Root @ linux ~] # Iptables-L-n -- line-number | grep 21 // -- line-number indicates the sequence number of the rule, it is easier to delete 5 ACCEPT tcp -- 192.168.1.0/24 0.0.0.0/0 tcp dpt: 21

If-t is not added, the filter table is used by default.

2. view the nat table

  1. [Root @ linux ~] # Iptables-t nat-vnL POSTROUTING -- line-number
  2. Chain POSTROUTING (policy ACCEPT 38 packets, 2297 bytes)
  3. Num pkts bytes target prot opt in out source destination
  4. 1 0 0 MASQUERADE all -- ** 192.168.10.0/24 0.0.0.0/0
[root@linux ~]# iptables -t nat -vnL POSTROUTING --line-numberChain POSTROUTING (policy ACCEPT 38 packets, 2297 bytes)num   pkts bytes target     prot opt in     out     source               destination1        0     0 MASQUERADE  all  --  *      *       192.168.10.0/24      0.0.0.0/0

4. modify rules

View copy print?
  1. [Root @ linux ~] # Iptables-r input 3-j DROP // change rule 3 to DROP
[Root @ linux ~] # Iptables-r input 3-j DROP // change rule 3 to DROP

5. delete iptables rules

View copy print?
  1. [Root @ linux ~] # Iptables-d input 3 // delete the 3rd rules of input
  2. [Root @ linux ~] # Iptables-t nat-d postrouting 1 // delete the first rule of postrouting in the nat table
  3. [Root @ linux ~] # Iptables-f input // clear all INPUT rules of the filter table
  4. [Root @ linux ~] # Iptables-F // clear all rules
  5. [Root @ linux ~] # Iptables-t nat-f postrouting // clear all the rules of the nat table POSTROUTING
[Root @ linux ~] # Iptables-d input 3 // delete the 3rd rules of input [root @ linux ~] # Iptables-t nat-d postrouting 1 // delete the first rule of postrouting in the nat table [root @ linux ~] # Iptables-f input // clear all INPUT rules of the filter table [root @ linux ~] # Iptables-F // clear all rules [root @ linux ~] # Iptables-t nat-f postrouting // clear all the rules of the nat table POSTROUTING

6. set default rules

View copy print?
  1. [Root @ linux ~] # Iptables-p input drop // set the default INPUT rule of the filter table to DROP.
[Root @ linux ~] # Iptables-p input drop // set the default INPUT rule of the filter table to DROP.

Save all added, deleted, and modified files,/etc/init. d/iptables save. the above is only some of the most basic operations. to use it flexibly, it takes some time for actual operations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.