防火墙linux关闭端口命令行
-
在Linux中关闭防火墙中的端口可以通过命令行操作完成。下面是一种常用的方法:
1. 查看当前防火墙规则
使用以下命令查看当前防火墙的规则:
“`shell
sudo iptables -L
“`2. 确定要关闭的端口
在当前防火墙规则列表中找到要关闭的端口,并记下对应的规则编号。3. 关闭端口
使用以下命令关闭指定的端口:
“`shell
sudo iptables -D INPUT <规则编号>
“`
其中,`<规则编号>`是要关闭的端口对应的规则编号。4. 保存修改
为了确保关闭的端口在系统重启后仍然保持关闭状态,需要保存修改的防火墙规则。可以使用以下命令保存规则:
“`shell
sudo service iptables save
“`
或
“`shell
sudo iptables-save > /etc/sysconfig/iptables
“`需要注意的是,以上方法适用于使用iptables防火墙的系统,比如CentOS、Ubuntu等。如果使用的是其他防火墙工具,如ufw或firewalld,需要使用相应的命令进行操作。
另外,关闭防火墙中的端口会增加系统的安全风险,建议在需要关闭端口的情况下谨慎操作,并确保已经采取了其他安全措施来保护系统。
2年前 -
在Linux系统中,可以使用命令行关闭防火墙上的端口。以下是关闭端口的几种常用方法:
1. 使用iptables命令:iptables是Linux上用于配置防火墙的工具之一。可以使用以下命令关闭指定端口:
“`
sudo iptables -A INPUT -p tcp –dport-j DROP
“`
其中,``是要关闭的端口号。执行以上命令后,所有进入该端口的TCP连接都会被拒绝。 2. 使用ufw命令:ufw是Ubuntu上较为常用的防火墙配置工具。可以使用以下命令关闭指定端口:
“`
sudo ufw deny“`
其中,``是要关闭的端口号。执行以上命令后,该端口的所有连接都会被拒绝。 3. 使用firewalld命令:firewalld是在CentOS/RHEL系统上常用的防火墙配置工具。使用以下命令关闭指定端口:
“`
sudo firewall-cmd –permanent –zone=public –remove-port=/tcp
sudo firewall-cmd –reload
“`
其中,``是要关闭的端口号。执行以上命令后,该端口将从防火墙规则中移除,并且防火墙规则被重新加载。 4. 编辑防火墙配置文件:在某些情况下,也可以直接编辑防火墙配置文件来关闭端口。编辑以下文件:
– iptables:/etc/sysconfig/iptables
– ufw:/etc/ufw/before.rules
– firewalld:/etc/firewalld/zone/public.xml
将相关端口的规则删除或注释掉,然后保存文件并重新加载防火墙配置。5. 禁用防火墙:如果你想完全关闭防火墙,可以使用以下命令:
– iptables:sudo systemctl stop iptables
– ufw:sudo ufw disable
– firewalld:sudo systemctl stop firewalld请注意,以上命令需要以root或具有sudo权限的用户身份执行。关闭防火墙上的端口可能会对系统安全造成风险,请谨慎操作。如果不确定自己在做什么,请在关闭端口之前备份配置文件以便恢复。
2年前 -
要在Linux上关闭防火墙端口,你可以使用iptables命令或firewalld命令来实现。下面分别介绍了这两种方法的具体操作流程。
方法一:使用iptables命令关闭防火墙端口
Step 1:使用以下命令检查系统上正在运行的防火墙:
“`
sudo systemctl status iptables
“`如果防火墙已启用,你将看到类似于下面的输出:
“`
iptables.service – IPv4 iptables firewall
Loaded: loaded (/lib/systemd/system/iptables.service; enabled; vendor preset: enabled)
Active: active (exited) since Mon 2019-12-23 20:39:03 UTC; 3 days ago
Main PID: 894 (code=exited, status=0/SUCCESS)
“`Step 2:使用以下命令列出当前的iptables规则:
“`
sudo iptables -L
“`你将看到类似于下面的输出:
“`
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp — anywhere anywhere tcp dpt:sshChain FORWARD (policy ACCEPT)
target prot opt source destinationChain OUTPUT (policy ACCEPT)
target prot opt source destination
“`上面的规则表示当前允许 SSH 连接。如果你想关闭SSH端口(例如22),你可以使用以下命令:
“`
sudo iptables -A INPUT -p tcp –dport 22 -j DROP
“`这将向 INPUT 链添加一个规则,将来自22端口的TCP连接丢弃。
Step 3:使用以下命令确认规则已经添加:
“`
sudo iptables -L
“`你会看到类似于下面的输出:
“`
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp — anywhere anywhere tcp dpt:sshChain FORWARD (policy ACCEPT)
target prot opt source destinationChain OUTPUT (policy ACCEPT)
target prot opt source destination
“`现在SSH端口已关闭。
方法二:使用firewalld命令关闭防火墙端口
Step 1:使用以下命令检查系统上正在运行的防火墙:
“`
sudo systemctl status firewalld
“`如果防火墙已启用,你将看到类似于下面的输出:
“`
firewalld.service – firewalld – dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-12-23 20:39:03 UTC; 3 days ago
“`Step 2:使用以下命令列出当前的防火墙规则:
“`
sudo firewall-cmd –list-all
“`你将看到当前的防火墙配置,例如:
“`
public (default, active)
interfaces: eth0
sources:
services: ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
“`上面的规则表示当前允许 SSH 连接。如果你想关闭SSH端口(例如22),你可以使用以下命令:
“`
sudo firewall-cmd –zone=public –remove-service=ssh –permanent
“`这会从 public 区域中的永久规则列表中删除SSH服务规则。
Step 3:使用以下命令重新加载防火墙设置,使更改生效:
“`
sudo firewall-cmd –reload
“`现在SSH端口已关闭。
无论你选择使用iptables还是firewalld,关闭防火墙端口后都应该进行测试,确保端口已被正确关闭。
2年前