Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of methods for viewing, adding, deleting and modifying iptables rules of linux Firewall

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. View

Iptables-nvL-- line-number

-L view all the rules of the current table. The default is the filter table. If you want to view the NAT table, you can add the-t NAT parameter.

-n does not reverse check the ip address, plus this parameter shows that the speed will be much faster

-v outputs details, including the number of packets passing through the rule, the total number of bytes, and the corresponding network interface

-- line-number displays the serial number of the rule, which is used when deleting or modifying the rule

2. Add

Adding a rule has two parameters:-An and-I. Where-An is added to the end of the rule;-I can be inserted at the specified location, or by default at the beginning of the rule if there is no specified location.

Current rules:

[root@test] # iptables-nL-- line-numberChain INPUT (policy ACCEPT) num target prot opt source destination1 DROP all-- 192.168.1.1 0.0.0.0 DROP all 02 DROP all-- 192.168.1.2 0.0.0.0 DROP all-- 192.168.1.4 0.0.0.0 Universe

Add a rule to the tail:

[root@test] # iptables-An INPUT-s 192.168.1.5-j DROP

Insert a rule into the third line and write the number of lines directly to the end of the rule chain:

[root@test] # iptables-I INPUT 3-s 192.168.1.3-j DROP

View:

[root@test] # iptables-nL-- line-numberChain INPUT (policy ACCEPT) num target prot opt source destination1 DROP all-- 192.168.1.1 0.0.0.0 DROP all 02 DROP all-- 192.168.1.2 0.0.0.0 DROP all-- 192.168.1.3 0.0.0.0 DROP all 04 DROP all-- 192.168.1.4 0.0.0.0 / 05 DROP all-- 192.168.1.5 0.0.0.0 Universe 0

You can see that 192.168.1.3 is inserted into the third row, while the original third line 192.168.1.4 becomes the fourth line.

3. Delete

Delete with-D parameter

Delete the previously added rule (iptables-An INPUT-s 192.168.1.5-j DROP):

[root@test] # iptables-D INPUT-s 192.168.1.5-j DROP

Sometimes the rule to be deleted is too long, and you have to write a long string when deleting, which is both time-consuming and easy to make mistakes. At this time, we can first use-line-number to find out the line number of the rule, and then delete the rule by the line number.

[root@test] # iptables-nv-- line-numberiptables v1.4.7: no command specifiedTry `iptables-h' or 'iptables-- help' for more information. [root@test ~] # iptables-nL-- line-numberChain INPUT (policy ACCEPT) num target prot opt source destination1 DROP all-- 192.168.1.1 0.0.0.002 DROP all-- 192.168.1.2 0.0.0.003 DROP all-- 192.168.1.3 0.0.0.0/0

Delete the second line rule

[root@test ~] # iptables-D INPUT 2

4. Modification

Modify the use-R parameter

Let's take a look at the current rules:

[root@test] # iptables-nL-- line-numberChain INPUT (policy ACCEPT) num target prot opt source destination1 DROP all-- 192.168.1.1 0.0.0.0 DROP all 02 DROP all-- 192.168.1.2 0.0.0.0 DROP all-- 192.168.1.5 0.0.0.0 Universe

Change the third rule to ACCEPT:

[root@test] # iptables-R INPUT 3-j ACCEPT

Check again:

[root@test] # iptables-nL-- line-numberChain INPUT (policy ACCEPT) num target prot opt source destination1 DROP all-- 192.168.1.1 0.0.0.0 DROP all 02 DROP all-- 192.168.1.2 0.0.0.0 ACCEPT all-- 0.0.0.0 ACCEPT all-0.0.0.0.0 Universe 0

The target of the third rule has been changed to ACCEPT.

5. Permanent effect

Service iptables save

Service iptables restart

These are the operations to view, add, delete and modify linux firewall iptables rules introduced in this article.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report