centos 7 防火墙配置和白名单问题-修改远程端口

1、Centos 7 配置:

查看防火墙状态:
systemctl status firewalld

开启防火墙并设置开机自启

systemctl start firewalld

systemctl enable firewalld

 

开放 22端口:
firewall-cmd --zone=public --add-port=22/tcp --permanent 重新载入一下: firewall-cmd --reload 查看下是否生效: firewall-cmd --zone=public --query-port=22/tcp 查看开放的端口: firewall-cmd --zone=public --list-ports 批量开放端口: firewall-cmd --zone=public --add-port=100-500/tcp --permanent 查看是否生效 firewall-cmd --zone=public --list-rich-rules

2. 插入代码:

#!/bin/bash

# enable the firewall service
service firewalld start

# config firewall to permit ip range:172.16.17.1-70, port:1521
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="172.16.1.0/26" port protocol="tcp" port="1521" accept'

# permit 172.16.17.63, since it is broadcast address in above ip range.
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="172.16.1.63" port protocol="tcp" port="1521" accept'

# permit 172.16.17.64-70 one by one
#不指定端口

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.2.3.10" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.0.21.51" accept"

#制定端口
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="172.1.17.64" port protocol="tcp" port="1521" accept'
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="172.1.17.65" port protocol="tcp" port="1521" accept'
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="172.1.17.70" port protocol="tcp" port="1521" accept'

#删除
firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="10.52.13.170" port protocol="tcp" port="1521" accept';

# reload
for taking effect this time firewall-cmd --reload

 

3. 查看文件,修改规则

vi /etc/firewalld/zones/public.xml

常用命令:
systemctl start firewalld #启动
systemctl stop firewalld #停止
systemctl status firewalld #查看状态
systemctl disable firewalld #开机禁用
systemctl enable firewalld #开机启动

开放或关闭端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent #开放80/tcp端口 (--permanent永久生效,没有此参数重启后失效)
firewall-cmd --zone=public --query-port=80/tcp #查看80/tcp端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent #关闭80/tcp端口

批量开放或关闭端口:
firewall-cmd --zone=public --add-port=40000-45000/tcp --permanent #批量开放端口,打开从40000到45000之间的所有端口
firewall-cmd --zone=public --list-ports #查看系统所有开放的端口
firewall-cmd --zone=public --remove-port=40000-45000/tcp --permanent #批量关闭端口,关闭从40000到45000之间的所有端口

4. centos 6 防火墙配置:

启动停止:
service iptables start service iptables stop service iptables status service iptables restart

开放某个端口:

iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 16379 -j ACCEPT;

放开某个ip:

iptables -I INPUT -s 11.49.253.191 -j ACCEPT;
iptables -I INPUT -s 169.254.0.0/16 -j ACCEPT;
iptables -A INPUT -s 11.49.253.83 -p tcp --dport 22022 -j ACCEPT;

禁用其他规则:
iptables -P INPUT DROP;

删除某个
iptables -D INPUT -s 169.254.0.0/16 -j ACCEPT;

保存配置:

service iptables save;

配置生效:
service iptables restart;

查看配置:

cat /etc/sysconfig/iptables

其他方法:
# 打开配置文件 
vi /etc/sysconfig/iptables 
# 直接删除对应端口的那句 !!!! 
# 保存(按Esc退出编辑) 
:wq 
# 重启防火墙,修改完成 
service iptables restart 
#保存修改 
/etc/init.d/iptables save 
#查看规则是否添加成功 
vi /etc/sysconfig/iptables 
#添加到iptables服务自启动 
chkconfig iptables on 
#重启iptables服务 
service iptables restart 

 

备注:

启动防火墙失败:

原因:python版本问题 python --version 查看版本

解决: vi /usr/sbin/firewalld

头部内容默认是 “#!/usr/bin/python” ,需要修改为: “#!/usr/bin/python/python2.7”

 

修改远程访问端口

#配置文件
sudo vi /etc/ssh/sshd_config
#修改端口:
Port 9022
#重启
sudo systemctl restart sshd

#防火墙添加
sudo firewall-cmd --permanent --add-port=29022/tcp
sudo firewall-cmd --reload

#iptables 防火墙处理
sudo iptables -I INPUT -p tcp --dport 2222 -j ACCEPT

 

posted @ 2020-11-19 11:20  leolzi  阅读(2529)  评论(0编辑  收藏  举报