centos7防火墙设置

来源:互联网 发布:淘宝的每日好店 编辑:程序博客网 时间:2024/06/10 06:59

问题:
虚拟机通过NAT方式配置,虚拟机与主机的通信可以互相ping通,且可以访问互联网。在虚拟机上的centos安装完jdk、tomcat,并修改了server.xml配置文件里的IP地址

<Host name="192.168.168.168"  appBase="webapps"        unpackWARs="true" autoDeploy="true">

在主机的浏览器地址访问tomcat:提示访问失败

主机访问虚拟机上的tomcat服务器

原因:
尝试查看虚拟机上的防火墙运行状态

[root@localhost bin]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2017-12-08 17:56:07 CST; 1s ago
Docs: man:firewalld(1)
Main PID: 1029 (firewalld)
CGroup: /system.slice/firewalld.service
└─1029 /usr/bin/python -Es /usr/sbin/firewalld –nofork –nopi…
Dec 08 17:56:06 localhost.localdomain systemd[1]: Starting firewalld - dy…
Dec 08 17:56:07 localhost.localdomain systemd[1]: Started firewalld - dyn…
Dec 08 17:56:07 localhost.localdomain firewalld[1029]: WARNING: ICMP type…
Dec 08 17:56:08 localhost.localdomain firewalld[1029]: WARNING: beyond-sc…
Dec 08 17:56:08 localhost.localdomain firewalld[1029]: WARNING: ICMP type…
Dec 08 17:56:08 localhost.localdomain firewalld[1029]: WARNING: failed-po…
Dec 08 17:56:08 localhost.localdomain firewalld[1029]: WARNING: ICMP type…
Dec 08 17:56:08 localhost.localdomain firewalld[1029]: WARNING: reject-ro…
Hint: Some lines were ellipsized, use -l to show in full.

可以看到我们的防火墙处于开启的状态,将防火墙关闭。

[root@localhost bin]# systemctl stop firewalld.service

再次访问tomcat服务器:
这里写图片描述

此时可以访问到tomcat服务器的资源了。


结论:
以上访问tomcat服务器失败的主要原因就是防火墙的设置。用到了systemctl 。
systemctl 是管制服务的主要工具,它整合了chkconfig 与 service功能于一体,所以通用性更好一点。在操作系统上安装软件就会绑定到某些服务,只有开启了服务才可以使用软件的功能,在windows上安装了很多的服务。

这里写图片描述

所以在centos上的systemctl的主要功能就是管理服务的。

systemctl主要工具选项详解:

选项 作用 systemctl is-enabled servicename.service 查询服务是否开机启动 systemctl enable *.service 开机运行服务 systemctl disable *.service 取消开机运行 systemctl start *.service 启动服务 systemctl stop *.service 停止服务 systemctl restart *.service 重启服务 systemctl reload *.service 重新加载服务配置文件 systemctl status *.service 查询服务运行状态 systemctl –failed 显示启动失败的服务



样列:例如在CentOS 7 上安装http

[root@CentOS7 ~]# yum -y install httpd
启动服务(等同于service httpd start)
systemctl start httpd.service
停止服务(等同于service httpd stop)
systemctl stop httpd.service
重启服务(等同于service httpd restart)
systemctl restart httpd.service
查看服务是否运行(等同于service httpd status)
systemctl status httpd.service
开机自启动服务(等同于chkconfig httpd on)
systemctl enable httpd.service
开机时禁用服务(等同于chkconfig httpd on)
systemctl disable httpd.service
查看服务是否开机启动 (等同于chkconfig –list)


上面是使用到了systemctl工具,而在该问题主机浏览器访问tomcat服务器失败的主要原因就是防火墙的设置。我们也可以不用通过管制服务工具systemctl设置防火墙,可以使用自带的firewall-cmd 命令进行设置。

1、查看防火墙的运行状态

[root@localhost bin]# firewall-cmd --staterunning

2、查看已经开放的端口

[root@localhost bin]# firewall-cmd --list-ports8080/tcp

3、开启端口

firewall-cmd --zone=public --add-port=80/tcp --permanent  命令含义:–zone #作用域–add-port=80/tcp #添加端口,格式为:端口/通讯协议–permanent #永久生效,没有此参数重启后失效

4、关闭端口

    firewall-cmd --zone=public --remove-port=8080/tcp --permanent  

centos7以下的版本防火墙设置使用iptables:
Iptables服务相关命令

选项 作用 service iptables status 查看iptables状态 service iptables start 开启iptables service iptables stop 关闭iptables chkconfig iptables –list 查看iptables是否开机启动 chkconfig iptables on 设置iptables开机启动 chkconfig iptables off 设置iptables开机不启动



使用样列:

查看帮助
iptables -h
man iptables

列出iptables规则
iptables -L -n
列出iptables规则并显示规则编号
iptables -L -n –line-numbers

列出iptables nat表规则(默认是filter表)
iptables -L -n -t nat

清除默认规则(注意默认是filter表,如果对nat表操作要加-t nat)
清楚所有规则
iptables -F

重启iptables发现规则依然存在,因为没有保存
service iptables restart

保存配置
service iptables save

禁止ssh登陆(若果服务器在机房,一定要小心)
iptables -A INPUT -p tcp –dport 22 -j DROP
删除规则
iptables -D INPUT -p tcp –dport 22 -j DROP

-A, –append chain 追加到规则的最后一条
-D, –delete chain [rulenum] Delete rule rulenum (1 = first) from chain
-I, –insert chain [rulenum] Insert in chain as rulenum (default 1=first) 添加到规则的第一条
-p, –proto proto protocol: by number or name, eg. ‘tcp’,常用协议有tcp、udp、icmp、all
-j, –jump target 常见的行为有ACCEPT、DROP和REJECT三种,但一般不用REJECT,会带来安全隐患
注意:INPUT和DROP这样的关键字需要大写

禁止192.168.33.0网段从eth0网卡接入
iptables -A INPUT -p tcp -i eth0 -s 192.168.33.0 -j DROP
iptables -A INPUT -p tcp –dport 22 -i eth0 -s 192.168.33.61 -j ACCEPT

禁止ip地址非192.168.10.10的所有类型数据接入
iptables -A INPUT ! -s 192.168.10.10 -j DROP

禁止ip地址非192.168.10.10的ping请求
iptables -I INPUT -p icmp –icmp-type 8 -s 192.168.50.100 -j DROP

扩展匹配:1.隐式扩展 2.显示扩展
隐式扩展
-p tcp
–sport PORT 源端口
–dport PORT 目标端口
显示扩展:使用额外的匹配规则
-m EXTENSTION –SUB-OPT
-p tcp –dport 22 与 -p tcp -m tcp –dport 22功能相同
state:状态扩展,接口ip_contrack追踪会话状态
NEW:新的连接请求
ESTABLISHED:已建立的连接请求
INVALID:非法连接
RELATED:相关联的连接

匹配端口范围
iptables -I INPUT -p tcp –dport 22:80 -j DROP

匹配多个端口
iptables -I INPUT -p tcp -m multiport –dport 22,80,3306 -j ACCEPT

不允许源端口为80的数据流出
iptables -I OUTPUT -p tcp –sport 80 -j DROP