屏蔽出站
iptables -t filter -A OUTPUT --dst 192.168.0.191/32 -j DROP
iptables -t filter -A OUTPUT --dst 192.168.0.192/32 -j DROP
iptables -t filter -A OUTPUT --dst 192.168.0.193/32 -j DROP
#!/bin/sh

# reset :
iptables -t filter -F
iptables -t filter -X # Block all :
iptables -t filter -P INPUT DROP
iptables -t filter -P FORWARD DROP
iptables -t filter -P OUTPUT DROP # Authorize already established connections :
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # Authorize backloop :
iptables -t filter -A INPUT -i lo -j ACCEPT
iptables -t filter -A OUTPUT -o lo -j ACCEPT # Authorize ssh :
iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 22 -j ACCEPT # Authorize HTTP :
iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT # Authorize HTTPS :
iptables -t filter -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 443 -j ACCEPT # Authorize DNS :
iptables -t filter -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -t filter -A INPUT -p udp --dport 53 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT # Ping :
iptables -t filter -A INPUT -p icmp -j ACCEPT
iptables -t filter -A OUTPUT -p icmp -j ACCEPT # Authorize FTP :
iptables -t filter -A INPUT -p tcp --dport 20 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 20 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 21 -j ACCEPT # # Authorize NTP :
# iptables -t filter -A INPUT -p udp --dport 123 -j ACCEPT
# iptables -t filter -A OUTPUT -p udp --dport 123 -j ACCEPT # Authorize IRC :
iptables -t filter -A INPUT -p tcp --dport 6667 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 6667 -j ACCEPT # Authorize port 10000 (for Node.JS server) :
iptables -t filter -A INPUT -p tcp --dport 10000 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 10000 -j ACCEPT # Authorize port 631 (Cups server) :
iptables -t filter -A INPUT -p tcp --dport 631 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 631 -j ACCEPT # Authorize port 9418 (git) :
iptables -t filter -A INPUT -p tcp --dport 9418 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 9418 -j ACCEPT
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0] #定义白名单群组
-N whitelist
#设置白名单IP,开放ip可以访问设备,白名单就按类似规则增加就可以
-A whitelist -s 192.168.1.100 -j ACCEPT
-A whitelist -s 100.20.30.40 -j ACCEPT
-A whitelist -s 1.2.3.4 -j ACCEPT
-A whitelist -s 25.36.47.58 -j ACCEPT
#开放所有设备主动访问的服务器,处在RELATED,ESTABLISHED状态可以通信,这样设备需要主动访问的服务器就不用一个一个添加到白名单了
-A INPUT -m state --state RELATED,ESTABLISHED -j whitelist
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
#白名单端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j whitelist
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j whitelist
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9000 -j whitelist
#也可以这样写
#-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306,80,9000 -j whitelist
#拒绝白名单以外IP访问指定端口
-A INPUT -p tcp -m multiport --dports 3306,80,9000 -j DROP
COMMIT

对指定ip开放1024——65535之间的端口

-A INPUT -s 61.135.125.14 -p tcp --dport 1024:65535 -m comment --comment "仅允许公司ip连"  -j ACCEPT

————

转发 SSH 端口   参考

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
iptables -t nat -A PREROUTING -d 1.1.1.1 -p tcp -m tcp --dport 22 -j DNAT --to-destination 10.0.0.2:22
iptables -t nat -A POSTROUTING -d 10.0.0.2 -p tcp -m tcp --dport 22 -j MASQUERADE
service iptables save
MASQUERADE 和 SNAT 效果基本相同。它们的作用:所有从本地网络出去的包的源地址改为 Internet 连接的地址。如果不作 SNAT 或 MASQUERADE ,返回给客户端的数据包不知道怎么发出去
区别:
SNAT 必须指定 source IP 地址
MASQUERADE 不需要指定 source IP 地址动态 IP 的情况必须用 MASQUERADE
 
iptables -t nat -A PREROUTING  -p tcp -m tcp --dport 3389 -j DNAT --to-destination 192.168.0.218



——

转发端口——————————————————————————————


2
3
4
iptables -t nat -A PREROUTING -p tcp --dport [端口号] -j DNAT --to-destination [目标IP]
iptables -t nat -A PREROUTING -p udp --dport [端口号] -j DNAT --to-destination [目标IP]
iptables -t nat -A POSTROUTING -p tcp -d [目标IP] --dport [端口号] -j SNAT --to-source [本地服务器IP]
iptables -t nat -A POSTROUTING -p udp -d [目标IP] --dport [端口号] -j SNAT --to-source [本地服务器IP]

iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.0.218
iptables -t nat -A POSTROUTING -p tcp -d 192.168.0.218 --dport 80 -j SNAT --to-source 192.168.0.219

iptables -A INPUT -p tcp -m tcp --dport 80 -m comment --comment "转发80到wordpress" -j ACCEPT



为新的iptables规则添加注释的语法为 :  comment --comment “要添加的注释文字”

具体的例子:下面添加一条允许ssh流量通过的规则,并且给这条规则添加注释:

$ sud iptables -A INPUT -p tcp -m tcp --dport 22 -m comment --comment "allow SSH to this host from anywhere" -j ACCEPT
————————————————————————————————————

CentOS 7关闭防火墙 SElinux 配ip (2016/6/2 9:34:10)


systemctl stop firewalld.service && systemctl disable firewalld.service && sed -i '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config && source /etc/selinux/config && setenforce 0
sed -i '/匹配字符串/s/源字符串/目标字符串/g' ab.txt


IP='192.168.5.21'

sed '3a IPADDR="192.168.1.23"' -i /etc/sysconfig/network-scripts/ifcfg-em2
sed '4a NETMASK="255.255.225.0"' -i /etc/sysconfig/network-scripts/ifcfg-em2 sed -i '/BOOTPROTO/s/dhcp/static/' /etc/sysconfig/network-scripts/ifcfg-em2
sed -i '/ONBOOT/s/no/yes/' /etc/sysconfig/network-scripts/ifcfg-em2

sed '5a GATEWAY="192.168.5.1"' -i /etc/sysconfig/network-scripts/ifcfg-em2
sed '6a DNS1="114.114.114.114"' -i /etc/sysconfig/network-scripts/ifcfg-em2

sed '2 ittt' -i a.txt # 在第2行前插入ttt,并且将结果更新到a.txt(


CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。
firewall:
systemctl start firewalld.service#启动firewall
systemctl stop firewalld.service#停止firewall
systemctl disable firewalld.service#禁止firewall开机启动
追答:
改成iptables
firewall:
systemctl start iptables.service

屏蔽端口 屏蔽3306端口
iptables -A OUTPUT -p tcp --dport 3306 -j DROP
iptables -A INPUT -p tcp --dport 3306 -j DROP

关闭centos7自带防火墙启用iptables

systemctl mask firewalld
systemctl stop firewalld
yum install iptables-services
systemctl enable iptables
systemctl start iptables

centos7防火墙添加开放端口
实际命令如下:
firewall-cmd --permanent --add-port=3389/tcp
执行可以成功  用该命令查询firewall-cmd --permanent --query-port=1000/tcp
 
#firewall-cmd --reload
查看当前默认区域
 firewall-cmd --get-default-zone
改变网卡接口区域  em2是网卡名
firewall-cmd --zone=trusted --change-interface=em2
查看网卡所在区域
 firewall-cmd --get-zone-of-interface=em2
 
 
 
 
 
 
 
 service iptables start
iptables -A INPUT -p tcp --dport 你的vps端口号 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 你的vps端口号 -j ACCEPT
service iptables save
————————————————————
启动失败时 Assertion failed for IPv4 firewall with iptables
service iptables save
systemctl stop firewalld
systemctl disable firewalld
systemctl start iptables
systemctl status iptables
systemctl enable iptables




CentOS系统中使用iptables设置端口转发 - 高明松博客 - Mozilla Firefox (2016/8/5 18:52:51)

CentOS系统中使用iptables设置端口转发 - 高明松博客 - Mozilla Firefox

CentOS系统中使用iptables设置端口转发


  之前写过一篇Haproxy下的转发,但Haproxy无法转发udp数据,所以这次使用iptables来转发tcp和udp的数据,首先我们来看一下数据包的走向

  1. +--------+ +--------+ +--------+
  2. | | <--D-- | | <--C-- | |
  3. | 用户 | | 中转机 | | 目标 |
  4. | | --A--> | | --B--> | |
  5. +--------+ +--------+ +--------+

A: 用户发出加密请求到SSS服务器

