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
- [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?
- [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
- [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?
- [Root @ linux ~] # Iptables-I INPUT 3-p tcp-m tcp -- dport 20-j ACCEPT
- [Root @ linux ~] # Iptables-L-n -- line-number
- Chain INPUT (policy DROP)
- Num target prot opt source destination
- 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
- 2 DROP icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 8
- 3 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: 22
- 5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 80
- 6 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED, ESTABLISHED
- 7 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID, NEW
- 8 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt: 21 //-A is inserted to the end by default
- Chain FORWARD (policy ACCEPT)
- Num target prot opt source destination
- Chain OUTPUT (policy ACCEPT)
- 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?
- [Root @ linux ~] # Iptables-L-n -- line-number | grep 21 // -- line-number indicates the sequence number of the rule, which is convenient for deletion.
- 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
- [Root @ linux ~] # Iptables-t nat-vnL POSTROUTING -- line-number
- Chain POSTROUTING (policy ACCEPT 38 packets, 2297 bytes)
- Num pkts bytes target prot opt in out source destination
- 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?
- [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?
- [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
[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?
- [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.