实验环境:
[root@westos_student50 Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=ens160
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.60
NETWORK=255.255.255.0
NAME=ens160
[root@westos_student50 Desktop]# nmcli connection reload
[root@westos_student50 Desktop]# nmcli connection up ens160
[root@westos_student50 Desktop]# cd /etc/yum.repos.d
[root@westos_student50 yum.repos.d]# vim westos.repo
[root@westos_student50 yum.repos.d]# vim /etc/sysconfig/selinux
[root@westos_student50 Desktop]#mkdir /westos
[root@westos_student50 Desktop]#mount /iso/rhel-8.2-x86_64-dvd.iso /westos
[root@westos_student50 Desktop]#reboot
[root@westos_student50 Desktop]#dnf install vsftpd -y
[root@westos_student50 Desktop]#vim /etc/vsftpd/vsftpd.conf
[root@westos_student50 Desktop]#chgrp ftp /var/ftp/pub
[root@westos_student50 Desktop]#chmod 775 /var/ftp/pub
[root@westos_student50 Desktop]#systemctl enable vsftpd
[root@westos_student50 Desktop]#systemctl disable --now firewalld
[root@westos_student50 Desktop]#dnf install lftp -y
[root@westos_student50 Desktop]#vim /etc/vsftpd/vsftpd.conf
[root@westos_student50 Desktop]#systemctl restart vsftpd.service
- Selinux的功能
1.观察现象
当Selinux未开启时
在/mnt中建立文件被移动到/var/ftp下可以被vsftpd服务访问
匿名用户可以通过设置后上传文件
[root@westos_student50 ~]# touch /mnt/westosfile
[root@westos_student50 ~]# mv /mnt/westosfile /var/ftp
当使用ls -Z /var/ftp查看文件时显示
ps auxZ | grep vsftpd 时显示:
当selinux开启:
在/mnt中建立文件被移动到/var/ftp下不可以被vsftpd服务访问
匿名用户可以通过设置后仍然不能上传文件
当使用ls -Z /var/ftp查看文件时显示信息
ps auxZ | grep vsftpd 时显示:
selinux:
对于文件的影响:
当selinux开启时,内核会对每个文件及每个开启的程序进行标签加载
标签内记录程序和文件的安全上下文(context)
对于程序功能的影响:
当selinux开启会对程序的功能加载开关,并设定此开关的状态为关闭
当需要此功能时需要手动开启功能开关
此开关叫做sebool
- Selinux的状态及管理
1.selinux的开启
vim /etc/selinux/config
SELINUX=disabled #selinux关闭
SELINUX=enforcing #selinux开机设定为强制状态此状态为selinux开启
SELINUX=permissive #selinux开机设定为警告状态此状态为selinux开启
"selinux开启或关闭需要重启系统"
enforcing:
不符合条件一定不能被允许,并会收到警告信息
permissive:
不符合条件被允许,并会收到警告信息
selinux状态的查看:
[root@westos_student50 Desktop]# getenforce
selinux开启后强制和警告级别的转换(不需重启) :(不能改变到关闭,切换到关闭需要重启)
[root@westos_student50 Desktop]# setenforce 0
[root@westos_student50 Desktop]# setenforce 1
- Selinux的安全上下文
[root@westos_student50 Desktop]# ls -Z /var/ftp 查看文件的安全上下
[root@westos_student50 Desktop]# ls -Zd /var/ftp 查看目录的安全上下
[root@westos_student50 Desktop]# ps axZ 查看进程的安全上下文
2.修改安全上下文#临时修改
此方式更改的安全上下文在selinux重启后会还原
chcon -t 标签 文件|目录
[root@westos_student50 Desktop]# chcon -t public_content_t /var/ftp/westosfile2 修改文件安全上下文
[root@westos_student50 Desktop]# ls -Z /var/ftp[root@westos_student50 Desktop]# chcon -t public_content_t /westoss修改目录安全上下文
[root@westos_student50 Desktop]# ls -Zd /westoss
永久修改安全上下文
#如果需要特殊指定安全上下文需要修改内核安全上下文列表
[root@westos_student50 Desktop]# semanage fcontext -l | grep /var/ftp 查看安全上下文列表
[root@westos_student50 Desktop]# semanage fcontext -a -t public_content_t '/westoss(/.*)?'
[root@westos_student50 Desktop]# semanage fcontext -l | grep /westoss
[root@westos_student50 Desktop]# restorecon -RvvF /westoss
touch /.autorelabel ##重启系统时selinux初始化文件标签开关文件
四.SEBOOL
[root@westos_student50 mnt]# chcon -t public_content_rw_t /var/ftp/pub
[root@westos_student50 mnt]# ls -Zd /var/ftp/pub
[root@westos_student50 mnt]# getsebool -a | grep ftp
[root@westos_student50 mnt]# setsebool -P ftpd_anon_write on
[root@westos_student50 mnt]# getsebool -a | grep ftp
五.SEPORT
[root@westos_student50 mnt]# semanage port -l | grep ssh
[root@westos_student50 mnt]# vim /etc/ssh/sshd_config
[root@westos_student50 mnt]# systemctl restart sshd
[root@westos_student50 mnt]# semanage port -l | grep ssh
[root@westos_student50 mnt]# semanage port -a -t ssh_port_t -p tcp 2222
[root@westos_student50 mnt]# vim /etc/ssh/sshd_config
[root@westos_student50 mnt]# systemctl restart sshd
六.setrouble
[root@westos_student50 mnt]# semanage port -d -t ssh_port_t -p tcp 2222
[root@westos_student50 mnt]# semanage port -l | grep ssh