CentOS7如何设置防火墙

1. firewall配置

在旧版本的CentOS中,是使用 iptables 命令来设置防火墙的。但是,从CentOS7开始,默认就没有安装iptables,而是改用firewall来配置防火墙。

firewall的配置文件是以xml的格式,存储在 /usr/lib/firewalld/ 和 /etc/firewalld/ 目录中。

1.1 系统配置目录
/usr/lib/firewalld/
/usr/lib/firewalld/services
/usr/lib/firewalld/zones
1.2 用户配置目录
/etc/firewalld/
/etc/firewalld/services
/etc/firewalld/zones
1.3 设置防火墙

设置防火墙的方式有两种:firewall命令和直接修改配置文件。

推荐使用firewall命令来设置防火墙。

注意: 对防火墙所做的更改,必须重启防火墙服务,才会立即生效。命令如下:

service firewalld restart
或
systemctl restart firewalld

重启防火墙
1.3.1 firewall命令
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 对外开放3306端口,供外部的计算机访问
# 该命令方式添加的端口,可在/etc/firewalld/zones中的对应配置文件中得到体现

systemctl restart firewalld
# 重启防火漆

说明:

  • firewall-cmd:Linux中提供的操作firewall的工具。
  • –zone:指定作用域。
  • –add-port=80/tcp:添加的端口,格式为:端口/通讯协议。
  • –permanent:表示永久生效,没有此参数重启后会失效。
1.3.2 直接修改配置文件

/etc/firewalld/zones/public.xml 文件的默认内容为:

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="dhcpv6-client"/>
  <service name="ssh"/>
</zone>

修改该配置文件,来添加3306端口。修改后的内容为:

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="dhcpv6-client"/>
  <service name="ssh"/>
  <port protocol="tcp" port="3306"/>
</zone>

2. firewall常用命令

2.1 查看firewall的状态
service firewalld status

systemctl status firewalld

firewall-cmd --state
2.2 启动、停止、重启
service firewalld start
或
systemctl start firewalld
# 启动

service firewalld stop
或
systemctl stop firewalld
# 停止

service firewalld restart
或
systemctl restart firewalld
# 重启
2.3 开机自启动的关闭与开启
systemctl disable firewalld
# 关闭开机自启动

systemctl enable firewalld
# 开启开机自启动
2.4 查看防火墙的规则
firewall-cmd --list-all 

3. CentOS7更改为iptables防火墙

CentOS7切换到iptables防火墙,首先应该关闭默认的firewall防火墙并禁止自启动,然后再来安装和启动iptables防火墙。

操作步骤如下:

systemctl stop firewalld
# 停止firewall

systemctl disable firewalld
# 禁止firewall的开机自启动

yum install iptables-services
# 安装iptables

systemctl start iptables
# 开启iptables

systemctl enable iptables
# 启用iptables的自启动

之后,就可以在CentOS7中使用iptables配置防火墙。

允许外部的计算机访问mysql,操作如下:

iptables -A INPUT -p tcp -dport 3306 -j ACCEPT
# 添加3306端口

service iptables save
# 保存当前的防火墙策略

service iptables restart
# 重启iptables

iptables常用命令:

service iptables start
# 启动iptables

service iptables stop
# 停止iptables

service iptables restart
# 重启iptables

service iptables status
# 查看iptables的状态

iptables防火墙,非常重要的两个文件:

  • 配置文件 /etc/sysconfig/iptables-config

  • 策略文件 /etc/sysconfig/iptables(默认是不存在的,使用service ipatables save 可以保存当前策略)

  • 10
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在CentOS 7上,设置防火墙端口通常需要root用户权限,因此需要输入系统内密码。这是因为设置防火墙端口需要更改系统级别的配置文件和服务,这些操作需要具有管理员权限的用户来执行。输入系统密码是一种安全机制,可以确保只有授权用户才能进行系统级别的更改和操作。 通过输入系统密码,CentOS 7可以验证用户的身份,并确保只有管理员才能进行修改防火墙端口的操作。这样可以防止未经授权的用户或恶意攻击者对系统进行任意更改,提高了系统的安全性。 要设置防火墙端口,可以使用“firewall-cmd”命令,该命令可以通过命令行界面或脚本执行。在执行命令时,会要求输入系统密码,以确认当前用户是具有管理员权限的用户。只有在输入正确的密码后,系统才会执行相应的命令并修改防火墙配置。 总而言之,设置防火墙端口在CentOS 7上一般需要输入系统密码,以确保操作的合法性和管理员权限。这是保障系统安全的重要一环。 ### 回答2: 在CentOS 7中,如果您以root用户身份运行命令来设置防火墙端口,则不需要再输入系统内密码。root用户具有超级用户权限,可以执行系统级别的操作。此外,CentOS 7还提供了sudo命令,允许其他用户以root权限运行特定命令,但需要输入其自己的密码。因此,如果您是以普通用户身份使用sudo命令来设置防火墙端口,则需要输入系统内普通用户的密码。 一般来说,使用CentOS 7设置防火墙端口的一般流程如下: 1. 用root用户登录系统或使用sudo命令切换到root权限。 2. 打开终端,并运行以下命令来安装必要的工具(如果尚未安装): ``` yum install firewalld ``` 3. 启动firewalld服务(如果尚未启动): ``` systemctl start firewalld ``` 4. 检查防火墙状态: ``` firewall-cmd --state ``` 5. 添加要打开的端口: ``` firewall-cmd --zone=public --add-port=端口号/协议 --permanent ``` 例如,要打开80端口的TCP流量,命令如下: ``` firewall-cmd --zone=public --add-port=80/tcp --permanent ``` 如果您设置了其他zone(如dmz、work、home等),请相应地更改--zone参数。 6. 重新加载防火墙规则以使更改生效: ``` firewall-cmd --reload ``` 此时,防火墙将允许通过选择的端口传输流量。 请注意,这仅适用于默认配置。如果有其它安全策略,如SELinux或其他额外的安全层,可能还需要进行其他配置并提供相关权限密码。 ### 回答3: 在CentOS 7中,设置防火墙端口时,通常不需要输入系统内密码。防火墙规则的配置分为两部分:添加规则到防火墙、保存规则到永久配置。 当通过命令行(如iptables)或使用Firewalld设置防火墙规则时,不需要输入系统密码。这是因为默认情况下,CentOS 7允许普通用户对防火墙规则进行修改。 然而,若通过system-config-firewall-tui等管理工具设置防火墙规则,可能需要在Ubuntu系统中输入管理员密码(如sudo密码)来进行授权。这是因为这些工具涉及到对系统配置文件的修改,需要管理员权限。 请注意,如果您未使用管理员权限运行这些命令或工具,则可能无法成功设置防火墙规则。因此,在执行任何更改或操作之前,请确保您具备适当的权限。 总之,在CentOS 7中,通过命令行或Firewalld设置防火墙端口规则时,通常不需要输入系统密码。但使用一些特定的图形界面工具进行设置可能需要输入管理员密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值