iptables防火墙设置
ubuntu系统:
apt-get install iptables #如果默认没有安装,请运行此命令安装防火墙
# whereis iptables #查看系统是否安装防火墙可以看到:
iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz #表示已经安装iptables
apt-get install iptables #如果默认没有安装,请运行此命令安装防火墙
# iptables -L #查看防火墙配置信息,显示如下:
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
# vi /etc/iptables.rules
添加以下内容(备注:80是指web服务器端口,3306是指MySQL数据库链接端口,22是指SSH远程管理端口.)
*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 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-restore < /etc/iptables.rules #使防火墙规则生效
# vi /etc/network/if-pre-up.d/iptables #创建文件,添加以下内容,使防火墙开机启动
#!/bin/bash
iptables-restore < /etc/iptables.rules
# chmod +x /etc/network/if-pre-up.d/iptables #添加执行权限
# iptables -L -n查看规则是否生效.
eg.
# Generated by iptables-save v1.4.21 on Sat Aug 11 23:44:51 2018 *filter :INPUT DROP [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [3122:409937] :syn-flood - [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT -A INPUT -p tcp -m tcp --dport 20 -j ACCEPT #-A INPUT -s 10.42.1.103 -p tcp -m tcp --dport 22 -j REJECT -A INPUT -s 10.42.1.0/24 -p tcp -m tcp --dport 22 -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 -s 10.42.1.0/24 -p tcp -m tcp --dport 3306 -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/sec --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 # Completed on Sat Aug 11 23:44:51 2018 # Generated by iptables-save v1.4.21 on Sat Aug 11 23:44:51 2018 *nat :PREROUTING ACCEPT [2177:722593] :INPUT ACCEPT [464:157009] :OUTPUT ACCEPT [1877:120700] :POSTROUTING ACCEPT [1877:120700] COMMIT # Completed on Sat Aug 11 23:44:51 2018
Chain INPUT (policy ACCEPT)
target prot opt source destination
target prot opt source destination
target prot opt source destination
[root@tp ~]# iptables -X 清除预设表filter中使用者自定链中的规则
查看防火墙规则列表: # iptables -L -n
设定预设规则:
#iptables -P INPUT DROP
注:如果你是远程SSH登陆的话,当你输入上述命令回车的时候就应该掉了.因为你没有设置任何规则.
怎么办,去本机操作呗!
# iptables -P OUTPUT ACCEPT
# iptables -P FORWARD DROP
上面的意思是,当超出了IPTABLES里filter表里的两个链规则(INPUT,FORWARD)时,不在这两个规则里的数据包怎么处理呢,那就是DROP(放弃).应该说这样配置是很安全的.我们要控制流入数据包
如果做了邮件服务器,开启25,110端口.
如果做了FTP服务器,开启21端口
IPTABLES -A OUTPUT -o lo -p all -j ACCEPT(如果是OUTPUT DROP)
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
target prot opt source destination
SNAT all -- 192.168.0.0/24 anywhere to:211.101.46.235
target prot opt source destination
[root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 172.16.0.0/12 -j DROP
[root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 192.168.0.0/16 -j DROP
如果我们想,比如阻止MSN,QQ,BT等的话,需要找到它们所用的端口或者IP,(个人认为没有太大必要)
[root@tp ~]# iptables -t nat -A PREROUTING -d 211.101.46.253 -j DROP
[root@tp ~]# iptables -A INPUT -m state --state INVALID -j DROP
[root@tp ~]# iptables -A OUTPUT -m state --state INVALID -j DROP
[root@tp ~]# iptables-A FORWARD -m state --state INVALID -j DROP
允许所有已经建立的和相关的连接(必须配置否则httpd无法连接)
[root@tp ~]# iptables-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@tp ~]# iptables-A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@tp ~]# /etc/rc.d/init.d/iptables save
这样就可以写到/etc/sysconfig/iptables文件里了.写入后记得把防火墙重起一下,才能起作用.
[root@tp ~]# service iptables restart
iptables防火墙设置的更多相关文章
- linux下iptables防火墙设置
各位linux的爱好者或者工作跟linux相关的程序员,我们在工作中经常遇到应用服务器端口已经启动, 在网络正常的情况下,访问不到应用程序,这个跟防火墙设置有关 操作步骤 1.检查有没有启动防火墙 s ...
- ubuntu 14.04/14.10 iptables 防火墙设置
1. 一键批处理设置 [plain] view plaincopyprint? #!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/loc ...
- Centos iptables防火墙设置
iptables的基本语法格式 iptables [-t 表名] 命令选项 [链名] [条件匹配] [-j 目标动作或跳转]说明:表名.链名用于指定iptables命令所操作的表和链,命令选项用于指定 ...
- [转载] iptables 防火墙设置
http://hongwei.im/iptables-setting-for-ubuntu-1304/ Iptables是一个防火墙,所有的Ubuntu官方发行版(Ubuntu,Kubuntu,Xub ...
- RedHat Linux下iptables防火墙设置
一般情况下iptables已经包含在Linux发行版中.运行 # iptables --version 来查看系统是否安装iptables 启动iptables:# service iptables ...
- 【Linux】Centos6的iptables防火墙设置
1,查看防火墙状态 # service iptables status //或 # /etc/init.d/iptables status 2,防火墙的启动.重启,关闭 # service iptab ...
- Linux iptables 防火墙设置
1.查看防火墙iptables -L -niptablesb -L -n --line-number 显示规则行号看到 INPUT ACCEPT, FORWARD ACCEPT , OUTPUT A ...
- Centos6 iptables 防火墙设置【转】
1.指令 vi /etc/sysconfig/iptables 添加以下内容和要开放的端口 # Firewall configuration written by system-config-fire ...
- 【服务器防护】centos iptables 防火墙设置 mac过滤
1.阻止MAC地址为XX:XX:XX:XX:XX:XX主机的所有通信: iptables -A INPUT -s 192.168.1.21 -m mac --mac-source XX:XX:XX:X ...
随机推荐
- Bootstrap之信息记录
Bootstrap中文网: http://www.bootcss.com/ 上面有一些资料和范例 实例精选: https://v3.bootcss.com/getting-started/#examp ...
- Java使用RabbitMQ之订阅分发(Topic)
使用RabbitMQ进行消息发布和订阅,生产者将消息发送给转发器(exchange),转发器根据路由键匹配已绑定的消息队列并转发消息,主题模式支持路由键的通配. 生产者代码: package org. ...
- HTTP协议 - 基础认识
在http协议使用场景上我们最熟悉的可能就是浏览器了,作为本系列第一篇,就讲一个问题 ”浏览器怎么连接上服务器并获取网页内容的“ : 首先 浏览器怎么连接上服务器的? 如果对OSI七层模型或者TCP ...
- DatasourceUtils类:获取连接池和数据库连接
本工具类用于获取连接池和数据库连接 package com.itheima.utils; import java.sql.Connection; import java.sql.ResultSet; ...
- Python中数字之间的进制转换
Python中的数据转换 在python中可以通过内置方法进行相应的进制转换,但需记得转化成非十进制时,都会将数字转化成字符串 转化成二进制 a = 10 #声明数字,默认十进制 b = bin(a) ...
- delegate--委托
delegate--委托 (可以把委托看成用来执行方法的一个东西) eg: namespace delegateTest{ delegate double MathsOp(double x); cla ...
- HTML条件注释
前面的话 IE条件注释是微软从IE5开始就提供的一种非标准逻辑语句,作用是可以灵活的为不同IE版本浏览器导入不同html元素.很显然这种方法的最大好处就在于属于微软官方给出的兼容解决办法而且还能通过W ...
- D - Mayor's posters POJ - 2528 离散化+线段树 区间修改单点查询
题意 贴海报 最后可以看到多少海报 思路 :离散化大区间 其中[1,4] [5,6]不能离散化成[1,2] [2,3]因为这样破坏了他们的非相邻关系 每次离散化区间 [x,y]时 把y+1点也加入 ...
- 洛谷P2670扫雷游戏题解
题目 这道题是一个简单的模拟搜索题,可以把每个雷的位置都记作1. 这样就可记录出数字啦 #include<iostream> #include<cstring> using n ...
- Game HDU - 3657(最小割)
Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...