屏蔽出站
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. 跟着廖雪峰学python 003

    ​ ​编辑 列表和元组 list 是一种有序.可变的数据类型,可添加删除其中的元素. len()函数:可以获取列表元素的个数 classmates = ['Micheal' , 'Bob' , 'Ja ...

  2. 关于C#中async/await的用法

    一直对c#中async/await的用法模模糊糊,不太清晰,今天写了一下Demo彻底明确一下async/await的用法,以免因为对其不了解而对后期的业务产生影响(比如事务导致的锁表等等). 1. 首 ...

  3. 借助 Flutter 跨平台特性连接 10 亿玩家 | Flutter 开发者故事

    由光子工作室及 Krafton 联合研发的 PUBG MOBILE 依然保持着极高的人气,目前全球有 10 亿玩家,日活跃 5,000 万 (不包括中国大陆地区).从游戏策划伊始,团队就打算为各个平台 ...

  4. 1 - 【RocketMQ 系列】CentOS 7.6 安装部署RocketMQ

    一.前置准备工作 CentOS 7.6 安装 jdk1.8 openjdk 1.查看JDK版本 yum search java|grep jdk 2.安装jdk1.8,安装默认的目录为: /usr/l ...

  5. python3中,isinstance() 函数

    #isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type(). #返回值:如果对象的类型与参数二的类型相同则返回True,否则返回False 使用isinstance函数的实 ...

  6. 树莓派4B—LCD触摸屏和硬件串口配置

    1.LCD触摸屏直接下载官网驱动,这里选用的是3.5寸显示屏,解压后直接运行 sudo ./LCD35-show 然后重启. 注意:一定要先安装LCD驱动,因为安装驱动会修改/boot/config. ...

  7. python爬虫学习——文件操作,异常处理

    文件操作 ''' f = open("a.txt","w") #打开一个文件, w模式(写),如果文件不存在就在当前目录下创建 f.write("he ...

  8. 【KAWAKO】TVM-tflite模型编译与优化

    目录 前言 准备模型 版本问题 精度问题 加载tflite模型 编译模型 在python上运行模型进行测试 加载输入数据 运行四连 优化(Autotune) 注: 前言 TVM的编译与优化主要有两种方 ...

  9. NOIP2022 总结

    \(\text{summary}\) 怎么都没想到这次题目那么有新意:把这样的题 \(T2\) 放 \(T2\)...... 策略出现很大问题,赛后也意识到很多选手也会出现同样的问题:死磕 \(T2\ ...

  10. 3D模型轻量化

    近几年,随着国内外文化产业的迅猛发展,3D建模行业迎来黄金发展期. 尤其是在元宇宙时代及数字体验经济时代的大背景下,越来越多的实时.可交互的3D内容将出现在人们的生活中. 有关3D建模师而言,无疑,行 ...