服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|

服务器之家 - 服务器系统 - Centos - CentOS服务器iptables配置简单教程

CentOS服务器iptables配置简单教程

2021-11-19 17:15王忠惠 Centos

这篇文章主要为大家详细介绍了CentOS服务器iptables配置简单教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

iptables是Linux类服务器重要的网络安全防范系统工具,考虑到多数服务器有专门的团队托管,服务器管理员多数时间只能通过SSH进行远程管理,在安全允许的情况下,保证SSH的合法联通,需要做如下的配置。

?
1
2
3
4
5
6
7
8
9
iptables -P INPUT ACCEPT
iptables -F
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -L -v

  这样能够保证SSH的22端口得到合法的通行,最后执行service iptables save,将刚才的配置保存。

  通过cat /etc/sysconfig/iptables可以查看iptables配置文件的信息,今后可以通过直接编辑该文件,增删配置条目。

  查看运行着的iptables的规则指令为:lsmod | grep ip_tables或iptables -L。

小编再补充一个知识点:防简单攻击iptables策略

?
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh
IPTABLES=/sbin/iptables
 
 
 
# clear
$IPTABLES -F
 
# if pkg type is allow, then accept
#$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
 
 
# 如果同时在80端口的连接数大于10,就Drop掉这个ip
 
netstat -an | grep :80 | awk -F: '{ print $8 }' | sort | uniq -c | awk -F\  '$1>10 && $2!="" { print $2 }' >> /etc/fw.list
less /etc/fw.list | sort | uniq -c | awk -F\  '$2!="" { print $2 }' > /etc/fw.list2
less /etc/fw.list2 > /etc/fw.list
while read line
 
    do
    t=`echo "$line"`
    $IPTABLES -A INPUT -p tcp -s $t -j DROP
done < /etc/fw.list2
 
# IP转发
$IPTABLES -A INPUT -p tcp --dport 20002 -j ACCEPT
$IPTABLES -A INPUT -d 172.16.204.7 -p tcp -m tcp --dport 20002 -i eth0 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -d 211.100.39.44 -p tcp -m tcp --dport 20002 -j DNAT --to-destination 172.16.204.7:20002
$IPTABLES -t nat -A POSTROUTING -d 172.16.204.7 -p tcp -m tcp --dport 20002 -j SNAT --to-source 10.6.39.44
 
 
# if pkg visit 80,7710 port then accept
$IPTABLES -A INPUT -p tcp --dport 80 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 8080 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 873 -j ACCEPT
# $IPTABLES -A INPUT -i eth0 -m limit --limit 1/sec --limit-burst 5 -j ACCEPT
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit 30/m --limit-burst 2 -j ACCEPT
$IPTABLES -A FORWARD -p tcp --syn -m limit --limit 10/s -j ACCEPT
$IPTABLES -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT
 
 
# if pkg from allow ip then accept
 
$IPTABLES -A INPUT -p tcp -s 127.0.0.1  -j ACCEPT
 
 
 
# if pkg not above then deny
 
$IPTABLES -A INPUT -p tcp --syn -j DROP
 
下面这个防火墙测试结果更正确,能起到一定的防攻击的功能
 
 
 
#!/bin/sh
 
IPTABLES="/sbin/iptables"
 
echo "1" > /proc/sys/net/ipv4/ip_forward
 
$IPTABLES -P INPUT DROP
$IPTABLES -P FORWARD DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -F
$IPTABLES -X
 
 
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 80 --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit 30/m --limit-burst 2 -j ACCEPT
 
 
$IPTABLES -A OUTPUT -p tcp -s 127.0.0.1 -j ACCEPT
$IPTABLES -A OUTPUT -p tcp -s 192.168.1.102 -j ACCEPT
$IPTABLES -A OUTPUT -p udp -s 127.0.0.1 -j ACCEPT
$IPTABLES -A OUTPUT -p udp -s 192.168.1.102 -j ACCEPT
 
 
 
$IPTABLES -A INPUT -p tcp --syn -j DROP

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐
  • Centoscentos下如何安装卸载命令rpm make install?

    centos下如何安装卸载命令rpm make install?

    一些朋友不知道centos下如何安装卸载命令rpm make install?今天小编为大家带来的是centos下安装卸载命令rpm,make install使用方法;有需要的朋友一起去看看吧...

    centos之家2982019-06-20
  • CentosCentOS 6.6实现永久修改DNS地址的方法

    CentOS 6.6实现永久修改DNS地址的方法

    这篇文章主要介绍了CentOS 6.6实现永久修改DNS地址的方法,涉及针对CentOS配置文件的相关设置技巧,具有一定参考借鉴价值,需要的朋友可以参考下 ...

    Linux社区3642020-08-21
  • CentosCentOS有哪些常见的处理目录的命令?

    CentOS有哪些常见的处理目录的命令?

    一些朋友再问CentOS有哪些常见的处理目录的命令?今天小编将为大家分享CentOS几个常见的处理目录的命令详解,有需要的朋友一起去看看吧...

    未知5192019-06-12
  • CentosCentOS下Uptime命令详解

    CentOS下Uptime命令详解

    在Linux下,我们可以使用uptime命令,而且此命令不必使用root权限。uptime命令在系统中已经默认安装了。今天小编为大家带来的是CentOS下Uptime命令详解;希望...

    CentOS之家7692019-06-19
  • CentosCentOS下DNS的基本和高级配置详解

    CentOS下DNS的基本和高级配置详解

    今天小编将为大家带来的是CentOS下DNS的基本和高级配置详解。希望能够帮助到大家,有需要的朋友一起去看看吧...

    CentOS之家4372019-06-12
  • CentosCentos的Inode和Block的相关知识

    Centos的Inode和Block的相关知识

    今天小编为大家带来了Centos的Inode和Block的一些相关知识;有需要的朋友可以过来看看...

    服务器之家4182019-07-07
  • CentosCentOS性能诊断工具命令集详解

    CentOS性能诊断工具命令集详解

    今天小编为大家带来得是CentOS性能诊断工具命令集详解;希望对大家会有帮助,有需要的朋友一起去看看吧...

    CentOS之家5512019-06-14
  • CentosCentOS SSH无密码登录的配置

    CentOS SSH无密码登录的配置

    本篇文章主要介绍了CentOS SSH无密码登录的配置,避免了繁琐的密码验证,有需要的朋友可以了解一下。...

    aaron4285152021-11-17