CentOS7系统的网络防护

CentOS7系统的网络防护

1、Linux运行级别

系统在运行过程中提供的功能/资源组合方式-不同级别,提供的服务不一样
主要运行级别

  • rescue.target
    //救援模式,需要修复系统时用
  • multi-user.target
    //多用户模式,无桌面
  • graphical.target
    //图形模式

1)检查任务数量

  • 找出某个名称的进程及ID信息:pgrep -l 关键词
[root@centos7 ~]# pgrep -l httpd	//列出进程名包含httpd的任务
114655 httpd
114657 httpd
114658 httpd			//httpd会启用多个进程
114659 httpd
114660 httpd
114661 httpd
[root@centos7 ~]# 
  • 统计当前系统中所有进程的数量
[root@centos7 ~]# pgrep -c .		//选项-c表示统计,'.'表示匹配任意字符
238
[root@centos7 ~]#

2)切换级别以节省系统资源

多数情况下,服务器并不需要运行图形模式

  • 只要切换为多用户模式就可以减少30%~40%基础资源消耗
    切换格式:systemctl isolate 系统级别
systemctl isolate multi-user.target		//切换至多用户模式
systemctl isolate graphical.target		//切换至图形模式
systemctl get-default 					//查看默认级别
systemctl set-default multi-user.target //设置默认运行级别

执行切换操作,改为多用户模式运行:

[root@centos7 ~]# systemctl isolate multi-user.target 
[root@centos7 ~]#

切换完成后,重新以root用户登入系统(如果是SSH远程登录的话,一般就不用重新登录),再次检查当前运行的任务数量(会大幅减少):

[root@centos7 ~]# pgrep -c .
160
[root@centos7 ~]#

3)将虚拟机的默认运行级别设置为多用户模式

[root@centos7 ~]# systemctl isolate multi-user.target 	//设置运行级别为多用户级别,不影响默认级别
[root@centos7 ~]# systemctl get-default 		//查看当前默认级别
graphical.target	//发现默认级别为图像模式
[root@centos7 ~]#
[root@centos7 ~]# systemctl set-default multi-user.target 		//设置默认级别为多用户模式
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
[root@centos7 ~]# 
[root@centos7 ~]# systemctl get-default 	//再次查看默认级别
multi-user.target	//此时默认级别已被修改为多用户模式
[root@centos7 ~]#

2、SELinux防护

Security Enhanced Linux,安全增强型Linux系统源于美国国家安全局(NSA)强制防护控制安全策略一主要针对Linux系统中的文件、进程等提供策略保护
用户只分配 “需要” 的最小权限
进程只访问 “需要” 的资源
网络服务只能开启 “需要” 的端口

2.1 SELinux的三种运行状态

  • Enforce,强制(严格按型号策略执行保护)
  • Permissive,宽松(若有违规会记录,但不做限制)
  • Disable,禁用(内核不加载SELinux)

2.2 检查当前SELinux运行状态

[root@centos7 ~]# getenforce 
Enforcing
[root@centos7 ~]#

2.3 在 “强制” 与 “宽松” 模式之间切换,重启后不再生效

[root@centos7 ~]# setenforce 0	//切换为宽松模式
[root@centos7 ~]#getenforce		//确认结果
Permissive

[root@centos7 ~]# setenforce 1	//切换为强制模式
[root@centos7 ~]# getenforce		//确认结果
Enforcing

2.4 永久配置SELinux运行状态

[root@centos7 ~]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive			此行决定每次开机后的SELinux状态
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

3、SELinux对web目录的保护

1)先创建网页目录及文件/webdir1/index.html,内容为 sss666

[root@centos7 ~]# mkdir /webdir1
[root@centos7 ~]# vim /webdir1/index.html
  <h1>sss666</h1>
  
[root@centos7 ~]# ls -dZ /webdir1/		//检查目录的SELinux属性
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /webdir1/
[root@centos7 ~]#

2)然后将/webdir1目录mv到/var/www/html/目录下

[root@centos7 ~]# mv /webdir1/ /var/www/html/
[root@centos7 ~]# ls -dZ /var/www/html/webdir1/		//再次检查/var/www/html/webdir1/目录的SELinux属性
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /var/www/html/webdir1/
[root@centos7 ~]#

注意:如果直接在/var/www/html/目录下创建新目录,默认会继承网页目录/var/www/html的SELinux属性;但是如果从其他地方mv目录过来,SELinux属性不会自动变更。

3)确保可访问 http://虚拟机IP地址/webdir1/

从浏览器访问刚刚部署的/var/www/html/webdir1目录,会提示被拒绝。
在这里插入图片描述
这是因为SELinux安全机制阻止了对这个目录/var/www/html/webdir1/的访问,但是访问原来的http://虚拟机IP地址/ 还是不受影响的。
在这里插入图片描述
要解决访问移入目录 /var/www/html/webdir1/ 的问题,要么禁用SELinux机制,要么调整此目录的SELinux安全属性。
① :禁用SELinux机制

