系统自动化安装和SELinux

一、知识整理

1anaconda系统安装程序:默认图形启动;

使用光盘启动,在选择模式界面tab键在后面增加text或按下ESC键,输入lnux text进入字符界面安装。

2、创建kickstart文件:

直接手动编辑:依据模板修改,/root目录下的anaconda.cfg

使用创建工具创建:system-config-kickstart,图形化工具:也可以使用模板修改

检查ks文件语法错误:ksvalidator

3、SELinux是美国国家安全局NSAthe National Security Agency)和SCCSecure Computing Corporation)开发的linux的一个强制访问控制的安全模块。2000年以GNU GPL发布,linux内核2.6版本后集成在内核中。模型有两种:

DACDiscretionary Access Control自由访问控制

MACMandatory Access Control 强制访问控制

工作类型有四种:strictcentos5,每个进程都收到sellinux的控制;

targeted:用来保护常见的网络服务,仅有限进程受到selinux控制,只监控容易被入侵的进程,rhel4只保护13个服务,rhel5保护88个服务。

minimumcentos7,修改过的targeted,只对选择的网络服务;

mls:提供MLS(多级安全)机制的安全性

后两者稳定性不足,未加以应用。

4、传统Linux一切皆文件,由用户,组,权限控制访问在SElinux中,一切皆对象,由存放在Inode的扩展属性域的安全元素所控制其访问。所有文件和端口资源和进程都具备安全标签:安全上下文(security context)。安全上下文有五个元素组成:

user:role:type:sensitivity:category

user_u:object_r:tmp_t:s0:c0

实际上下文:存放在文件系统中,ls -Z可以查看文件的元素;ps -Z查看进程的。

期望上下文:存放在二进制的SELinux策略库(映射目录和期望安全上下文)中

semanage fcontext -l查看所有期望上下文

五个安全元素:User:指示登录系统的用户类型,如rootuser_usystem_u,多数本地进程都属于自由(unconfined)进程;

Role:定义文件、进程和用户的用途:文件:object_r,进程和用户:system_r

Type:指定数据类型,规则中定义何种进程类型访问何种文件;

Target策略基于type实现,多服务公用:public_content_t

sensitivity:限制访问的需要,由组织定义的分层安全级别,如unclassifiedsecrettopsecret,一个对象有且只有一个sensitivity,分0-15级,s0最低,Target策略默认使用s0

Category:对于特定组织划分不分层的分类,如FBI SecretNSA secret,一个对象可以有多个categoryc0-c10231024个分类,Target策略不是用category

5、SElinux策略:对象:所有可以读取的对象,包括文件、目录和进程、端口等

主体,进程称为主体

SELinux中对所有的文件都赋予一个type的文件类型便签,对于多有的进程也赋予各自的一个domain的标签。Domain标签能够执行的操作由安全策略里定义。

当一个subject试图访问一个objectkernel中的策略执行服务器将建成AVC(访问矢量缓存Access Vector Cache),在AVC中,subjectobject的权限被缓存(cached),查找“应用+文件”的安全环境。然后根据查询结果允许或拒绝访问。

安全策略:定义主体读取对象的规则数据库,规则中记录了哪个类型的主体使用哪个方法读取哪一个对象是允许还是的,并且定义了哪种行为是允许或拒绝。

6、SELinux帮助:yum -y install selinux-policy-devel

centos6中使用makewhatis同步数据库;在centos7中使用mandb同步数据库。

 

二、命令详解和事例

1、SELinux的状态:enforcing:强制,每个受限的进程都必然受限;

permissive:允许,每个受限的进程违规操作不会被禁止,但会被记录于审计日志;

disabled:禁用。

2、getenforce 获取sellinux当前状态

sestatus 查看selinux状态

setenforce 0|1 设置为permissiveenforcing

[root@centos68 usb]# getenforce 
Enforcing
[root@centos68 usb]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted
[root@centos68 usb]# setenforce 0
[root@centos68 usb]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   permissive
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted
[root@centos68 usb]# getenforce 
Permissive

配置文件:

/boot/grub/grub.conf使用selinux=0禁用selinux

/etc/sysconfig/selinux

/etc/selinux/config

所有的修改都无法直接生效,都必须重启之后生效。

3、给文件重新打安全标签:chcon [opt] [-u USER] [-r ROLE] [-t TYPE] FILE

-R递归设置

–reference=FILE 与此文件相同设置