B: SSS服务器解密请求后转发到目标服务器

C: 目标服务器回应SSS服务器

D: SSS服务器加密回应返回给用户然后解密后使用

清空相关规则

加载相关的内核模块

  1. /sbin/modprobe ip_tables
  2. /sbin/modprobe ip_nat_ftp
  3. /sbin/modprobe ip_conntrack_ftp

清除预设表 filter 中,所有规则链中的规则

  1. /sbin/iptables -F

清除nat表中,所有规则链中的规则

  1. /sbin/iptables -F -t nat

清除预设表 filter 中,使用者自订链中的规则

  1. /sbin/iptables -X

将封包计数器归零。封包计数器是用来计算同一封包出现次数,是过滤阻断式攻击不可或缺的工具

  1. /sbin/iptables -Z

清除mangle表中,所有规则链中的规则

  1. iptables -F -t mangle

清除mangle表中,使用者自订链中的规则

  1. iptables -t mangle -X

清除nat表中,使用者自订链中的规则

  1. iptables -t nat -X

定义链的规则(设定预设规则)

  1. /sbin/iptables -P INPUT ACCEPT
  2. /sbin/iptables -P FORWARD DROP
  3. /sbin/iptables -P OUTPUT ACCEPT

打开 forward 功能 (或在/etc/sysconfig/network 中添加 FORWARD_IPV4=yes 打开转发功能,实现各网段互访)

  1. echo "1"> /proc/sys/net/ipv4/ip_forward

配置iptables转发

  1. iptables -t nat -A POSTROUTING -j MASQUERADE
  2. iptables -A FORWARD -s 1.1.1.1 -j ACCEPT
  3. iptables -t nat -A PREROUTING -p tcp --dport 10000:20000 -j DNAT --to-destination 1.1.1.1:10000-20000
  4. iptables -t nat -A PREROUTING -p udp --dport 10000:20000 -j DNAT --to-destination 1.1.1.1:10000-20000
  5. iptables -t nat -A POSTROUTING -p tcp -d 1.1.1.1 --dport 10000:20000 -j SNAT --to-source 2.2.2.2
  6. iptables -t nat -A POSTROUTING -p udp -d 1.1.1.1 --dport 10000:20000 -j SNAT --to-source 2.2.2.2
  7. iptables -A INPUT -p tcp --dport 22 -j ACCEPT
  8. iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

然后继续下面的操作

  1. ###重要说明###
  2. 1.1.1.1 为目标服务器的IP地址
  3. 10000:20000 需要转发的端口范围
  4. 2.2.2.2 本机服务器的IP(也就是中转机)
  5. 最后面两条是ssh的端口,根据你自己使用的ssh端口进行添加
  6. ---------------------------------------------------
  7. 然后保存和重启iptables以及添加开机启动
  8. 保存:service iptables save
  9. 重启:service iptables restart
  10. 开机启动:chkconfig iptables on

iptables转发的配置也就这么多,写这些步骤的目的只是为了让大家明白原理,下面贴出脚本下载地址:

  1. wget --no-check-certificate https://soft.jiasu.cloud/iptables/iptables.sh

此脚本并非是一键脚本,下载后对照上面的说明,使用vim修改里面的IP地址以及端口,修改完以后保存,然后执行:bash iptables.sh

附件列表

