Centos7不关闭selinux和firewall,如何访问Apache
在VM虚拟机上刚安装的Centos7,默认是开启selinux和firewall。
查询当前selinux和firewall是否开启:
[root@localhost ~]# getenforce
Enforcing
显示为Enforcing时,表示selinux时开启的,Permissive,表示selinux不拦截,Disabled,表示selinux关闭[root@localhost ~]# setenforce 0|1 设置为0时显示Permissive,设置为1时显示Enforing
[root@localhost ~]# systemctl status firewalld
显示的是disabled时,表示关闭
[root@localhost ~]# systemctl start firewalld
显示的是active时,表示开启
[root@localhost ~]# 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 四 2022-06-09 00:10:56 CST; 5 months 13 days ago
Docs: man:firewalld(1)
Main PID: 624 (firewalld)
CGroup: /system.slice/firewalld.service
└─624 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
[root@localhost ~]# systemctl enable firewalld 设置开机自启
yum安装Apache:
[root@localhost ~]# yum -y install httpd
开启Apache服务,并设置开机自启:
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
创建测试文件,用于测试是否可以正常访问:
[root@localhost ~]# echo "test Apache hello" > /var/www/html/index.html
在浏览器输入IP地址,访问Apache:
如图:
firewall设置
firewall查看当前开放的服务和端口:
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
如上显示,常用服务仅放开了ssh
firewall-cmd --add-service添加服务
firewall-cmd --add-service=服务名 --permanent
不添加--permanent,表示临时添加该服务,即时生效,服务器重启后失效
[root@localhost ~]# firewall-cmd --add-service=http --permanent
[root@localhost ~]# firewall-cmd --add-service=https --permanent
firewall-cmd --add-port添加端口
firewall-cmd --add-port=端口号/协议
[root@localhost ~]# firewall-cmd --add-port=80/tcp --permanent
[root@localhost ~]# firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --reload 重新载入规则
设置完相关规则后,记得调用该命令,否则添加的服务跟端口不生效
[root@localhost ~]# firewall-cmd --reload
由于/var/www/html/文件夹存在httpd的selinux权限,所以在该文件夹中创建文件时,已经默认添加selinux关于httpd的权限,此时在浏览器中是可以访问之前设置的网页内容:
SELinux设置
为了模拟selinux设置的情况下,无法访问网页,我们在家目录创建index.html文件,并移动到/var/www/html/文件夹:
[root@localhost ~]# echo "test Apache2 12312312" > index2.html
[root@localhost ~]# mv index2.html /var/www/html/
之前在/var/www/html/文件夹已经创建了index.html,我们通过ls -Z命令查询selinux权限,对比下两个文件的区别:
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index2.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
浏览器输入ip/index2.html,跟上图对比下区别:
此时可以看到,没有权限访问index2.html。
httpd_sys_content_t 这个类型针对静态web内容,比如静态网站使用的.html
文件,标记为这种类型的文件,httpd
可以读取访问,并且可以执行脚本。 默认情况下,标有此类型的文件和目录不能被httpd
或其他进程写入或修改。
1.临时修改httpd_sys_content_t
chcon命令修改文件和目录的类型上下文,-R参数是递归全部子目录和文件,-t参数是添加一个类型
[root@localhost html]# chcon -t httpd_sys_content_t index2.html
[root@localhost html]# chcon -t httpd_sys_content_t index2.html
[root@localhost html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index2.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
2. restorecon作用是把文件的上下文恢复成默认的上下文,/var/www/html/文件夹下,默认上下文为httpd_syscontent_t,所以restorecon命令,可以将该文件夹下的文件或目录,恢复为httpd_syscontent_t
[root@localhost html]# restorecon index2.html
restorecon命令的 -R参数是递归全部子目录和文件 -v参数是显示修改的标记
修改后,通过浏览器重新访问ip/index2.html,此时可以正常访问