[root@centos68 tmp]# ll -Z
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 f1
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 f2
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 f3
[root@centos68 tmp]# chcon -u unconfined_u -r object_r -t default_t f1
[root@centos68 tmp]# ll -Z
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 f1
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 f2
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 f3
[root@centos68 tmp]# chcon --reference=f2 f1
[root@centos68 tmp]# ll -Z 
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 f1
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 f2
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 f3

恢复目录或文件默认的安全上下文:restorecon /PATH/FILE

查看默认的安全上下文,若没有默认安全上下文则无法设置:semanage fcontext -l

semanage来自policycoreutils-python

添加安全上下文:semanage scontext -a -t httpd_sys_content_t /testdir(/.*)?

restorecon -Rv /testdir

删除安全上下文:semanage fcontext -d -t httpd_sys_content_t  /testdir(/.*)?

[root@centos68 tmp]# semanage fcontext -a -t default_t '/tmp(/.*)?'
[root@centos68 tmp]# ll -Z
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 f1
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 f2
-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 f3
[root@centos68 tmp]# semanage fcontext -l | grep "/tmp(/.*)?"
/tmp(/.*)?     all files          system_u:object_r:default_t:s0 
[root@centos68 tmp]# restorecon -Rv /tmp
restorecon reset /tmp/f3 context unconfined_u:object_r:user_tmp_t:s0->unconfined_u:object_r:default_t:s0
restorecon reset /tmp/.ICE-unix context system_u:object_r:xdm_tmp_t:s0->system_u:object_r:default_t:s0
restorecon reset /tmp/f2 context unconfined_u:object_r:user_tmp_t:s0->unconfined_u:object_r:default_t:s0
[root@centos68 tmp]# ll -Z
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 f1
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 f2
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 f3
[root@centos68 tmp]# semanage fcontext -d -t default_t '/tmp(/.*)?'
[root@centos68 tmp]# semanage fcontext -l | grep "/tmp(/.*)?"

对文件进行移动不改变安全标签;复制文件则改变便签。

4、端口便签:查看端口标签:semanage port -l

添加端口:semanage port -a -t port_label -p tcp|udp PORT

删除端口:semanage port -d -t port_label -p tcp|udp PORT

[root@centos68 tmp]# semanage port -a -t tftp_port_t -p udp  9527
[root@centos68 tmp]# semanage port -l | grep "tftp_port_t"
tftp_port_t                    udp      9527, 69
[root@centos68 tmp]# semanage port -d -t tftp_port_t -p udp 9527
[root@centos68 tmp]# semanage port -l | grep "tftp_port_t"
tftp_port_t                    udp      69

修改端口:semanage port -m -t port_label -p tcp|udp PORT

5、SElinux的布尔值:

查看bool值:getsebool -a

查看bool值,包括说明semanage boolean -l

查看修改过的布尔值:semanage boolean -l -C

设置bool值的命令:

setsebool BOOLEAN VALUE

0为开启,1为关闭;选项-P永久生效

[root@centos68 ~]# semanage boolean -l | grep virt_use_samba
virt_use_samba                 (关    ,    关)  Allow virt to manage cifs files
[root@centos68 ~]# setsebool virt_use_samba 1
[root@centos68 ~]# semanage boolean -l | grep virt_use_samba
virt_use_samba                 (开    ,    关)  Allow virt to manage cifs files

6、SELinux日志管理:yum install setroublesshoot*(重启生效)

将错误的信息写入/var/log/message

[root@centos68 ~]# grep setroubleshoot /var/log/messages
Sep 11 03:50:45 centos68 yum[4947]: Installed: setroubleshoot-server-3.0.47-11.el6.x86_64
Sep 11 03:50:47 centos68 yum[4947]: Installed: setroubleshoot-plugins-3.0.40-2.el6.noarch
Sep 11 03:50:48 centos68 yum[4947]: Installed: setroubleshoot-3.0.47-11.el6.x86_64

查看安全日志说明:

[root@centos68 ~]# sealert -l 0
Error
query_alerts error (1003): id (0) not found
扫描并分析日志:sealert -a /var/log/audit/audit.log
[root@centos68 ~]# sealert -a /var/log/audit/audit.log 
100% donefound 0 alerts in /var/log/audit/audit.log

三、课后练习

1、制作光盘或U盘引导盘。

创建引导光盘:

步骤一:复制光盘目录下的isolinux目录至/tmp/myiso目录下

[root@centos68 tmp]# mkdir myiso
[root@centos68 tmp]# cp -rf /media/cdrom/isolinux ./myiso/

步骤二:编辑isolinux.cfg

[root@centos68 tmp]# vim myiso/isolinux/isolinux.cfg 
label linux
  menu label ^Install or upgrade an existing system
  menu default
  kernel vmlinuz
  append initrd=initrd.img text ks=cdrom:/myks.cfg

步骤三:生成kickstart文件myks.cfg并将其放入isolinux目录中,此处使用在图形界面下创建的kickstart文件:

[root@centos68 tmp]# cp /root/myks.cfg ./myiso/
[root@centos68 tmp]# vim myiso/myks.cfg

步骤四:生成引导文件,光盘镜像boot.iso

[root@centos68 tmp]# cd myiso/
[root@centos68 myiso]# mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 6.8 x86_64 boot" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso  ./
I: -input-charset not specified, using utf-8 (detected in locale settings)
genisoimage 1.1.9 (Linux)
Scanning ./
Scanning ./isolinux
Excluded by match: ./isolinux/boot.cat
Excluded: ./isolinux/TRANS.TBL
Writing:   Initial Padblock                        Start Block 0
Done with: Initial Padblock                        Block(s)    16
Writing:   Primary Volume Descriptor               Start Block 16
Done with: Primary Volume Descriptor               Block(s)    1
Writing:   Eltorito Volume Descriptor              Start Block 17
Size of boot image is 4 sectors -> No emulation
Done with: Eltorito Volume Descriptor              Block(s)    1
Writing:   Joliet Volume Descriptor                Start Block 18
Done with: Joliet Volume Descriptor                Block(s)    1
Writing:   End Volume Descriptor                   Start Block 19
Done with: End Volume Descriptor                   Block(s)    1
Writing:   Version block                           Start Block 20
Done with: Version block                           Block(s)    1
Writing:   Path table                              Start Block 21
Done with: Path table                              Block(s)    4
Writing:   Joliet path table                       Start Block 25
Done with: Joliet path table                       Block(s)    4
Writing:   Directory tree                          Start Block 29
Done with: Directory tree                          Block(s)    2
Writing:   Joliet directory tree                   Start Block 31
Done with: Joliet directory tree                   Block(s)    2
Writing:   Directory tree cleanup                  Start Block 33
Done with: Directory tree cleanup                  Block(s)    0
Writing:   Extension record                        Start Block 33
Done with: Extension record                        Block(s)    1
Writing:   The File(s)                             Start Block 34
 22.37% done, estimate finish Sun Sep 11 12:10:41 2016
 44.66% done, estimate finish Sun Sep 11 12:10:41 2016
 67.02% done, estimate finish Sun Sep 11 12:10:41 2016
 89.29% done, estimate finish Sun Sep 11 12:10:41 2016
Total translation table size: 4703
Total rockridge attributes bytes: 1440
Total directory bytes: 2048
Path table size(bytes): 26
Done with: The File(s)                             Block(s)    22215
Writing:   Ending Padblock                         Start Block 22249
Done with: Ending Padblock                         Block(s)    150
Max brk space used 0
22399 extents written (43 MB)

步骤五:测试使用

使用光盘镜像:

blob.png

开机使用光盘启动:

注意:添加虚拟机的时候给的空间不能少于ks模板中给定的数值,否则报错。

blob.png

创建引导U

方法一:直接将光盘内容写入U盘使用;

[root@centos68 ~]# dd if=/dev/sr0 of=/dev/sdb

记录了7649280+0 的读入

记录了7649280+0 的写出

3916431360字节(3.9 GB)已复制,160.877 秒,24.3 MB/

 

2、安装http服务,改变网站的默认主目录为/website,添加SELinux文件标签规则,设置http_sys_content_t/website及目录下所有文件,使网站可访问。

步骤一:更改配置文件,改变默认主目录:

[root@centos68 ~]# vim /etc/httpd/conf/httpd.conf
# This should be changed to whatever you set DocumentRoot to.
<Directory "/var/www/website">
# symbolic links and aliases may be used to point to other locations.
DocumentRoot "/var/www/website"

更改两行,将目录设置为website

步骤二:重启服务,添加网页文件

