
[root@server ~]# ls -Z #-Z:输出每个文件的所有安全上下文信息
unconfined_u:object_r:admin_home_t:s0 公共
unconfined_u:object_r:admin_home_t:s0 模板
unconfined_u:object_r:admin_home_t:s0 视频
unconfined_u:object_r:admin_home_t:s0 图片
unconfined_u:object_r:admin_home_t:s0 文档
unconfined_u:object_r:admin_home_t:s0 下载
unconfined_u:object_r:admin_home_t:s0 音乐
unconfined_u:object_r:admin_home_t:s0 桌面
system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
安全上下文用3个冒号分为4个字段
身份标识(identify):相当于账号的身份标识,有三种类型:
root:安全上下文的身份是root(用户编号为0),默认会映射为unconfined_u,可以通过以下命令查看映射关系:
[root@server ~]# semanage login -l

system_u:系统用户身份(用户id1-999),其中u代表user
普通用户名_u:普通用户身份(用户id1000-65535),用户数据的user字段是user_u
角色(role):表示此数据是进程还是文件或目录包含(了解就行)
类型(type):
灵敏度:用 s0、s1、s2 来命名,数字为灵敏度分级,数值越大,灵敏度越高,管的越多
作用:查询身份,角色等信息,需要安装才能使用
[root@server ~]# yum install setools-console -y
格式:
[root@server ~]# seinfo -参数
参数:
-u: 列出SELinux中所有的身份(user);
-r: 列出SELinux中所有的角色(role);
-t: 列出SELinux中所有的类型(type);
-b: 列出所有的布尔值(也就是策略中的具体规则名称);
-x: 显示更多的信息;
[root@server ~]# seinfo -u
Users: 8
guest_u
root
staff_u
sysadm_u
system_u
unconfined_u
user_u
xguest_u
[root@server ~]# seinfo -r
Roles: 14
auditadm_r
dbadm_r
guest_r
logadm_r
nx_server_r
object_r
secadm_r
staff_r
sysadm_r
system_r
unconfined_r
user_r
webadm_r
xguest_r
[root@server ~]# seinfo -t | wc -l
5051
[root@server ~]# yum install httpd -y
[root@server ~]# ls -Zd /var/www/html #-d当遇到目录时列出目录本身而非目录内的文件
system_u:object_r:httpd_sys_content_t:s0 /var/www/html

[root@server ~]# getenforce
Enforcing
[root@server ~]# setenforce 1 #临时开启
[root@server ~]# getenforce
Enforcing
[root@server ~]# setenforce 0 #临时关闭进入宽容模式
[root@server ~]# getenforce
Permissive
selinux的配置文件在 /etc/selinux/config
[root@server ~]# vim /etc/selinux/config
SELINUX=enforcing 或 enabled
#注:如果将配置文件修改为enabled,则重启后的状态为Permissive
selinux的配置文件在 /etc/selinux/config
[root@server ~]# vim /etc/selinux/config
SELINUX=disabled
[root@server ~]# sestatus
[root@server ~]# sestatus
SELinux status: enabled # 是否启用
SELinuxfs mount: /sys/fs/selinux # selinux临时文件系统的挂载点
SELinux root directory: /etc/selinux # 启动目录,配置文件目录位置
Loaded policy name: targeted # 当前加载的策略类型
# 策略类型
# targeted:只保护目标进行,默认
# minimum:少数选定进程进行保护
# mls:多级安全保护,最高级
Current mode: enforcing # 执行的模式,重要
Mode from config file: enforcing # 配置文件的模式
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33

