一.SELinux:
SELinux是一种基于 域-类型 模型(domain-type)的强制访问控(MAC)安全系统,它由NSA编写并设计成内核模块包含到内核中,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。
二.SELinux的模式:
[root@localhost yum.repos.d]# vim /etc/sysconfig/selinux ##编辑文件
[root@localhost yum.repos.d]# getenforce
Disabled ##禁用模式
[root@localhost yum.repos.d]# reboot
Connection to 172.25.254.220 closed by remote host.
Connection to 172.25.254.220 closed.
[kiosk@foundation20 Desktop]$ ssh root@172.25.254.220
root@172.25.254.220's password:
Last login: Sun May 6 02:54:06 2018 from 172.25.254.20
[root@localhost ~]# getenforce
Enforcing ##强制模式
setenforce 0 | 1
0 表示 permissive ##警告
1 表示 enforcing ##强制
两种情况都会有警告
模式转换需要重启,因为这是内核级的插件。
三.临时更改安全上下文:
所有操作系统访问控制都是以关联的客体和主体的某种类型的访问控制属性为基础的。在SELinux中,访问控制属性叫做安全上下文。selinux文件上下文通常是由父目录指定的,即在创建文件时,将父目录的安全上下文指定给新建文件。若文件是在别处建立,再利用mv或cp -a等命令移至其他位置,该文件仍将保持原来的selinux上下文。
[root@localhost ~]# ls -Zd /westos/ ##文件westos安全上下文已经失效
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/
[root@localhost ~]# ls -Zd /var/ftp/ /var/ftp/ ##目录永久生效
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp/
[root@localhost ~]# semanage fcontext -l | grep /var/ftp/ ##文件中已经写入了/var/ftp
/var/ftp/bin(/.*)? all files system_u:object_r:bin_t:s0
/var/ftp/etc(/.*)? all files system_u:object_r:etc_t:s0
/var/ftp/lib(/.*)? all files system_u:object_r:lib_t:s0
/var/ftp/lib/ld[^/]*\.so(\.[^/]*)* regular file system_u:object_r:ld_so_t:s0
[root@localhost ~]# semanage fcontext -l | grep /westos/ ##文件中没有写入westos
[root@localhost ~]# semanage fcontext -a -t public_content_t '/westos(/.*)?'
(-a表示添加,-t表示类型。意思将westos添加到配置文件中)
[root@localhost ~]# semanage fcontext -l | grep /westos ##查看配置文件是否写入
/westos(/.*)? all files system_u:object_r:public_content_t:s0
[root@localhost ~]# restorecon -FvvR /westos/ ##刷新
restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
[root@localhost ~]# lftp 172.25.254.220 ##连接可以显示文件下面的内容
lftp 172.25.254.220:~> ls
-rw-r--r-- 1 0 0 0 May 12 03:44 westosfile
lftp 172.25.254.221:/> quit
[root@localhost ~]# touch /.autorelabel ##建立这个文件相当于重启selinux
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# reboot ##重启
Connection to 172.25.254.220closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.220
root@172.25.254.220's password:
Last login: Fri May 11 22:08:26 2018 from 172.25.254.20
[root@localhost ~]# ls -Zd /westos ##查看已经有了安全上下文权限,意味着永久修改了安全上下文权限
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /westos
四.管理SELinux布尔值:
经常使用SELinux会发现,有些活动按理应该被允许,却被拒绝了。当有些原因是依据某些因素(选择),将推荐SELinux策略作者将策略设置为可选的。SELinux中可选意味着访问权限通过SELinux布尔值触发。SELinux布尔值是一个字符串(可赋予具体含义)来改变SELinux发挥作用。使用getsebool工具可以显示布尔值列表和当前值。
[root@localhost ~]# lftp 172.25.254.220 -u student ##用student用户登陆启用
Password:
lftp student@172.25.254.220:~> put /etc/inittab ##上传一个文件
put: Access failed: 553 Could not create file. (inittab) ##上传失败553(权限过小)
lftp student@172.25.254.220:~> quit
[root@localhost ~]# getsebool -a| grep ftp ##查看ftp服务的布尔值设置状态
ftp_home_dir --> off
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off
[root@localhost ~]# setsebool -P ftp_home_dir on (-p表示永久) ##修改ftp服务selinux策略值,并永久保存
[root@localhost ~]# lftp 172.25.254.220 -u student ##再次登陆
Password:
lftp student@172.25.254.220:~> put /etc/inittab ##上传
491 bytes transferred ##上传成功
lftp student@172.25.254.220:~> ls
-rw-r--r-- 1 1000 1000 491 May 12 03:43 inittab ##查看一下
五.更改端口:
[root@localhost ftp]# yum install httpd -y ##安装httpd
[root@localhost ftp]# vim /etc/httpd/conf/httpd.conf ##编辑文件
42 Listen 6666 ##在第42行更改端口
[root@localhost ftp]# systemctl restart httpd ##修改http端口后不能重启,因为http里没有6666的端口
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
[root@localhost ftp]# setenforce 0 ##警告
[root@localhost ftp]# systemctl restart httpd ##警告模式后可以重启
[root@localhost ftp]# semanage port -l | grep http ##查看当前selinux允许的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@localhost ftp]# semanage port -a -t http_port_t -p tcp 6666 ##添加6666端口到selinux
[root@localhost ftp]# 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 6666, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
[root@localhost ftp]# setenforce 1 ##强制警告也可以重启了
[root@localhost ftp]# systemctl restart httpd ##打开httpd服务