1 Ubuntu 18.04 搭建iptables防火墙

1、检查是否有安装iptables, 我是在root账号下执行的,如果非root有些请加上sudo

# 检查
root@cocosum:~# which iptables
/sbin/iptables
root@cocosum:~# whereis iptables
iptables: /sbin/iptables /etc/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz
# 说明有安装iptables

在这里插入图片描述
2、如果没有安装iptables则需要安装

# 进行安装
sudo apt-get install iptables

3、如果安装了需要配置防火墙规则,我这里自己创建一个防火墙规则

# 创建防火墙规则的文件
root@cocosum:~# vi /etc/iptables
# 添加下面的规则
 
*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 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-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 7070 -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

4、保存规则

# iptables-save > /etc/iptables

5、创建一个脚本, 为了每次重启系统, iptables防火墙自动启动

# 直接创建,目的是为了系统每次重启自动加载
# vi /etc/network/if-pre-up.d/iptables
 
# 内容
#!/bin/bash
iptables-restore < /etc/iptables
 
# :wq保存

6、给添加的脚本有执行的权限

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

7、查看iptables;直接iptables -L ,和centos 差不多

# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:mysql
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:http-alt
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:7070
ACCEPT     icmp --  anywhere             anywhere             limit: avg 100/sec burst 100
ACCEPT     icmp --  anywhere             anywhere             limit: avg 1/sec burst 10
syn-flood  tcp  --  anywhere             anywhere             tcp flags:FIN,SYN,RST,ACK/SYN
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
 
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
 
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
 
Chain syn-flood (1 references)
target     prot opt source               destination         
RETURN     tcp  --  anywhere             anywhere             limit: avg 3/sec burst 6

查看iptables规则

# iptables -vnL --line-numbers

-v 是输出详细信息

-n 指的是显示地址和端口号

-L 指显示链里面的规则

–line-number参数用来显示行号,删除的时候很有用
在这里插入图片描述
删除所有规则

iptables -F

注意,如果在INPUT DROP策略启用的同时,采用了-F删除规则,会导致22端口的规则被删除,因此被阻在服务器外面了。所以要特别小心,-F本身无害,但是和DROP策略在一起的时候,就是危险的。

在这里插入图片描述

执行iptables -F,删除了22端口的规则,导致断开连接,重启恢复

删除指定规则

iptables -D INPUT 4

添加一个规则,允许已经建立的连接接收进来的数据

iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

只接受指定的服务器访问
如果想限制有限的机器向服务器发起请求,可以用-s参数,比如:

iptables -A INPUT -p tcp -s 10.112.18.0/24 --dport 27017 -j ACCEPT

仅在10.112.18.0/0网段的机器才能连接上本机的27017端口。

如果去掉/24,代表指定的某个IP才能访问。

如果仅仅想本机访问,用下面的命令:

iptables -A INPUT -p tcp -s 127.0.0.1 --dport 27017 -j ACCEPT

也可以通过这个命令,将本地访问完全放开:

iptables -A INPUT -i lo -j ACCEPT

参考链接:

https://blog.csdn.net/csfreebird/article/details/8132362

https://help.ubuntu.com/community/IptablesHowTo

https://blog.csdn.net/qq_40058321/article/details/103180923

猜你喜欢

转载自blog.csdn.net/qq_40907977/article/details/114977760