[root@server ~]# vim /etc/selinux/config
SELINUX=enforcing # 设置模式
SELINUXTYPE=targeted # 设置策略类型
# 方法1
[root@server ~]# chcon [-R] [-t type] [-u user] [-r role] 文件名
-R:递归修改,当前目录及目录下的所有文件都同时设置
-t:后面接安全上下文件的类型字段(重要)
-u:后面接身份标识
-r:后面接角色
-v:显示变动结果
# 方法2:会把范例文件的4个字段全部进行参照,即全部修改
[root@server ~]# chcon -R --reference=范例文件 文件名
[root@server ~]# touch test
[root@server ~]# ls
公共 模板 视频 图片 文档 下载 音乐 桌面 anaconda-ks.cfg test
[root@server ~]# ls -Z test
unconfined_u:object_r:admin_home_t:s0 test
[root@server ~]# ls -Z /etc/hosts
system_u:object_r:net_conf_t:s0 /etc/hosts
[root@server ~]# chcon -v -t net_conf_t /root/test
正在更改 '/root/test' 的安全上下文
[root@server ~]# ls -Z /root/test
unconfined_u:object_r:net_conf_t:s0 /root/test
[root@server ~]# touch temp
[root@server ~]# ls
公共 模板 视频 图片 文档 下载 音乐 桌面 anaconda-ks.cfg temp test
[root@server ~]# ls -Z temp
unconfined_u:object_r:admin_home_t:s0 temp
[root@server ~]# ls -Z /etc/passwd
system_u:object_r:passwd_file_t:s0 /etc/passwd
[root@server ~]# chcon -v --reference=/etc/passwd ~/temp
正在更改 '/root/temp' 的安全上下文
[root@server ~]# ls -Z temp
system_u:object_r:passwd_file_t:s0 temp
# 恢复快照
# 开启selinux
# 查看状态
[root@server ~]# getenforce
Enforcing
[root@server ~]# yum install httpd -y
[root@server ~]# mkdir /web1
# 使用mobaxterm将zy网页文件上传
[root@server ~]# systemctl start httpd
[root@server ~]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/web1"
<Directory "/web1">
[root@server ~]# systemctl restart httpd
# 测试:Windows中浏览器中输入server主机的IP地址,只会看到apache的欢迎页,说明selinux对/web1的安全上下文检测未通过
[root@server ~]# ls -Zd /var/www/html # 查看默认网页目录的安全上下文
system_u:object_r:httpd_sys_content_t:s0 /var/www/html
[root@server ~]# ls -Zd /web1
unconfined_u:object_r:default_t:s0 /web1
# 将/web1的类型修改为默认网页目录的类型
[root@server ~]# chcon -Rv -t httpd_sys_content_t /web1
[root@server ~]# ls -Zd /web1
unconfined_u:object_r:httpd_sys_content_t:s0 /web1
[root@server ~]# systemctl restart httpd
# 测试:此时输入IP地址则可以浏览
[root@server ~]# restorecon [-Rv] 文件或目录
-R:连同子目录一起修改;
-v:将过程显示到屏幕上
[root@server ~]# ls -Zd /web1
unconfined_u:object_r:httpd_sys_content_t:s0 /web1
[root@server ~]# restorecon -Rv /web1
[root@server ~]# ls -Zd /web1
unconfined_u:object_r:default_t:s0 /web1
[root@server ~]# semanage 选项 参数 文件
选项:
login
user
port
interface
fcontext #注意:fcontext查询默认安全上下文(重要)
translation
boolean
参数:
-l :查询;
-a :添加
-m :修改
-d :删除
-D :全部删除
-t :类型
-r :角色
-s :用户
-f :文件
文件:
文件或目录
[root@server ~]# semanage fcontext -l | grep 文件名
[root@server ~]# semanage fcontext -l | grep /etc/passwd
/etc/passwd[-\+]? regular file system_u:object_r:passwd_file_t:s0
/etc/passwd\.OLD regular file system_u:object_r:passwd_file_t:s0
/etc/passwd\.adjunct.* regular file system_u:object_r:passwd_file_t:s0
/etc/passwd\.lock regular file system_u:object_r:passwd_file_t:s0
[root@server ~]# semanage port -l | grep 协议
[root@server ~]# semanage port -l | grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
[root@server ~]# semanage port -l | grep ssh
ssh_port_t tcp 22
#先把/web1的安全上下文类型修改为httpd_sys_content_t
[root@server ~]# ls -Zd /web1
unconfined_u:object_r:default_t:s0 /web1
[root@server ~]# ls -Zd /var/www/html
system_u:object_r:httpd_sys_content_t:s0 /var/www/html
[root@server ~]# chcon -R -t httpd_sys_content_t /web1
[root@server ~]# ls -Zd /web1
unconfined_u:object_r:httpd_sys_content_t:s0 /web1
[root@server ~]# vim /etc/httpd/conf/httpd.conf
Listen 7777 # 修改80端口为7777
[root@server ~]# systemctl restart httpd # 报错
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xeu httpd.service" for details.
[root@server ~]# semanage port -l | grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000 # 7777端口未在放行列表中
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
[root@server ~]# systemctl status httpd.service # 查看日志
[root@server ~]# semanage port -a -t http_port_t -p tcp 7777 # 添加新端口
[root@server ~]# semanage port -l | grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 7777, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
[root@server ~]# systemctl restart httpd # 重启服务成功
[root@server ~]#
# 测试:192.168.48.130:7777

[root@server ~]# semanage port -l | grep ssh
ssh_port_t tcp 22
[root@server ~]# vim /etc/ssh/sshd_config
Port 2222 # 去掉#,修改端口号
[root@server ~]# systemctl restart sshd # 重启失败
[root@server ~]# semanage port -a -t ssh_port_t -p tcp 2222 # 添加新端口
[root@server ~]# semanage port -l | grep ssh
ssh_port_t tcp 2222, 22
[root@server ~]# systemctl restart sshd # 重启成功
# 新建会话输入以下内容测试
PS C:\Users\13289> ssh root@192.168.80.130 -p 2222
或
PS C:\Users\13289> ssh -p 2222 root@192.168.80.130
#测试成功