ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

ubuntu中防火墙iptables配置

2021-04-28 21:02:18  阅读:241  来源: 互联网

标签:iptables -- 防火墙 ACCEPT anywhere tcp ubuntu INPUT


借鑑:https://www.cnblogs.com/xwgcxk/p/10820518.html

1.查看系统是否安装防火墙

root@localhost:/usr# which iptables
/sbin/iptables
root@localhost:/usr# whereis iptables
iptables: /sbin/iptables /etc/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz

如果是这样的信息,那么表明iptables就是安装了的。
如果没有安装,那么使用sudo apt-get install iptables 安装。

2.查看防火墙的配置信息

配置好了的,是这个样子。

root@localhost:/usr# sudo 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: 22
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:http
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
  1. 新建规则文件
mkdir /etc/iptables #先新建目录,本身无此目录
vim /etc/iptables/rules.v4

/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 3306 -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-restore < /etc/iptables/rules.v4

5.创建文件,添加以下内容,使防火墙开机启动

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

#!/bin/bash
iptables-restore < /etc/iptables/rules.v4

6.添加执行权限

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

7.查看规则是否生效

iptables -L -n

Ubuntu中没有直接停止关闭iptables的命令,像service iptables stop这类命令,是centos才有的。关闭的话,可以暂时开放所有端口作为替代方案

iptables -P INPUT ACCEPT  
iptables -P OUTPUT ACCEPT

标签:iptables,--,防火墙,ACCEPT,anywhere,tcp,ubuntu,INPUT
来源: https://blog.csdn.net/sinat_33384251/article/details/116243087

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有