本文是针对CentOS 7的防火墙的配置,7版本之后与之前的版本在防火墙配置上有些不同,主要是因为没有使用默认的iptables 做端口的限制。后面我们也捎带讲解一下通过iptables的方式进行防火墙的配置的方式。
1、查看已经开放的端口列表:
firewall-cmd --list-ports
2、开启对应端口,比如开放8080端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent
命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
3、想要配置生效,需要重启防火墙
firewall-cmd --reload #重启firewall systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动
systemctl status firewalld.service #查看firewall状态
以下是CentOS 7 之前版本 iptables 配置命令,7版本之前都是通过iptables方式配置防火墙
1、如要开放80,22,8080 端口,输入以下命令即可
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
2、然后保存:
/etc/rc.d/init.d/iptables save
3、查看打开的端口:
/etc/init.d/iptables status
4、关闭防火墙
1) 永久性生效,重启后不会复原
开启: chkconfig iptables on
关闭: chkconfig iptables off
2) 即时生效,重启后复原
开启: service iptables start
关闭: service iptables stop
其他内容:
1 默认是没有安装iptables,以下通过安装iptables来进行管理
[root@centos ~]# yum install iptables-services
设置防火墙规则 (增加80端口)
[root@centos ~]# vim /etc/sysconfig/iptables
打开文件,增加80端口那一行
*filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
:wq #保存退出
2 、CentOS 7以后默认firewall作为防火墙的设置
#禁止firewall开机启动 [root@centos ~]# systemctl disable firewalld.service #设置防火墙开机启动 [root@centos ~]# systemctl enable iptables.service #停止firewall [root@centos ~]# systemctl stop firewalld.service #重启防火墙使配置生效 [root@centos ~]# systemctl restart iptables.service (必须重启)
3、查看当前系统打开的端口
[root@centos ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2157/master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1292/sshd tcp6 0 0 ::1:25 :::* LISTEN 2157/master tcp6 0 0 :::22 :::* LISTEN 1292/sshd
总结一下,其实学习Linux不是很难的,只要你愿意用心去学习并且能够坚持。万事开头难,你将系统系统环境搭建起来了,照着书去敲打命令即可,不会的可以去查阅资料或者找度娘。我也是习惯了Windows的操作环境了,因为一直手比较懒给自己找了很多的理由,等到工作中你会看到你平时的积累都不会白费的。