腾讯云linux防火墙,腾讯云ubuntu配置iptables黑白名单防火墙规则

腾讯云主机ubuntu默认没有iptables service文件所以要自行配置 如果不配可以按照如下方式配置加载新增防火墙规则

04959f472eca

image.png

mkdir /etc/iptables & vi /etc/iptables/rules.v4

*filter

:INPUT DROP [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

:syn-flood - [0:0]

-A INPUT -i lo -j ACCEPT

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT

# 禁止8080所有IP访问 配置白名单 允许118.249.xx.xx/24 公网访问

-I INPUT -p tcp --dport 8080 -j DROP

-I INPUT -s 118.249.xxx.xx/24 -p tcp --dport 8080 -j ACCEPT

-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT

-A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT

-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN

-A syn-flood -j REJECT --reject-with icmp-port-unreachable

COMMIT

使防火墙生效

vi /etc/network/if-pre-up.d/iptables

#!/bin/bash

iptables-restore < /etc/iptables/rules.v4

chmod +x /etc/network/if-pre-up.d/iptables

iptables -L -n

apt-get install iptables 安装后一般自带iptables.service那么即可用如下方式添加黑白名单

vim /etc/sysconfig/iptables # 或 /etc/rc.d/init.d/iptables目录

#先关闭所有的3306端口

iptables -I INPUT -p tcp --dport 3306 -j DROP

#开启ip段192.168.1.0/24端的3306口

iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 3306 -j ACCEPT

#/etc/rc.d/init.d/ 或 /etc/sysconfig 目录下

service iptables save

service iptables restart

1) 重启后生效

开启: chkconfig iptables on

关闭: chkconfig iptables off

2) 即时生效,重启后失效

开启: service iptables start

关闭: service iptables stop

限制ip访问某个端口 和开放

1、在tcp协议中,禁止所有的ip访问本机的50070端口。

iptables -I INPUT -p tcp --dport 50070 -j DROP

2、允许192.168.1.123访问本机的50070端口

iptables -I INPUT -s 192.168.1.123 -p tcp --dport 50070 -j ACCEPT

3、允许192.168.1.124访问本机的50070端口

iptables -I INPUT -s 192.168.1.124 -p tcp --dport 50070 -j ACCEPT

4、允许192.168.1.100访问本机的50070端口

iptables -I INPUT -s 192.168.1.100 -p tcp --dport 50070 -j ACCEPT

iptables -L -n --line-numbers

#定义白名单变量名

-N whitelist

#设置白名单ip段

-A whitelist -s 116.90.86.196 -j ACCEPT

-A whitelist -s 116.90.86.197 -j ACCEPT

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -p icmp -j whitelist

-A INPUT -i lo -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j whitelist

-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT