2.1 Linux 防火墙基础
[root@oo ~]# systemctl stop firewalld.service
[root@oo ~]# systemctl disable firewalld.service
[root@oo ~]# yum -y install iptables iptables-service
[root@oo ~]# systemctl start iptables.service
[root@oo ~]# systemctl enable iptables.service
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@oo ~]#
[root@oo ~]# iptables -t filter -I INPUT -p icmp -j REJECT
[root@oo ~]# iptables -t filter -A INPUT -p tcp -j ACCEPT
[root@oo ~]# iptables -I INPUT -p udp -j ACCEPT
[root@oo ~]# iptables -I INPUT 2 -p icmp -j ACCEPT
[root@oo ~]#
[root@oo ~]# iptables -L INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT udp -- anywhere anywhere
2 ACCEPT icmp -- anywhere anywhere
3 REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
4 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
5 ACCEPT icmp -- anywhere anywhere
6 ACCEPT all -- anywhere anywhere
7 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
8 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
9 ACCEPT tcp -- anywhere anywhere
[root@oo ~]#
[root@oo ~]# iptables -n -L INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
REJECT icmp -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0
[root@oo ~]#
[root@oo ~]#
[root@oo ~]# iptables -D INPUT 3
[root@oo ~]# iptables -n -L INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0
[root@oo ~]#
[root@oo ~]# iptables -F INPUT
[root@oo ~]# iptables -n -L INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
[root@oo ~]#
[root@oo ~]# iptables -t filter -P FORWARD DROP
[root@oo ~]# iptables -P OUTPUT ACCEPT
[root@oo ~]#
[root@oo ~]# iptables -I INPUT -p icmp -j DROP
[root@oo ~]# iptables -A FORWARD ! -p icmp -j ACCEPT //感叹号”!”表示取反
[root@oo ~]#
[root@oo ~]# iptables -A FORWARD -s 192.168.1.11 -j REJECT
[root@oo ~]# iptables -A FORWARD -s 192.168.7.0/24 -j REJECT
[root@oo ~]#
[root@oo ~]# iptables -I INPUT -s 10.20.30.0/24 -j DROP
[root@oo ~]# iptables -I FORWARD -s 10.20.30.0/24 -j DROP
[root@oo ~]# iptables -A INPUT -i ens33 -s 10.0.0.0/8 -j DROP
[root@oo ~]# iptables -A INPUT -i ens33 -s 172.16.0.0/16 -j DROP
[root@oo ~]# iptables -A INPUT -i ens33 -s 192.168.0.0/16 -j DROP
[root@oo ~]# iptables -A FORWARD -s 192.168.4.0/24 -p udp --dport 53 -j ACCEPT
[root@oo ~]# iptables -A FORWARD -s 192.168.4.0/24 -p udp --sport 53 -j ACCEPT
[root@oo ~]#
[root@oo ~]# iptables -A INPUT -p tcp --dport 20:21 -j ACCEPT
[root@oo ~]# iptables -A INPUT -p tcp --dport 24500:24600 -j ACCEPT
[root@oo ~]# iptables -A INPUT -p icmp --icmp-type 8 -j DROP
[root@oo ~]# iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
[root@oo ~]# iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT
[root@oo ~]# iptables -A INPUT -p icmp -j DROP
[root@oo ~]#
[root@oo ~]# iptables -p icmp -h
iptables v1.4.21
Usage: iptables -[ACD] chain rule-specification [options]
iptables -I chain [rulenum] rule-specification [options]
iptables -R chain rulenum rule-specification [options]
iptables -D chain rulenum [options]
iptables -[LS] [chain [rulenum]] [options]
iptables -[FZ] [chain] [options]
iptables -[NX] chain
iptables -E old-chain-name new-chain-name
iptables -P chain target [options]
iptables -h (print this help information)
Commands:
Either long or short options are allowed.
[root@oo ~]# iptables -A INPUT -p tcp -m multiport --dport 25,80,110,143 -j ACCEPT
[root@oo ~]# iptables -A FORWARD -p tcp -m iprange --src-range 192.168.4.21-192.168.4.28 -j DROP
[root@oo ~]#
[root@oo ~]# iptables -A INPUT -m mac --mac-source 00:0c:29:7e:ab:51 -j DROP
[root@oo ~]# iptables -A FORWARD -m state --state NEW -p tcp ! --syn -j DROP
[root@oo ~]#
[root@oo ~]# iptables -I INPUT -p tcp -m multiport --dport 80 -j ACCEPT
[root@oo ~]# iptables -I INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT
[root@oo ~]# iptables -P INPUT DROP
最后看一下我们配置的INPUT表
[root@oo ~]# iptables -n -L INPUT
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports 80
DROP all -- 10.20.30.0/24 0.0.0.0/0
DROP icmp -- 0.0.0.0/0 0.0.0.0/0
DROP all -- 10.0.0.0/8 0.0.0.0/0
DROP all -- 172.16.0.0/16 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:20:21
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:24500:24600
DROP icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 8
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 3
DROP icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports 25,80,110,143