CentOS 7关闭防火墙 SElinux 配ip的更多相关文章

  1. CentOS关闭防火墙&SELinux

    须知: 防火墙配置文件:/etc/sysconfig/iptables 查看防火墙状态:service iptables status 关闭防火墙:service iptables stop 关闭ip ...

  2. CentOS 7 关闭防火墙

    CentOS 7.0默认使用的是firewall作为防火墙 直接关闭防火墙 systemctl stop firewalld.service #停止firewall systemctl disable ...

  3. hadoop学习;安装jdk,workstation虚拟机v2v迁移;虚拟机之间和跨物理机之间ping网络通信;virtualbox的centos中关闭防火墙和检查服务启动

    JDK 在Ubuntu下的安装 与 环境变量的配置 前期准备工作: 找到  JDK 和 配置TXT文件  并拷贝到桌面下  不是目录 而是文件拷贝到桌面下 以下的命令部分就直接复制粘贴就能够了 1.配 ...

  4. 利用CentOS系统IPtables防火墙添加网站IP白名单

    参考博文: 利用CentOS系统IPtables防火墙添加360网站卫士节点IP白名单 centos6.5添加白名单如下: 在防火墙 配置文件中加入白名单  ip -A INPUT -s 183.13 ...

  5. CentOS虚拟机关闭防火墙

    关闭防火墙 systemctl stop firewalld 关闭防火墙开机自启动 systemctl disable firewalld 关闭安全机制,将selinux设置为disabled vi ...

  6. centos 如何关闭防火墙?

    1 查看防火墙状态: 命令: /etc/init.d/iptables status 如果是开着显示内容类是截图 2 临时关闭防火墙: 命令:/etc/init.d/iptables stop     ...

  7. CentOS 7 关闭防火墙和SELinux

    [修改机器名] # vi /etc/hostname [关SELinux] # vi /etc/selinux/config设置SELINUX=disabled [关防火墙] # systemctl ...

  8. CentOS 系统开启防火墙,屏蔽IP,解决DDOS攻击

    刚才发现网站特别慢,然后看了一下服务器状态 CPU 负载100%. 然后看了下网络,发现一个IP一直在请求本服务器的 443 端口,就是本站. 然后在终端通过 iftop 命令(一个流量健康软件,如果 ...

  9. Linux CentOS 下关闭防火墙

    永久关闭(重启后生效) 开启: chkconfig iptables on 关闭: chkconfig iptables off 临时关闭(重启后失效) 开启: service iptables st ...

  10. CentOS 8 关闭防火墙

    SELINUX=disabled vim /etc/selinux/config systemctl disable firewalld.service

随机推荐

  1. Node.js+Koa2+TypeScript技术概览

    最近几年一直使用Node.js作为后端服务平台,通过Koa2框架中间件快速搭建Web服务,但是使用JavaScript开发大型后端服务时会使程序变得难以维护,继而使用TypeScript语言开发,使编 ...

  2. 百万级数据excel导出功能如何实现?

    前言 最近我做过一个MySQL百万级别数据的excel导出功能,已经正常上线使用了. 这个功能挺有意思的,里面需要注意的细节还真不少,现在拿出来跟大家分享一下,希望对你会有所帮助. 原始需求:用户在U ...

  3. ATM购物车大作业

    项目开发流程 1.需求分析:品经理与架构师,根据客户的需求,理出一套比较容易编写的流程 2.架构设计:架构师根据具体的业务需求选择 具体的开发编程语言与项目框架,所需要的数据库(主库,从库).与开发目 ...

  4. 视觉十四讲:第七讲_3D-3D:ICP估计姿态

    1.ICP 假设有一组配对好的3D点, \(P={P_{1}, ..., P_{N}}\) , \(P^{'}={P_{1}^{'}, ..., P_{N}^{'}}\). 有一个欧式变换R,t,使得 ...

  5. UBUNTU16.04卸载安装protobuf

    1.卸载 sudo apt-get remove libprotobuf-dev which protoc 然后删除路径即可 2.安装 sudo apt-get install autoconf au ...

  6. 剑指 Offer 34. 二叉树中和为某一值的路径(java解题)

    目录 1. 题目 2. 解题思路 3. 数据类型功能函数总结 4. java代码 1. 题目 给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总 ...

  7. [代码审计基础 14]某cms变量覆盖到N处漏洞

    PHP:5.4.5 设置调试:https://blog.csdn.net/m0_46641521/article/details/120107786 PHPCMS变量覆盖到SQL注入 0x01:路由分 ...

  8. chatGPT帮助开发实战解答问题和反思

    问题来自.Net开发群友 问题 我想做一个自动生成单据号的功能,但是在EFCORE里没有行级锁,请有什么等价方案吗? ChatGPT回答 在 EF Core 中确实没有提供行级锁(row-level ...

  9. CRT 与 exCRT

    感觉网上很多写的都不是很清楚啊 awa. CRT 就是解这个方程 \(\begin{cases}x\equiv r_1 \pmod {m_1}\\ x\equiv r_2 \pmod {m_2} \\ ...

  10. 跳板攻击之:Socks 代理转发

    跳板攻击之:Socks 代理转发 目录 跳板攻击之:Socks 代理转发 1 Socks 代理转发原理 2 实验环境 2.1 客户端配置 2.2 服务端配置 2.3 利用 Socks 代理扫描内网主机 ...