关于Linux CentOS 7防火墙问题总结

写在前面,方便使用

1
2
# 停止,并永久禁用
systemctl stop firewalld && systemctl disable firewalld

iptables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看防火墙状态
service iptables status

# 停止防火墙
service iptables stop

# 启动防火墙
service iptables start

# 重启防火墙
service iptables restart

# 永久关闭防火墙
chkconfig iptables off

# 开机自启动
chkconfig iptables on
1
2
3
4
5
* 关于-P -L -F
-F 要小心,搞不好,你就马上同服务器断开连接了。
情况1:可以iptables -F清除所有规则来暂时停止防火墙: (警告:这只适合在没有配置防火墙的环境中,如果已经配置过默认规则为deny的环境,此步骤将使系统的所有网络访问中断)
情况2:先执行iptables -P INPUT ACCEPT,然后执行 iptables -F ,通过执行 iptables -L ,看到信息Chain INPUT (policy DROP 0 packets, 0 bytes) (注意 是DROP),所以肯定立马断开连接
情况3:先执行iptables -P INPUT ACCEPT,再次通过iptables -L看信息的话就是Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) (注意 是ACCEPT),所以现在是可以安全使用了iptables -F

开启80端口

1
2
3
4
5
6
7
vi /etc/sysconfig/iptables

#加入如下代码
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

#保存退出后重启防火墙
service iptables restart

firewall

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1、查看firewall服务状态
service firewalld status
* 出现Active: active (running)切高亮显示则表示是启动状态。
* 出现 Active: inactive (dead)灰色表示停止,看单词也行。

2、查看firewall的状态
firewall-cmd --state
* running 运行中
* not running 已停止

3、对firewalld.service服务进行操作
# 开启
service firewalld start
# 停止
service firewalld stop
# 重启
service firewalld restart
# 永久禁用
service firewalld disable
# 开机自启动
service firewalld enable

4、查看防火墙规则
firewall-cmd --list-all

5、查询、开放、关闭端口
# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放端口80
firewall-cmd --permanent --add-port=80/tcp
# 移除端口8080
firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙),同service firewalld restart
firewall-cmd --reload

# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具
2、--permanent:永久生效,没有此参数重启后失效
3、--add-port:标识添加的端口

测试Linux端口

可以使用Windows的telnet命令。
telnet 192.168.1.1 22
如果telnet命令不能使用,参考telnet命令使用