[ 总结 ] web server iptables 简单配置
2023-09-27 14:20:25 时间
[root@server ~]# iptables -F [root@server ~]# iptables -X [root@server ~]# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # 不允许服务器主动建立新连接 [root@server ~]# iptables -A INPUT -p tcp -m multiport --dport 22,80 -m state --state NEW -j ACCEPT # 允许22,80端口的连接和监听 [root@server ~]# iptables -A OUTPUT -p tcp -m multiport --sport 22,80 -j ACCEPT # 允许客户端访问22,80端口 [root@server ~]# iptables -P INPUT DROP # 默认禁止 [root@server ~]# iptables -P FORWARD DROP # 默认禁止 [root@server ~]# iptables -P OUTPUT DROP # 默认禁止 [root@server ~]# iptables -A INPUT -p udp --sport 53 -j ACCEPT # 允许dns服务 [root@server ~]# iptables -A OUTPUT -p udp --dport 53 -j ACCEPT # 允许dns服务 [root@server ~]# iptables -A INPUT -p icmp -j ACCEPT # 开启 icmp协议 [root@server ~]# iptables -A OUTPUT -p icmp -j ACCEPT # 开启 icmp协议 [root@server ~]# service iptables save # 保存配置 iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] [root@server ~]# service iptables restart iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] iptables: Applying firewall rules: [ OK ]
对应一般的简单web服务器基本够用,当然ssh端口肯定会修改,以上命令也进行调整。如果要禁止别人ping服务器,建议进行以下设置:
临时生效:echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
永久生效:
[root@server ~]# echo "net.ipv4.icmp_echo_ignore_all = 1" >> /etc/sysctl.conf
[root@server ~]# sysctl -p
# Generated by iptables-save v1.4.7 on Mon Mar 21 18:13:01 2016 *filter :INPUT DROP [3:134] :FORWARD DROP [0:0] :OUTPUT DROP [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p tcp -m multiport --sports 22,80 -m state --state NEW -j ACCEPT -A INPUT -p udp -m udp --sport 53 -j ACCEPT -A INPUT -p icmp -j ACCEPT -A OUTPUT -p tcp -m multiport --sports 22,80 -j ACCEPT -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT -A OUTPUT -p icmp -j ACCEPT COMMIT # Completed on Mon Mar 21 18:13:01 2016
可以直接复制上面iptables配置到vim /etc/sysconfig/iptables 然后重启iptables
关于转发:
因为非root用户不能监听1024以下端口,所以经常使用iptables来进行转发的工作:
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 # 8080端口映射到80端口 iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.2.11:8080 # 192.168.2.11的8080端口映射到80端口。可用于两台主机转发
相关文章
- Spark History Server配置使用
- CentOS 7.5 安装与配置 Percona Server 5.7
- 配置SQL Server Reporting Services(SSRS)2012与SharePoint 2013集成
- ArcGIS Server开发教程系列(2)配置ARCMAP和ARCCatalog发布服务
- 解决在nginx+php环境下$_SERVER['PHP_SELF']获取不到值的问题
- 亲测有效,解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";
- SQL Server自动产生一年多少周及月
- 使用VisualSVN Server搭建SVN服务器(测试通过)
- 工业智能网关BL110应用之四十五: 数据上传云平台Modbus TCP Server配置
- linux杂谈(十三):代理server
- Sublime Text 3使用技巧 | server
- Introduction to Change Data Capture (CDC) in SQL Server 2008[转]
- nginx 配置指令之server_name
- 聊聊WebRTC网关服务器3:如何优化Server的线程方案?
- (总结)CentOS Linux下VNC Server远程桌面配置详解
- 升级到0.9 log4jmongodb(mongo-java-driver 3.x)后,报No server chosen by WritableServerSelector from cluster description ClusterDescription
- 在ubuntu server中安装和配置docker