CentOS网络访问防火墙与端口修改

 

 

参考 /etc/sysconfig/iptables

复制代码
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8084 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8078 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8090 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8091 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8092 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8093 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 10000 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5672 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 15672 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 1099 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9998 -j ACCEPT






-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
复制代码

 

 

補充《20个Linux防火墙应用技巧》,參考:http://www.51testing.com/html/46/n-806546-4.html

 

 

本文带领大家探讨了Linux系统管理员应该掌握的20个防火墙应用技巧。

广为人知的iptables命令行
  Netfilter作为Linux内置的主机防火墙,它可以使用iptables命令处理IPv4协议,也可以使用ip6tables命令处理IPv6协议。在iptables之前,Linux 2.2中使用ipchains来配置防火墙,Linux 2.0中则使用ipfwadm,它基于BSD的ipfw命令实现。
  以下命令在RHEL 6.x上执行通过,但也适用于其他Linux发行版。
  1、显示防火墙的状态
  以root权限运行下面的命令:
# iptables -L -n -v
  参数说明:
  ● -L:列出规则。
  ● -v:显示详细信息。此选项会显示接口名称、规则选项和TOS掩码,以及封包和字节计数。
  ● -n:以数字形式显示IP地址和端口,不使用DNS解析。
  如果希望输出的结果中显示行号,可以运行:
# iptables -L -n -v --line-nmubers
  这样,就可以按照行号在防火墙中添加、删除规则。
  要显示输入或输出链规则,可以运行:
# iptables -L INPUT -n -v
# iptables -L OUTPUT -n -v --line-numbers
  2、停止、开启和重启防火墙
  如果你使用的是RHEL/Fedora/CentOS系统,可以运行:
# service iptables stop
# service iptables start
# service iptables restart
  我们也可以使用iptables命令停止防火墙并删除所有规则:
# iptables -F
# iptables -X
# iptables -t nat -F
# iptables -t nat -X
# iptables -t mangle -F
# iptables -t mangle -X
# iptables -P INPUT ACCEPT
# iptables -P OUTPUT ACCEPT
# iptables -P FORWARD ACCEPT
  参数说明:
  ● -F:删除所有的规则
  ● -X:删除链
  ● -t table_name:匹配表(称为nat或mangle)
  ● -P:设置默认策略(如DROP、REJECT或ACCEPT)
  3、删除防火墙规则
  以带行号的形式显示已有的防火墙规则,请运行:
# iptables -L INPUT -n --line-numbers
# iptables -L OUTPUT -n --line-numbers
# iptables -L OUTPUT -n --line-numbers | less
# iptables -L OUTPUT -n --line-numbers | grep 202.54.1.1
  下面我们使用行号删除规则:
# iptables -D INPUT 4
  将IP地址202.54.1.1从规则中删除:
# iptables -D INPUT -s 202.54.1.1 -j DROP
  参数说明:
  ● -D:从选择的链中删除一条或多条规则
4、插入防火墙规则
  首先运行下面的命令:
# iptables -L INPUT -n --line-numbers
  得到运行结果:
Chain INPUT (policy DROP)
num target prot opt source destination
1 DROP all -- 202.54.1.1 0.0.0.0/0
2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
  在行1和行2之间插入规则:
# iptables -I INPUT 2 -s 202.54.1.2 -j DROP
  查看更新后的规则,会发现插入成功,下面是示例:
Chain INPUT (policy DROP)
Num target prot opt source destination
1 DROP all -- 202.54.1.1 0.0.0.0/0
2 DROP all -- 202.54.1.2 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
  5、保存防火墙规则
  在RHEL/Fedora/CentOS Linux下,可以使用下面的命令保存防火墙规则:
# service iptables save
  在其它Linux发行版(如Ubuntu)上,可以使用iptables-save命令保存防火墙规则:
# iptables-save > /root/my.active.firewall.rules
# cat /root/my.active.firewall.rules
  6、重新加载防火墙规则
  我们可以使用iptables-restore命令重新加载使用iptables-save命令保存的防火墙规则:
# iptables-restore < /root/my.active.firewall.rules
  我们还可以利用这种特性来快速部署防火墙规则。
  7、设置默认防火墙策略
  我们首先来配置一个防火墙策略,它默认丢弃所有的网络数据包:
# iptables -P INPUT DROP
# iptables -P OUTPUT DROP
# iptables -P FORWARD DROP
# iptables -L -v -n
#连接失败,因为防火墙丢弃所有的网络数据包
# ping cyberciti.biz
# wget http://www.kernel.org/pub/linux/kernel/v3.0/testing/linux-3.2-rc5.tar.bz2
  在此基础上,我们只关闭入站连接:
