我们 来讨论一下如何为你的CentOS 服务器来设置简单的防火墙。 这里我们以DigitalOcean的CentOS 6 VPS为基础来讨论的,同样也适用于 阿里云上其他类型的LINUX系统。 (阿里云有个云盾系统,因此在你自己的VPS上不设置防火墙也是可以的)

需要说明的是: 本文只涉及最基础最常用的防火墙设置,能屏蔽一些常用的攻击,但并不能彻底保证你的服务器的安全。

系统的随时更新 以及 关闭不必要的服务 仍然是保证系统安全非常重要的步骤。

如果你需要更全面的了解iptables,阅读本文后,请google或是阅读更加深入的资料!

首先简单介绍一下什么是IPTables:

iptables是Linux内核中内置的防火墙,可以允许管理员通过设置table, chain以及相关的规则来进行数据包过滤和NAT。 一般来讲,iptables防火墙已经内置于CentOS 6及其他Linux版本中,而且iptables服务默认都是启动的。  iptables应用于IPv4, 如果要用IPv6,需要使用ip6tables.

iptables的命令格式:

1
 iptables [-t table] command [chain] [rules] [-j target] 

[-t table] :用来指明使用的表, 有三种选项: filter, nat 和 mangle,如果未指定,则使用filter作为缺省表。 事实上,对于单个服务器的防火墙配置,一般来讲,我们只需要对filter表进行配件就OK了。filter表包括 INPUT, OUTPUT,和FORWARD三个chain.

command 表明iptables命名要做什么,比如

-A (–append): 该命令会把一条规则附件到chain的末尾。

-D(–delete)用来删除某个规则。

-F (–flush) 如果指定了chain, 删除该chain中的所有规则,如果未指定chain, 则删除所有chain中的所有规则。

target: 是由规则指定的操作。 包括下面几种:

ACCEPT: 接收信息包(允许它前往目的地),并且将停止遍历chain.

DROP:  拒绝,

此外还有REJECT, RETURN, LOG, REDIRECT, MARK, MIRROR, MAQUERADE等。

具体的iptables的语法和概念就不再多说了,请参照iptables man page 官方文档 .

简单来说,iptables防火墙是由一系列的规则(rule)组成,  一个数据请求进来, 会依次和这些规则进行比较,如果正好符合规则的定义,那这个数据请求要么会被接收ACCEPT,要么被拒绝DRIP。如果不符合任何规则的定义,最后缺省的规则会被应用。

开始操作之前:

注意:一定要把你在DigitalOcean/ Linode/ 阿里云上的服务器做一下快照备份 , 否则一旦你 iptables的配置出了问题,极有可能把你自己挡在门外,你自己都无法连接到服务器了!! 出现这种情况可是会欲哭无泪呀,除了重新做系统好像没有更好的办法了。( DigitalOcean提供了一个web console的界面,有时候会给你反悔和擦除iptables设置的机会,但阿里云没有)

 决定哪些端口需要开放

首先, SSH 的端口22自然是需要开放的,否则我们就无法登录服务器了。

一般来讲,CentOS的VPS经常作为用LAMP搭建的Web服务器,FTP服务器, Mail服务器等。

对于Web服务来说,需要开放80端口,如果是HTTPS/SSL协议的话,还需用开放443端口

对于Mail服务来说,由于涉及SMTP, POP3, IMAP协议,需要开放的端口如下:

SMTP : 25   Secure SMTP:465  POP3: 110   Secure POP3: 995   IMAP: 143   IMAP over SSL: 993

对于FTP服务来说,需要开放 20, 21两个端口

第一步: 屏蔽最常见的攻击

缺省情况下,CentOS的iptables的设置是允许任何数据通过的。

我们首先要清空iptables中的所有的规则:

1
iptables -F

然后我们加上阻止简单扫描和攻击的规则

1
2
3
4
5
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP             #NONE 包(所有标识bit都没有设置)主要是扫描类的数据包
 
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP     #防止sync-flood 攻击
 
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP              #ALL包(所有的标注bit都被设置了)也是网络扫描的数据包

关于sync-flood, 请参照wikipedia 的解释。

第二步: 为相应的服务开放对应的端口

