Suppression des règles iptables simplement

Voici comment faire pour supprimer les règles iptables par numéro de règle dans une chaine.

Affichez d'abord les règles iptables avec le numéro de ligne :

delovan@betty:~$ iptables -t nat -vnL --line-numbers
Chain PREROUTING (policy ACCEPT 457K packets, 68M bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 S20-WEB    tcp  --  *      *       0.0.0.0/0            10.161.96.71        tcp dpt:80
2        0     0 S20-WEB    tcp  --  *      *       0.0.0.0/0            10.161.32.71        tcp dpt:80

Chain POSTROUTING (policy ACCEPT 2403 packets, 183K bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x1f4 to:10.161.32.71
2        0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x1f5 to:10.161.33.71
3        0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x1f6 to:10.161.34.73
4        0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x1f7 to:10.161.33.73

Chain OUTPUT (policy ACCEPT 2403 packets, 183K bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 S20-WEB    tcp  --  *      *       0.0.0.0/0            10.161.96.71        tcp dpt:80
2        0     0 S20-WEB    tcp  --  *      *       0.0.0.0/0            10.161.32.71        tcp dpt:80

Chain S20-WEB (4 references)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           to:10.161.96.71:60080
2        0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0  

Pour supprimer une règle, tappez simplement :

delovan@betty:~$ iptables -t nat -D S20-WEB 1

Résultat :

delovan@betty:~$ iptables -t nat -vnL --line-numbers
Chain PREROUTING (policy ACCEPT 457K packets, 68M bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 S20-WEB    tcp  --  *      *       0.0.0.0/0            10.161.96.71        tcp dpt:80
2        0     0 S20-WEB    tcp  --  *      *       0.0.0.0/0            10.161.32.71        tcp dpt:80

Chain POSTROUTING (policy ACCEPT 2403 packets, 183K bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x1f4 to:10.161.32.71
2        0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x1f5 to:10.161.33.71
3        0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x1f6 to:10.161.34.73
4        0     0 SNAT       all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x1f7 to:10.161.33.73

Chain OUTPUT (policy ACCEPT 2403 packets, 183K bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 S20-WEB    tcp  --  *      *       0.0.0.0/0            10.161.96.71        tcp dpt:80
2        0     0 S20-WEB    tcp  --  *      *       0.0.0.0/0            10.161.32.71        tcp dpt:80

Chain S20-WEB (4 references)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0 

Vous remarquerez que les règles ont automatiquement été renumérotées.

Damien Gustave

Read more posts by this author.