# iptables -P INPUT DROP
# iptables -P FORWARD DROP
# iptables -P OUTPUT ACCEPT
# iptables -A INPUT -m state --state NEW,ESTABLISHED -j ACCEPT
# iptables -L -v -n
#ping和wget可以正常工作
# ping cyberciti.biz
# wget http://www.kernel.org/pub/linux/kernel/v3.0/testing/linux-3.2-rc5.tar.bz2
  8、在公网网络接口上停用私有网络地址
  我们可以从公网网络接口上删除私有IPv4网段,以防止IP欺骗。运行下面的命令,没有源路由地址的数据包会被丢弃:
# iptables -A INPUT -i eth1 -s 192.168.0.0/24 -j DROP
# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP
  下面是私有网络IPv4地址范围,请确认在公网接口予以屏蔽:
  ● 10.0.0.0/8 -j (A)
  ● 172.16.0.0/12 (B)
  ● 192.168.0.0/16 (C)
  ● 224.0.0.0/4 (多播 D)
  ● 240.0.0.0/5 (E)
  ● 127.0.0.0/8 (回环)
  9、屏蔽IP地址访问
  如果我们想屏蔽一个IP地址,比如1.2.3.4,可以运行:
# iptables -A INPUT -s 1.2.3.4 -j DROP
# iptables -A INPUT -s 192.168.0.0/24 -j DROP
  10、屏蔽入栈端口请求
  如果我们想80端口上屏蔽所有的服务请求,可以运行:
# iptables -A INPUT -p tcp --dport 80 -j DROP
# iptables -A INPUT -i eth1 -p tcp --dport 80 -j DROP
  只想屏蔽IP地址1.2.3.4对80端口的请求,可以运行:
# iptables -A INPUT -p tcp -s 1.2.3.4 --dport 80 -j DROP
# iptables -A INPUT -i eth1 -p tcp -s 192.168.1.0/24 --dport 80 -j DROP
11、屏蔽出栈IP地址
  现在我们来演示如何屏蔽对主机名和IP地址的出栈访问。
  首先,我们来获取一个域名的IP地址:
# host -t a cyberciti.biz
  输出示例:
cyberciti.biz has address 75.126.153.206
  要屏蔽访问域名cyberciti.biz的网络数据包,可以运行:
# iptables -A OUTPUT -d 75.126.153.206 -j DROP
  下面是使用子网掩码的示例:
# iptables -A OUTPUT -d 192.168.1.0/24 -j DROP
# iptables -A OUTPUT -o eth1 -d 192.168.1.0/24 -j DROP
  下面我们以屏蔽facebook.com为例,进行说明。首先,我们需要facebook的所有IP地址:
# host -t a www.facebook.com
  示例输出:
www.facebook.com has address 69.171.228.40
  找出IP地址69.171.228.40的CIDR:
# whois 69.171.228.40 | grep CIDR
  示例输出:
CIDR:69.171.224.0/19
  现在我们来阻止对facebook.com的访问:
# iptables -A OUTPUT -p tcp -d 69.171.224.0/19 -j DROP
  我们也可以直接屏蔽域名:
# iptables -A OUTPUT -p tcp -d www.facebook.com -j DROP
# iptables -A OUTPUT -p tcp -d facebook.com -j DROP
  12、记录并丢弃数据包
  在公网网络接口上记录并丢弃IP地址欺骗数据包:
# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j LOG --log-prefix "IP_SPOOF A: "
# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP
  默认情况下日志记录在/var/log/messages文件中:
# tail -f /var/log/messages
# grep --color 'IP SPOOF' /var/log/messages
  我们还可以用-m参数对日志记录进行限制,以防止日志文件过度膨胀。
# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -m limit --limit 5/m --limit-burst 7 -j LOG --log-prefix "IP_SPOOF A: "
# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP
  13、根据MAC地址允许或阻止数据包的传入
  我们可以根据MAC地址允许或阻止数据包的传入:
# iptables -A INPUT -m mac --mac-source 00:0F:EA:91:04:08 -j DROP
  14、屏蔽ICMP ping请求
  我们可以通过允许下面的命令屏蔽ping请求:
# iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
# iptables -A INPUT -i eth1 -p icmp --icmp-type echo-request -j DROP
  也可以按照特定的网段和主机限制ping请求:
# iptables -A INPUT -s 192.168.1.0/24 -p icmp --icmp-type echo-request -j ACCEPT
  以下命令只接受受限制的ping请求:
#假定默认INPUT策略为丢弃数据包
# iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
# iptables -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
# iptables -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
#所有的服务器都对ping请求作出应答
# iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  15、开启端口序列
  下面的命令可以允许7000到7010范围内的TCP端口访问:
# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 7000:7010 -j ACCEPT
 16、允许一系列IP地址访问
  下面的命令可以允许IP地址范围
#运行IP地址范围192.168.1.100 到192.168.1.200 访问80端口
# iptables -A INPUT -p tcp --destination-port 80 -m iprange --src-range 192.168.1.100-192.168.1.200 -j ACCEPT
#NAT示例
# iptables -t nat -A POSTROUTING -j SNAT --to-source 192.168.1.20-192.168.1.25
  17、建立连接并重启防火墙
  当重启iptables服务时,它会断开所有已建立的连接。这是因为在重启防火墙时,会卸载IPTABLES_MODULES_UNLOAD模块。
  要解决这个问题,可以编辑/etc/sysconfig/iptables-config
IPTABLES_MODULES_UNLOAD = no
  18、使用Crit日志级别
# iptables -A INPUT -s 1.2.3.4 -p tcp --destination-port 80 -j LOG --log-level crit
  19、屏蔽或开启常见端口
  屏蔽或开启常用的TCP、UDP端口:
#可以使用DROP替换ACCEPT,实现端口屏蔽。
#打开22端口(SSH)
# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
# iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 22 -j ACCEPT
#打开TCP/UDP631端口(打印服务)
# iptables -A INPUT -s 192.168.1.0/24 -p udp -m udp --dport 631 -j ACCEPT
# iptables -A INPUT -s 192.168.1.0/24 -p tcp -m tcp --dport 631 -j ACCEPT
# 打开123端口,允许局域网用户进行NTP时间同步
# iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p udp --dport 123 -j ACCEPT
#打开25端口(SMTP)
# iptables -A INPUT -m state --state NEW -p tcp --dport 25 -j ACCEPT
# 打开DNS端口
# iptables -A INPUT -m state --state NEW -p udp --dport 53 -j ACCEPT
# iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
#打开http/https端口
# iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
# iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
#打开TCP110端口(POP3)
# iptables -A INPUT -m state --state NEW -p tcp --dport 110 -j ACCEPT
#打开TCP143端口
# iptables -A INPUT -m state --state NEW -p tcp --dport 143 -j ACCEPT
#为局域网用户开启Samba访问
# iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 137 -j ACCEPT
# iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 138 -j ACCEPT
# iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 139 -j ACCEPT
# iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 445 -j ACCEPT
#为局域网用户开启代理服务器访问
# iptables -A INPUT -s 192.168.1.0/24 -m state --state NEW -p tcp --dport 3128 -j ACCEPT
#为局域网用户开启MySQL访问
# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
20、限制客户端IP的并发连接数
  我们可以使用connlimit模块限制客户端IP的并发连接数。下面的命令允许每个客户端只能并发3个ssh连接:
# iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT
  设置HTTP并发连接为20个:
# iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j DROP
  参数说明:
  ● --connlimit-above 3:连接数超过3个自动匹配
  ● --connlimit-mask 24:子网掩码匹配
  更好的使用iptables
  首先,我们要学会查看man手册:
$ man iptables
  我们还可以这样查看帮助:
# iptables -h
  我们还可以查看特定命令的帮助:
# iptables -j DROP -h
  测试防火墙
  测试端口是否开放:
# netstat -tulpn
  测试TCP 80端口是否开放:
# netstat -tulpn | grep :80
  如果80端口未开放,请确保启动Apache服务器:
# service httpd start
  并确保打开iptables防火墙80端口:
# iptables -L INPUT -v -n | grep 80
  如果80端口没有开放,可以运行下面的命令:
# iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
# service iptables save
  下面使用telnet命令测试是否可以连接到80端口:
$ telnet www.cyberciti.biz 80
  下面是示例输出:
Trying 75.126.153.206...
Connected to www.cyberciti.biz.
Escape character is '^]'.
^]
telnet> quit
Connection closed.
  最后,我们也推荐使用嗅探工具(如tcpdump、ngrep)对防火墙设置进行测试。
  以上只是一些基本的防火墙配置策略,如果你想构造更复杂的防火墙策略,需要对TCP/IP和Linux内核配置文件sysctl.conf进行更深入的学习。

 

posted @   念槐聚  阅读(755)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2012-11-27 [原][linux]踢出某正在访问的用户||永久禁止某IP访问
2011-11-27 [收藏]远程桌面工具
点击右上角即可分享
微信分享提示