首先我们应该接受本机localhost的任何请求,否则,数据库连接等将无法工作

1
iptables -A INPUT -i lo -j ACCEPT

对于不同的服务需要开放不同的端口

1
2
3
4
5
6
7
8
9
iptables -A INPUT -p tcp --dport 22 -j ACCEPT      # SSH
iptables -A INPUT -p tcp --dport 80 -j ACCEPT      # HTTP
iptables -A INPUT -p tcp --dport 443 -j ACCEPT     #HTTPS
iptables -A INPUT -p tcp --dport 25 -j ACCEPT   #SMTP
iptables -A INPUT -p tcp --dport 465  -j ACCEPT #Secure SMTP
iptables -A INPUT -p tcp --dport 110 -j ACCEPT   #POP3
iptables -A INPUT -p tcp --dport 995 -j ACCEPT   #Secure POP3
iptables -A INPUT -p tcp --dport 143 -j ACCEPT   #IMAP
iptables -A INPUT -p tcp --dport 993 -j ACCEPT   #Secure IMAP

第三步: 加上通用的规则

首先要允许所有从服务器端发起的连接,由此返回的响应数据应该是允许的!比如VPS发起的yum update , 必须要允许外部的update数据进来

1
iptables -I INPUT -m state  --state ESTABLISHED, RELATED -j ACCEPT

最后,设置缺省的策略:屏蔽任何进入的数据请求,允许所有从Server发出的请求

1
2
3
iptables -P OUTPUT ACCEPT
 
iptables -P INPUT DROP

至此,规则设置完毕

第四步: 保存设置

首先通过下面的命令查看一下我们的设置是否正确!

1
iptable -L -n

确认没有问题后,执行下面的命令

1
service iptables save

执行上述命令后,相应的规则会写入 /etc/sysconfig/iptables这个文件,你可以检查一下看看。

最后执行

1
service iptables restart

重新启动iptables防火墙,以使上述设置生效。

最佳的方法

为了更方便的修改和维护自己的iptables的设置,我一般是把所有的iptables的设置先写到一个单独文件中,测试没有问题后。然后再保存到iptable的配置文件中。

下面是我自己的iptables文件    ~/script/firewall.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre>
#!/bin/bash
# A simple iptables firewall configuration
 
PATH=/sbin:/bin:/usr/sbin:/usr/bin; export PATH
 
#flush/erase original rules
iptables -F #清除所有已制定的rule
iptables -X #清除用户自定义的chain/table
iptables -Z #将所有的chain的计数和流量统计归零
 
#Accept localhost connetting, no matter what it is
iptables -A INPUT -i lo -j ACCEPT
 
#Accept any response package which is initiated from inside
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
#block most common network attacks(recon packets and syn-flood attack)
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
 
#open ports for different services
iptables -A INPUT -p tcp --dport 22 -j ACCEPT #SSH
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #HTTP
#iptables -A INPUT -p tcp --dport 443 -j ACCEPT #HTTPS
#iptables -A INPUT -p tcp --dport 25 -j ACCEPT #SMTP
#iptables -A INPUT -p tcp --dport 465 -j ACCEPT #Secure SMTP
#iptables -A INPUT -p tcp --dport 110 -j ACCEPT #POP3
#iptables -A INPUT -p tcp --dport 995 -j ACCEPT #Secure POP
 
#ICMP configuration
#To prevent ICMP DDOS,we do not allow ICMP type 8(echo-request) or limit this request with 1/second
#some ICMP requests are allowed.
icmp_type="0 3 4 11 12 14 16 18"
for ticmp in $icmp_type
do
    iptables -A INPUT -p icmp --icmp-type $ticmp -j ACCEPT
done
#iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit 1/second -j ACCEPT
 
#default policies
iptables -P OUTPUT ACCEPT
iptables -P INPUT DROP
 
#save to /etc/sysconfig/iptables
/etc/init.d/iptables save

你可以根据你的需要进行相应的修改。

