阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux基础教程之系统自动化安装和SELinux简介

130次阅读
没有评论

共计 12285 个字符,预计需要花费 31 分钟才能阅读完成。

一、知识整理

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

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

2、创建 kickstart 文件:

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

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

检查 ks 文件语法错误:ksvalidator

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

DAC:Discretionary Access Control 自由访问控制

MAC:Mandatory Access Control 强制访问控制

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

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

minimum:centos7,修改过的 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:指示登录系统的用户类型,如 root,user_u,system_u,多数本地进程都属于自由(unconfined)进程;

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

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

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

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

Category:对于特定组织划分不分层的分类,如 FBI Secret,NSA secret,一个对象可以有多个 category,c0-c1023 共 1024 个分类,Target 策略不是用 category。

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

主体,进程称为主体

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

当一个 subject 试图访问一个 object,kernel 中的策略执行服务器将建成 AVC(访问矢量缓存 Access Vector Cache),在 AVC 中,subject 和 object 的权限被缓存(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 设置为 permissive 或 enforcing

[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)

步骤五:测试使用

使用光盘镜像:

Linux 基础教程之系统自动化安装和 SELinux 简介

开机使用光盘启动:

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

Linux 基础教程之系统自动化安装和 SELinux 简介

创建引导 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 访问限制,关闭防火墙,访问检验

Linux 基础教程之系统自动化安装和 SELinux 简介

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 浏览器检验是否能够访问:

Linux 基础教程之系统自动化安装和 SELinux 简介

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

Linux 基础教程之系统自动化安装和 SELinux 简介

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-12-03发表,共计12285字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中