[root@centos68 website]# service httpd restart
停止 httpd:                          [确定]
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for centos68
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName     [确定]
[root@centos68 website]# echo "hello man" > index.html
[root@centos68 website]# ls
index.html

步骤三:关闭selinux访问限制,关闭防火墙,访问检验

blob.png

1、修改网站端口为9527,增加SELinux端口标签,使网站可访问。

修改http监听的端口:

[root@centos68 ~]# semanage port -l  | grep http
http_cache_port_t              tcp      3128, 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@centos68 ~]# semanage port -a -t http_port_t -p tcp 9527
[root@centos68 ~]# semanage port -l  | grep http
http_cache_port_t              tcp      3128, 8080, 8118, 8123, 10001-10010
http_cache_port_t              udp      3130
http_port_t                    tcp      9527, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989

修改文件:

#Listen 12.34.56.78:80

Listen 80

Listen 9527

使用windows浏览器检验是否能够访问:

blob.png

3、启动SELinux布尔值,使用户student的家目录可通过http访问。

[root@centos68 ~]# semanage boolean -l | grep http
httpd_enable_homedirs     (关 , 关)  Allow httpd to read home directories
[root@centos68 ~]# setsebool httpd_enable_homedirs 1
[root@centos68 ~]# semanage boolean -l | grep httpd_enable_homedirs
httpd_enable_homedirs      (开    ,    关)  Allow httpd to read home directories

更改配置文件:

<IfModule mod_userdir.c>

    #UserDir disabled

    UserDir public_html

</IfModule>

<Directory /home/*/public_html>

    AllowOverride FileInfo AuthConfig Limit

    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec

    <Limit GET POST OPTIONS>

        Order allow,deny

        Allow from all

    </Limit>

    <LimitExcept GET POST OPTIONS>

        Order deny,allow

        Deny from all

    </LimitExcept>

</Directory>

按配置文件的格式来看,需要家目录中的文件名如下

[root@centos68 user1]# echo 43123123 > public_html

blob.png

原创文章,作者:SilencePavilion,如若转载,请注明出处:http://www.178linux.com/49220

(0)
上一篇 2016-09-26 08:52
下一篇 2016-09-26 08:56

相关推荐

  • N25第8周作业

    1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;     在线的主机使用绿色显示;      不在线的主使用红色显示; #!/bin/bash ipnet=172.20.51. trap ‘trap_action’ INT trap…

    Linux干货 2017-02-22
  • 文本处理工具之AWK

    概述:     在之前的文章中,我们介绍过文本处理三剑客的grep、sed,本篇就简要说明下awk的用法。主要是围绕常见的一些用法进行展开,分为以下几个部分:     1、awk的基础语法     2、awk的进阶语法  &nbs…

    Linux干货 2016-09-20
  • linux基础知识:计算机的组成及其功能

    摘要:
    1. 描述计算机的组成及其功能。
    2. 按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别。
    3. 描述Linux的哲学思想,按照自己的理解对其解释性描述。
    4. 说明Linux系统上命令的使用格式;详细介绍ifconfig、echo、……等命令使用,配合相应实例阐述。
    5. 如何获取帮助信息,描述man文档章节划分。
    6. 罗列发行版基础目录名称命名法则及功用规定。

    2017-12-03
  • 高级文件系统管理之mdadm与lvm的创建

    ※配置配额系统 磁盘配额就是管理员可以为用户所能使用的磁盘空间进行配额限制,每一用户只能使用最大配额范围内的磁盘空间,比如一些网盘就是基于这个功能来做的,分配给用户固定的空间,花钱买,可以将空间设置的更大。 功能作用 磁盘配额可以限制指定账户能够使用的磁盘空间,这样可以避免因某个用户的过度的使用磁盘空间造成其它用户无法运行或工作甚至影响系统运行。这个功能不止…

    Linux干货 2016-08-27
  • Shell脚本编程—数组、字符串处理

    数组 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间     数组名:整个数组只有一个名字;     数组索引:编号从0开始;          [数组名索引]  &nbs…

    Linux干货 2016-08-30
  • 新鲜出炉的一篇好PPT,MySQL Optimizer团队力作

    新鲜出炉的一篇好PPT,MySQL Optimizer团队Senior Principal Software Engineer Øystein同学亲自操刀完成的《How to Analyze and Tune MySQL Queries for Better Performance》内容值得期待,有实现分析,有实战用例。有需要的朋友可以点击下载! 下载地址:…

    Linux干货 2015-02-27