linux教程从0到精通,SELinux 从入门到精通教程

一、介绍

1.官方意思

SELinux是一种基于 域-类型 模型(domain-type)的强制访问控制(MAC)安全系统,它由NSA编写并设计成内核模块包含到内核中,相应的某些安全相关的应用也被打了SELinux的补丁,最后还有一个相应的安全策略。任何程序对其资源享有完全的控制权。假设某个程序打算把含有潜在重要信息的文件扔到/tmp目录下,那么在DAC情况下没人能阻止他。SELinux提供了比传统的UNIX权限更好的访问控制。

2.简单意思

selinux就像是一种系统的保护机制,如果关闭它的话,也没什么问题,操作上不会因为selinux而感到阻碍和困难,但是你开启之后,你会感觉做什么都是错的,寸步难行。举个乐子:”当你打开了HTTP服务,内容能正常访问,但是你开启selinux之后就报错,那你会觉得是selinux的问题,然后你就设置好这个内容的selinux的上下文(类似用户权限),又可以正常访问了,然后你开开心心地将一个项目代码上传到网页根目录,不能打开了,你会想到selinux ~~ 首页能打开,但是其他jpg、gif等等不能正常显示,你又改权限,噼里啪啦地一顿操作猛如虎,都可以了,但是跳转到别人的网站又不行了,emmm ~~ 直接重装系统“

3.有点意思

selinux毕竟是保护机制,所以肯定能起到保护作用,保护着系统不被任意修改,保护着站点的权限又严格访问限制,你的文件都是等于数据,数据是钱买不了的,想好好地存在于互联网,就要做相对的安全(没有绝对的安全),通过各种手段保护你的数据不被修改等等,安全意识是非常重要的。

4.没什么意思

selinux是非常好的一种保护机制,但在企业中,大多数是没有使用这个安全机制的,毕竟都是直接关闭的,比如云服务器,你买的时候就已经默认是关闭selinux这个保护机制了。所以在这里演示,后期的软件服务和部署都不会开启。

二、Selinux入门--基本设置

1.关于selinux

selinux在系统的分布

[root@linuxidc ~]# find / -name selinux

/sys/fs/selinux

/etc/sysconfig/selinux

/etc/selinux

/usr/lib64/Python2.7/site-packages/selinux

/usr/share/selinux

/usr/libexec/selinux

[root@linuxidc ~]#

2.分析

selinux目录组成:

/sys/:系统全局设备管理目录,比如cgroup、pstore、selinux、xfs都存在这个目录。

/etc/:服务的配置文件目录。

/usr/:存放二进制文件、共享文件、函数库文件、服务执行文件的目录。

3.命令式宽容模式或严格模式(以下设置全局有效)

查看当前selinux的状态

[root@linuxidc ~]# getenforce

Enforcing

[root@linuxidc ~]#

设置宽容模式

[root@linuxidc ~]# setenforce 0

[root@linuxidc ~]# getenforce

Permissive

[root@linuxidc ~]#

设置拒绝模式

[root@linuxidc ~]# setenforce 1

[root@linuxidc ~]# getenforce

Enforcing

[root@linuxidc ~]#

4.配置文件说明(可以设置关闭、宽容、严格模式)

/etc/sysconfig/selinux 是 /etc/selinux/config 文件的软链接

所以修改这两个其中一个,文件内容都会改变。

5.配置文件

[root@linuxidc ~]# cat /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=enforcing #设置selinux的状态模式

#SELINUXTYPE= can take one of three two values:

#targeted - Targeted processes are protected, #目标模式

#minimum - Modification of targeted policy. Only selected processes are protected. #最小化权限控制

#mls - Multi Level Security protection. #多种selinux模式,根据文件的上下文设置而改变访问权限

SELINUXTYPE=targeted #设置selinux的类型,默认目标模式

6.更新配置文件

[root@linuxidc ~]# vim /etc/selinux/config

[root@linuxidc ~]# cat /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=disabled #修改配置文件,需要重启系统以生效

# SELINUXTYPE= can take one of three two values:

# targeted - Targeted processes are protected,

# minimum - Modification of targeted policy. Only selected processes are protected.

# mls - Multi Level Security protection.

SELINUXTYPE=targeted

[root@linuxidc ~]#

7.重启系统

重启系统方式有很多:

第一种方法:

[root@linuxidc ~]# reboot

第二种方法:

[root@linuxidc ~]# init 6

这是系统启动级别命令,参考我另一个博客文章:

http://blog.51cto.com/linuxidcheng/2161336

第三种方法:

虚拟机--》鼠标右击--》电源--》重新启动客户机

632ae81b8deca82a52e4d0d58ac8d5f2.png

8.查看selinux状态

[root@linuxidc ~]# getenforce

Disabled

三、Selinux进阶---上下文

提醒:如果想使用上下文,必须开启selinux才能有效,如果设置了上下文,却没有开启selinux,那等于白忙了。

这里举个例子:其他设置,类比就好

1.安装Apache服务

[root@linuxidc ~]# yum install -y httpd #提供网页服务

2.安装网络工具

[root@linuxidc ~]# yum install -y net-tools #提供查询系统网络状态的工具

3.启动网页服务并检查网站端口(默认端口为80)

[root@linuxidc ~]# systemctl start httpd && netstat -tunlp |grep httpd

tcp6 0 0 :::80 :::* LISTEN 1633/httpd

[root@linuxidc ~]#

4.访问网页(默认网页)

cf549233acf096f363af0a8f4273a500.png

5.开启selinux

[root@linuxidc ~]# getenforce

Enforcing

[root@linuxidc ~]#

6.设置自定义网页

c2a84c2e856dab4b4ebf9be06882405d.png

7.查看web上下文

[root@linuxidc html]# ls -Z

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html

[root@linuxidc html]#

上下文为:httpd_sys_content_t

8.在家目录创建网页文件,并移动到网站根目录

[root@linuxidc html]# cd

[root@linuxidc ~]# vim linuxidc.html

[root@linuxidc ~]# cat linuxidc.html

test2 web

linuxidc

[root@linuxidc ~]# mv linuxidc.html /var/www/html/

[root@linuxidc ~]# cd /var/www/html/

[root@linuxidc html]# ls -Z

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 linuxidc.html

[root@linuxidc html]#

9.访问新创建的网页文件(显示没权限访问该文件)

1415ac43e0f47c2b910cfcfec483a141.png

10.设置文件selinux,并访问网页(可正常访问该网页文件)

42c4b8107fc088d4026f0ff963b25902.png

11.chcon命令掌握

[root@linuxidc html]# chcon --help

Usage: chcon [OPTION]... CONTEXT FILE...

or: chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...

or: chcon [OPTION]... --reference=RFILE FILE...

选项如下:

--reference #引用其他文件上下文

--dereference #不引用其他文件上下文

-h, --no-dereference #影响符号链接而不是引用文件

-u, --user=USER #指定用户

-r, --role=ROLE #指定角色

-t, --type=TYPE #指定上下文

-l, --range=RANGE #指定上下文的范围

--no-preserve-root #不区分对待根目录

--preserve-root #区分对待根目录

--reference=RFILE #直接引用该文件的上下文

-R, --recursive #递归,一般用在目录

-v, --verbose #对每个文件输出信息

12.显示一下版本信息

[root@linuxidc html]# chcon --version

chcon (GNU coreutils) 8.22

Copyright (C) 2013 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later .

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Written by Russell Coker and Jim Meyering.

[root@linuxidc html]#

0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值