实验前环境配置
由于上一个vsftpd实验对selinux也做了更改,所以这里需要改掉之前的配置或者直接删除vsftpd.conf文件然后重新安装vsftpd
确认效果:
1、目录下的文件能识别
lftp -u 登陆后能查看
2、lftp -u 登陆后能上传删除
接下来开始实验
selinux对文件系统的影响
[root@localhost vsftpd]# cd /mnt
[root@localhost mnt]# touch file
[root@localhost mnt]# ls -Z
-rw-r–r-- root root ? file
[root@localhost mnt]# ps auxZ | grep ftp
-
root 2434 0.0 0.0 52756 692 ? Ss 01:46 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
-
root 2961 0.0 0.1 112640 980 pts/0 S+ 02:07 0:00 grep --color=auto ftp
[root@localhost mnt]# lftp 172.25.254.131
lftp 172.25.254.131:~> quit
[root@localhost mnt]# mv file /var/ftp/pub
[root@localhost mnt]# lftp 172.25.254.131
lftp 172.25.254.131:~> ls
-rw-r–r-- 1 0 0 0 Oct 24 04:57 file
drwxr-xr-x 3 0 0 67 Oct 26 06:09 pub
lftp 172.25.254.131:/> quit
[root@localhost mnt]# vim /etc/sysconfig/selinux ##将selinux类型改为disabled
然后重启查看效果
[root@localhost Desktop]# cd /mnt
[root@localhost mnt]# touch file2
[root@localhost mnt]# mv file2 /var/ftp
[root@localhost mnt]# lftp 172.25.254.131
lftp 172.25.254.131:~> ls
-rw-r–r-- 1 0 0 0 Oct 24 04:57 file
drwxr-xr-x 3 0 0 67 Oct 26 06:09 pub
lftp 172.25.254.131:/> quit
[root@localhost mnt]# ls /var/ftp/
file file2 pub
发现创建的文件无法查看
[root@localhost mnt]# lftp 172.25.254.131 -u westos
Password:
lftp westos@172.25.254.131:~> ls
-rw-r–r-- 1 1003 1003 2120 Oct 26 05:50 passwd
lftp westos@172.25.254.131:~> rm passwd
rm: Access failed: 550 Delete operation failed. (passwd)
lftp westos@172.25.254.131:~> ls
-rw-r–r-- 1 1003 1003 2120 Oct 26 05:50 passwd
lftp westos@172.25.254.131:~> put /etc/group
put: Access failed: 553 Could not create file. (group)
lftp westos@172.25.254.131:~> quit
可以看到以系统本地用户登陆也无法进行相关操作
[root@localhost mnt]# ls -Z /mnt/
[root@localhost mnt]# mv /var/ftp/file2 /mnt/
[root@localhost mnt]# ls -Z /mnt/
-rw-r–r--. root root unconfined_u:object_r:mnt_t:s0 file2 ##可以看到和重启前的问号不同,这就是selinux加的标签
[root@localhost mnt]# ps auxZ | grep ftp
system_u:system_r:ftpd_t:s0-s0:c0.c1023 root 1150 0.0 0.0 52756 564 ? Ss 02:16 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 2451 0.0 0.1 112644 976 pts/0 R+ 02:24 0:00 grep --color=auto ftp
[root@localhost mnt]# ls
file2
临时更改selinux的标签
[root@localhost mnt]# cd /var/ftp/
[root@localhost ftp]# ls
file pub
[root@localhost ftp]# mv /mnt/file2 /var/ftp/
[root@localhost ftp]# ls
file file2 pub
[root@localhost ftp]# ls -Z
-rw-r–r--. root root system_u:object_r:public_content_t:s0 file
-rw-r–r--. root root unconfined_u:object_r:mnt_t:s0 file2
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub
[root@localhost ftp]# chcon -t public_content_t file2
[root@localhost ftp]# ls -Z
-rw-r–r--. root root system_u:object_r:public_content_t:s0 file
-rw-r–r--. root root unconfined_u:object_r:public_content_t:s0 file2
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub
[root@localhost ftp]# lftp 172.25.254.131 ##file2的标签已经更改为 public_content_t
lftp 172.25.254.131:~> ls
-rw-r–r-- 1 0 0 0 Oct 24 04:57 file
-rw-r–r-- 1 0 0 0 Oct 26 06:18 file2
drwxr-xr-x 3 0 0 67 Oct 26 06:09 pub
lftp 172.25.254.131:/> quit
[root@localhost ftp]# cd /
[root@localhost /]# ls
bin etc lib mnt public sbin tmp westos
boot ftphome lib64 opt root srv usr
dev home media proc run sys var
[root@localhost /]# mkdir /redhat
[root@localhost /]# ls -Zd /redhat/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /redhat/
[root@localhost /]# chcon -t public_content_t /redhat/
[root@localhost /]# ls -Zd /redhat/
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /redhat/
##/redhat目录的标签也改为public_content_t
永久更改标签
[root@localhost Desktop]# ls -Zd /redhat/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /redhat/
[root@localhost Desktop]# semanage fcontext -l | grep /var/ftp
/var/ftp(/.)? all files system_u:object_r:public_content_t:s0
/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 Desktop]# semanage fcontext -l | grep redhat
/etc/redhat-lsb(/.)? all files system_u:object_r:bin_t:s0
[root@localhost Desktop]# semanage fcontext -l | grep /redhat
/etc/redhat-lsb(/.)? all files system_u:object_r:bin_t:s0
##可以看到内核没有redhat的相关信息,说明之前的修改不是永久的
[root@localhost Desktop]# semanage fcontext -a -t public_content_t '/redhat(/.)?’
[root@localhost Desktop]# semanage fcontext -l | grep /redhat/etc/redhat-lsb(/.)? all files system_u:object_r:bin_t:s0
/redhat(/.*)? all files system_u:object_r:public_content_t:s0 ##文件标签修改为 public_content_t
[root@localhost Desktop]# ls -Zd /redhat/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /redhat/
[root@localhost Desktop]# restorecon -RvvF /redhat/
restorecon reset /redhat context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
##通知系统内核记录本次标签修改,R表示递归,两个v代表显示在该目录下子文件的修改标签信息,F代表强制设置目录子文件和目录的属性一致
selinux对程序的影响就是为程序加了个开关,0表示关闭,1表示开启
[root@localhost Desktop]# lftp 172.25.254.131 -u westos
Password:
lftp westos@172.25.254.131:~> ls
-rw-r–r-- 1 1003 1003 2120 Oct 26 05:50 passwd
lftp westos@172.25.254.131:~> put /etc/group
put: Access failed: 553 Could not create file. (group)
lftp westos@172.25.254.131:~> quit
[root@localhost Desktop]# getsebool -a | grep 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
##可以看到ftp_home_dir --> off是关闭的
[root@localhost Desktop]# setsebool -P ftp_home_dir on
[root@localhost Desktop]# getsebool -a | grep ftp
ftp_home_dir --> on
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 Desktop]# lftp 172.25.254.131 -u westos
Password:
lftp westos@172.25.254.131:~> ls
-rw-r–r-- 1 1003 1003 2120 Oct 26 05:50 passwd
lftp westos@172.25.254.131:~> put /etc/group
888 bytes transferred
lftp westos@172.25.254.131:~> rm -fr group
rm ok, `group’ removed
lftp westos@172.25.254.131:~> quit
##服务开启之后可以正常执行权限