为Linux设置IPTables防火墙的更多相关文章

  1. linux设置iptables防火墙的详细步骤(centos防火墙设置方法)

    CentOS系统也是基于linux中的它的防火墙其实就是iptables了,下面我来介绍在CentOS防火墙iptables的配置教程,希望此教程对各位朋友会有所帮助.   iptables是与Lin ...

  2. LINUX中IPTABLES防火墙使用

    对于有公网IP的生产环境VPS,仅仅开放需要的端口,即采用ACL来控制IP和端口(Access Control List). 这里可以使用Linux防火墙netfilter的用户态工具 iptable ...

  3. 9.Linux之iptables防火墙

    Linux之iptables防火墙 目录 Linux之iptables防火墙 iptables防火墙概述 netfilter和iptables之间的关系 netfilter iptables ipta ...

  4. linux 的iptables防火墙

    .a文件就是*.o文件的集合, 是object文件的归档文件, 所以, 用nm -A  ???.a看到的 symbolic符合名称都是 相应的,  包含的  .o文件.... linux 2.4内核中 ...

  5. (转载)Linux下IPTABLES防火墙的设定

    (转载)http://www.jefflei.com/post/1760.html 1.iptables防火墙启动和停止 启动iptables防火墙时命令行输入 #service iptables s ...

  6. Linux上iptables防火墙的基本应用教程

    iptables是Linux上常用的防火墙软件,下面vps侦探给大家说一下iptables的安装.清除iptables规则.iptables只开放指定端口.iptables屏蔽指定ip.ip段及解封. ...

  7. (转载)Linux上iptables防火墙的基本应用教程

    (转载)http://www.vpser.net/security/linux-iptables.html iptables是Linux上常用的防火墙软件,下面vps侦探给大家说一下iptables的 ...

  8. CentOS 7设置iptables防火墙开放proftpd端口

    由于ftp的被动模式是这样的,客户端跟服务器端的21号端口交互信令,服务器端开启21号端口能够使客户端登录以及查看目录.但是ftp被动模式用于传输数据的端口却不是21,而是大于1024的随机或配置文件 ...

  9. Linux 安装 iptables防火墙

    CentOS7默认的防火墙不是iptables,而是firewalle. 安装iptable iptable-service #先检查是否安装了iptables service iptables st ...

随机推荐

  1. C#(winform)设置窗体的启动位置

    只需要设置窗体的StartPosition属性: registerForm.StartPosition = FormStartPosition.CenterScreen; FormStartPosit ...

  2. [PY3]——字符串的分割、匹配、搜索方法总结

    ?分割.匹配.搜索时可以用到什么样的解决方法? 分割方法总结 1. str.split( ) * 分割字符串 * 返回列表 s1='I love python' # 默认以空格为界定符,且多个空格都当 ...

  3. JAVA练手--文件操作

    1. File类 主要作用:用于文件和文件夹的创建.查找.删除等操作 public static void main(String[] args) throws IOException { File ...

  4. [转]emailjs-smtp-client

    本文转自:https://github.com/emailjs/emailjs-smtp-client/blob/master/README.md SMTP Client SMTP Client al ...

  5. WINFORM如何实现无聚焦框的Button按钮

    当我们将一个button按钮设置如下属性时,总有一个聚焦框来困扰着我们 button1.FlatStyle = FlatStyle.Flat; 我们想要的效果是这样的: 但当使用了Tab切换焦点时 发 ...

  6. spring mongodb分页,动态条件、字段查询

    使用MongRepository public interface VideoRepository extends MongoRepository<Video, String> { Vid ...

  7. 使用重绘项美化WinForm中的控件

    如果你觉得项目中的ComboBox.ListBox或其它的Winforms控件不能满足你的显示要求,包括窗体在内很多控件都支持重绘修改显示样式.下面的示例完成对ComBox数据项的重绘,希望能起到抛砖 ...

  8. Hadoop源码学习笔记(2) ——进入main函数打印包信息

    Hadoop源码学习笔记(2) ——进入main函数打印包信息 找到了main函数,也建立了快速启动的方法,然后我们就进去看一看. 进入NameNode和DataNode的主函数后,发现形式差不多: ...

  9. Spring 中的Enum HttpStatus 及HTTP状态码

    官方API https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/HttpStatus.htm ...

  10. Java线程入门第二篇

    Java线程通信方法 0.(why)每个线程都有自己的栈空间,我们要线程之间进行交流,合作共赢. 1.synchronized和volatile关键字 a)  看下面的synchronized关键字 ...