CentOS 7关闭防火墙 SElinux 配ip
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 端口 参考
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
转发端口——————————————————————————————
|
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规则添加注释的语法为 : 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设置端口转发
之前写过一篇Haproxy下的转发,但Haproxy无法转发udp数据,所以这次使用iptables来转发tcp和udp的数据,首先我们来看一下数据包的走向
- +--------+ +--------+ +--------+
- | | <--D-- | | <--C-- | |
- | 用户 | | 中转机 | | 目标 |
- | | --A--> | | --B--> | |
- +--------+ +--------+ +--------+
A: 用户发出加密请求到SSS服务器
B: SSS服务器解密请求后转发到目标服务器
C: 目标服务器回应SSS服务器
D: SSS服务器加密回应返回给用户然后解密后使用
清空相关规则
加载相关的内核模块
- /sbin/modprobe ip_tables
- /sbin/modprobe ip_nat_ftp
- /sbin/modprobe ip_conntrack_ftp
清除预设表 filter 中,所有规则链中的规则
- /sbin/iptables -F
清除nat表中,所有规则链中的规则
- /sbin/iptables -F -t nat
清除预设表 filter 中,使用者自订链中的规则
- /sbin/iptables -X
将封包计数器归零。封包计数器是用来计算同一封包出现次数,是过滤阻断式攻击不可或缺的工具
- /sbin/iptables -Z
清除mangle表中,所有规则链中的规则
- iptables -F -t mangle
清除mangle表中,使用者自订链中的规则
- iptables -t mangle -X
清除nat表中,使用者自订链中的规则
- iptables -t nat -X
定义链的规则(设定预设规则)
- /sbin/iptables -P INPUT ACCEPT
- /sbin/iptables -P FORWARD DROP
- /sbin/iptables -P OUTPUT ACCEPT
打开 forward 功能 (或在/etc/sysconfig/network 中添加 FORWARD_IPV4=yes 打开转发功能,实现各网段互访)
- echo "1"> /proc/sys/net/ipv4/ip_forward
配置iptables转发
- iptables -t nat -A POSTROUTING -j MASQUERADE
- iptables -A FORWARD -s 1.1.1.1 -j ACCEPT
- iptables -t nat -A PREROUTING -p tcp --dport 10000:20000 -j DNAT --to-destination 1.1.1.1:10000-20000
- iptables -t nat -A PREROUTING -p udp --dport 10000:20000 -j DNAT --to-destination 1.1.1.1:10000-20000
- iptables -t nat -A POSTROUTING -p tcp -d 1.1.1.1 --dport 10000:20000 -j SNAT --to-source 2.2.2.2
- iptables -t nat -A POSTROUTING -p udp -d 1.1.1.1 --dport 10000:20000 -j SNAT --to-source 2.2.2.2
- iptables -A INPUT -p tcp --dport 22 -j ACCEPT
- iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
然后继续下面的操作
- ###重要说明###
- 1.1.1.1 为目标服务器的IP地址
- 10000:20000 需要转发的端口范围
- 2.2.2.2 本机服务器的IP(也就是中转机)
- 最后面两条是ssh的端口,根据你自己使用的ssh端口进行添加
- ---------------------------------------------------
- 然后保存和重启iptables以及添加开机启动
- 保存:service iptables save
- 重启:service iptables restart
- 开机启动:chkconfig iptables on
iptables转发的配置也就这么多,写这些步骤的目的只是为了让大家明白原理,下面贴出脚本下载地址:
- wget --no-check-certificate https://soft.jiasu.cloud/iptables/iptables.sh
此脚本并非是一键脚本,下载后对照上面的说明,使用vim修改里面的IP地址以及端口,修改完以后保存,然后执行:bash iptables.sh
附件列表
CentOS 7关闭防火墙 SElinux 配ip的更多相关文章
- CentOS关闭防火墙&SELinux
须知: 防火墙配置文件:/etc/sysconfig/iptables 查看防火墙状态:service iptables status 关闭防火墙:service iptables stop 关闭ip ...
- CentOS 7 关闭防火墙
CentOS 7.0默认使用的是firewall作为防火墙 直接关闭防火墙 systemctl stop firewalld.service #停止firewall systemctl disable ...
- hadoop学习;安装jdk,workstation虚拟机v2v迁移;虚拟机之间和跨物理机之间ping网络通信;virtualbox的centos中关闭防火墙和检查服务启动
JDK 在Ubuntu下的安装 与 环境变量的配置 前期准备工作: 找到 JDK 和 配置TXT文件 并拷贝到桌面下 不是目录 而是文件拷贝到桌面下 以下的命令部分就直接复制粘贴就能够了 1.配 ...
- 利用CentOS系统IPtables防火墙添加网站IP白名单
参考博文: 利用CentOS系统IPtables防火墙添加360网站卫士节点IP白名单 centos6.5添加白名单如下: 在防火墙 配置文件中加入白名单 ip -A INPUT -s 183.13 ...
- CentOS虚拟机关闭防火墙
关闭防火墙 systemctl stop firewalld 关闭防火墙开机自启动 systemctl disable firewalld 关闭安全机制,将selinux设置为disabled vi ...
- centos 如何关闭防火墙?
1 查看防火墙状态: 命令: /etc/init.d/iptables status 如果是开着显示内容类是截图 2 临时关闭防火墙: 命令:/etc/init.d/iptables stop ...
- CentOS 7 关闭防火墙和SELinux
[修改机器名] # vi /etc/hostname [关SELinux] # vi /etc/selinux/config设置SELINUX=disabled [关防火墙] # systemctl ...
- CentOS 系统开启防火墙,屏蔽IP,解决DDOS攻击
刚才发现网站特别慢,然后看了一下服务器状态 CPU 负载100%. 然后看了下网络,发现一个IP一直在请求本服务器的 443 端口,就是本站. 然后在终端通过 iftop 命令(一个流量健康软件,如果 ...
- Linux CentOS 下关闭防火墙
永久关闭(重启后生效) 开启: chkconfig iptables on 关闭: chkconfig iptables off 临时关闭(重启后失效) 开启: service iptables st ...
- CentOS 8 关闭防火墙
SELINUX=disabled vim /etc/selinux/config systemctl disable firewalld.service
随机推荐
- 【随笔记】SiliconLabs Android Demo 编译
AndroidStudio:3.5.2 AndroidPhone:XiaoMi5 (Android 8.0) AndroidDemo:3.1.4-44 AndroidDemo 源码路径:Simplic ...
- springBoot简单记录日志
记录日志的几种方法 springboot项目内置日志框架 在配置文件中添加以下配置: logging: file: name: "./log/xxx.log" pattern: f ...
- 【学习笔记】Http请求方法总结
Http常用请求方法对比 请求方法 常见参数传递方式 是否幂等 说明 API举例 GET URL,注意:Http协议对URL长度没有限制,所谓的限制是浏览器和处理服务器的 幂等 用于查询 批量查询:/ ...
- IDEA 2022 开启热部署
POM.XML <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 亲测有效! Bypass V1.15.5 12306分流抢票助手 for Windows
亲测有效! Bypass V1.15.5 12306分流抢票助手 for Windows 12306Bypass也就是12306分流抢票软件,是一款全程自动抢票,自动识别验证码,多线程秒单.稳定捡漏, ...
- elasticsearch之使用正则表达式自定义分词逻辑
一.Pattern Analyzer简介 elasticsearch在索引和搜索之前都需要对输入的文本进行分词,elasticsearch提供的pattern analyzer使得我们可以通过正则表达 ...
- ctfshow_web入门 命令执行
命令执行 刚刚开始学习命令执行,萌新一个 因为是学习嘛,所以东西写的杂乱了 web 29 error_reporting(0); if(isset($_GET['c'])){ $c = $_GET[' ...
- LG P5043 树同构
\(\text{problem}\) 无根树同构的判断 \(\text{Analysis}\) 考虑树哈希,注意使用较正确的哈希方法 无根树同构有个性质 只要判断以这两棵树的重心为根是否同构即可 \( ...
- JZOJ 3226. 【HBOI2013】ALO
题目 网上自己搜 解析 区间异或很容易想到可持久化字典树 但本题的关键是如何高效率求出以某个数为区间最大值时这个区间的范围 依题我们知道区间最长可到比它第二大的位置(开区间) 所以我们如果能找到每个数 ...
- 如何在VC++ 6.0中实现拖动指令改变执行路径?
前文提要: 在VC6.0之后出现的VS系列开发工具都具有的调试功能:移动指针更改执行流,VC6不支持这个UI操作. 调试程序暂停时,源代码或"反汇编"窗口边距处的黄色箭头标记要运行 ...