以下三个规则:
①对所有的地址开放本机的tcp(80、22、10~21)访问端口。
②运行对所有地址开放本机的基于ICMP访问协议的数据包。
③禁止访问其他不允许的端口。
#查看本机开放的端口 netstat -luntp
[root@bogon ~]# netstat -luntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 9215/X tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 9106/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 8591/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 8587/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 9024/master tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 20030/sshd: root@pt tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 20313/sshd: root@pt tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::6000 :::* LISTEN 9215/X tcp6 0 0 :::22 :::* LISTEN 8591/sshd tcp6 0 0 ::1:631 :::* LISTEN 8587/cupsd tcp6 0 0 ::1:25 :::* LISTEN 9024/master tcp6 0 0 ::1:6010 :::* LISTEN 20030/sshd: root@pt tcp6 0 0 ::1:6011 :::* LISTEN 20313/sshd: root@pt udp 0 0 0.0.0.0:50213 0.0.0.0:* 8206/avahi-daemon: udp 0 0 0.0.0.0:5353 0.0.0.0:* 8206/avahi-daemon: udp 0 0 192.168.122.1:53 0.0.0.0:* 9106/dnsmasq udp 0 0 0.0.0.0:67 0.0.0.0:* 9106/dnsmasq udp 0 0 0.0.0.0:68 0.0.0.0:* 20242/dhclient udp 0 0 0.0.0.0:111 0.0.0.0:* 1/systemd udp 0 0 127.0.0.1:323 0.0.0.0:* 8237/chronyd udp 0 0 0.0.0.0:693 0.0.0.0:* 8158/rpcbind udp6 0 0 :::111 :::* 1/systemd udp6 0 0 ::1:323 :::* 8237/chronyd udp6 0 0 :::693 :::* 8158/rpcbind [root@bogon ~]#
查看iptables版本
iptables -v
[root@bogon ~]# iptables -v iptables v1.4.21: no command specified Try `iptables -h' or 'iptables --help' for more information. [root@bogon ~]#
查看iptables配置的链,-n让主机名等不显示
iptable -nL
[root@bogon ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination [root@bogon ~]#
设置80端口可以访问,设置22端口可以访问,不然ssh不能登录,设置10~允许访问21。
iptables -I INPUT -p tcp --dport 80 -j ACCEPT iptables -I INPUT -p tcp --dport 22 -j ACCEPT iptables -I INPUT -p tcp --dport 10:21 -j ACCEPT
此时配置策略有:
[root@bogon ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:10:21 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
再支持ICMP协议,然后-A,最后,添加一条拒绝所有规则。
iptables -I INPUT -p icmp -j ACCEPT iptables -A INPUT -j REJECT
这里实现了以上三个规则,相应的策略是这样的。