首页 网建技术 关于Ubuntu 18.04 配置防火墙iptables(常见iptables的用法记录)详解

关于Ubuntu 18.04 配置防火墙iptables(常见iptables的用法记录)详解

基于服务器的安全考虑想法,我们需要在Ubuntu 18.04环境中进行配置iptables防火墙,这里小编简单记录下iptables的常规用法。一般我们都是在需要服务器的ROOT权…

基于服务器的安全考虑想法,我们需要在Ubuntu 18.04环境中进行配置iptables防火墙,这里小编简单记录下iptables的常规用法。一般我们都是在需要服务器的ROOT权限下进行的,有些服务器环境默认是安装过的,我们需要检查到底是否有安装,如果有安装过,直接就添加防火墙规则。

第一、检查是否安装iptables

# 检查
# which iptables
/sbin/iptables
# whereis iptables
iptables: /sbin/iptables /etc/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz
第二、安装iptables

如果没有安装的话,我们则需要安装。

# 进行安装
sudo apt-get install iptables
第三、如果安装过我们创建规则
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
然后保存规则
iptables-save > /etc/iptables
第四、创建规则确保重启也执行iptable规则
vi /etc/network/if-pre-up.d/iptablesSelect Code
添加:
iptables-restore < /etc/iptables
保存退出。

第五、查看防火墙规则

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
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
一般情况下,就这么用的。
免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

作者: 小小编

为您推荐

SSH的使用详解

SSH的使用详解

今天小编为大家分享一篇关于SSH 的介绍和使用方法的文章。本文从SSH是什么出发,讲述了SSH的基本用法,之后在远程登录...
宝塔linux面板安装软件错误:宝塔面板检测到系统目录不可写。解决方法

宝塔linux面板安装软件错误:宝塔面板检测到系统目录不可写。解决方法

错误:宝塔面板检测到系统目录不可写。 1、安装了宝塔系统加固,请先关闭; 2、安装了云锁,请关闭系统加固功能; 3、安装...
宝塔linux面板node.js项目服务器重启丢失,pm2列表被删除清空解决方案

宝塔linux面板node.js项目服务器重启丢失,pm2列表被删除清空解决方案

宝塔linux面板node.js项目服务器重启丢失,pm2列表被删除清空解决方案 近期由于服务器异常重启,发现node....
宝塔linux面板 apache网站访问报错503 Service Unavailable解决

宝塔linux面板 apache网站访问报错503 Service Unavailable解决

宝塔linux面板 apache网站访问报错: 503 Service Unavailable Service Unav...
宝塔windows面板apache开启Gzip压缩方法

宝塔windows面板apache开启Gzip压缩方法

感谢网友安好反馈问题,经检查少写一个模块导致开启后apache无法启动 2018/05/18更正 第1步 apache配...

发表回复

返回顶部