[root@centos7 ~]# getenforce 
Enforcing
[root@centos7 ~]# setenforce 0
[root@centos7 ~]# getenforce 
Permissive
[root@centos7 ~]#

访问成功
在这里插入图片描述
② :调整此目录的SELinux安全属性

[root@centos7 ~]# setenforce 1		//先将SELinux机制调回来
[root@centos7 ~]# getenforce 
Enforcing
[root@centos7 ~]#

在这里插入图片描述

[root@centos7 ~]# chcon -R /var/www/html/webdir1/ --reference=/var/www		//参照模板目录修改SELinux属性
[root@centos7 ~]# 
[root@centos7 ~]# ls -dZ /var/www/html/webdir1/		//确认修改结果
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/webdir1/
[root@centos7 ~]#

再次访问成功

在这里插入图片描述

4、SELinux对web端口的保护

1)配置httpd服务监听82端口

添加一个配置文件,使httpd服务监听82端口:

[root@centos7 ~]# vim /etc/httpd/conf.d/port82.conf
	Listen 82

[root@centos7 ~]# httpd -t		//检查语法,确认没有错误
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::f17c:c729:6d95:d760. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@centos7 ~]#

尝试重启httpd服务时,会提示失败:

[root@centos7 ~]# systemctl restart httpd
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.
[root@centos7 ~]#

这是因为SELinux默认只允许Web服务使用80、81等少数几个端口,通过以下命令可以查看:

[root@centos7 ~]# semanage port -l | grep http_port_t
http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
[root@centos7 ~]# 

2)确保可访问 http://虚拟机IP地址:82/

要解决Web端口限制的问题,要么禁用SELinux机制,要么调整SELinux的端口保护策略,添加想开放的端口。如果采用后一种方法,可以参考下列操作。
根据重启httpd服务失败时的提示,执行journalctl -xe命令:

[root@centos7 ~]# journalctl -xe
......
 *****  Plugin bind_ports (99.5 confidence) suggests   ************************
                                         
		If you want to allow /usr/sbin/httpd to bind to network port 82
		Then you need to modify the port type.
		Do
		# semanage port -a -t PORT_TYPE -p tcp 82
		where PORT_TYPE is one of the following: http_cache_port_t, http_port_t, j
[root@centos7 ~]# 

根据上述提示信息,获得命令结果,执行下列操作:

[root@centos7 ~]# semanage port -a -t http_port_t -p tcp 82		//允许Web网站使用82端口

[root@centos7 ~]# semanage port -l | grep http_port_t		//确认设置结果
http_port_t                    tcp      82, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
[root@centos7 ~]#

然后再次重启httpd服务,即可正常使用。

[root@centos7 ~]# systemctl restart httpd
[root@centos7 ~]#

从浏览器访问http://虚拟机IP地址:82/,也可以成功访问.
在这里插入图片描述

5、Firewalld网络防护

1)启用防火墙

在这里插入图片描述

2)打开firewall-config配置工具

需要切换到图形模式,并以root用户登录。

[root@centos7 ~]# firewall-config 
......

即可打开图形化的防火墙配置工具:在这里插入图片描述

3)确认默认安全区为public

如上图所示,如果没有修改过,默认安全区应该就是public(在区域部分加粗显示)。

如果不是,也可以通过菜单“选项”-“改变默认区”来进行更改,选择public即可。
在这里插入图片描述

4)设置策略允许访问httpd服务

在“配置”处选择“永久”,在“区域”处选择“public”,在“服务”处勾选“httpd”即可。
在这里插入图片描述

5)设置策略允许访问本机的 tcp/82端口

在“配置”处选择“永久”,在“区域”处选择“public”,在“端口”处单击“添加”,弹出窗口后根据提示填写即可。
在这里插入图片描述
在这里插入图片描述

6)重载防火墙

在这里插入图片描述

7)验证防火墙保护效果

防火墙允许时正常访问Web
从同网络内的另外一台主机(注意不要从Web服务器本机访问,因为本机访问自己不需要经过防火墙,这里我用了真机的浏览器)访问http://虚拟机IP地址/,可以成功访问。
在这里插入图片描述

8)如果不开放“http”,默认public安全区将拒绝访问Web

调整防火墙策略,取消对“http”的勾选,并重载防护墙。

再次从其他主机访问http://虚拟机IP地址/,将会被拒绝。
在这里插入图片描述

9)停止防火墙服务(不保护)后,也可以正常访问Web服务

如果将firewalld服务停止:

[root@centos7 ~]# systemctl stop firewalld.service 
[root@centos7 ~]#

从其他主机也可以正常访问此主机的Web服务。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Sakura0156/article/details/110235449