SElinux

实验前环境配置
由于上一个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
##服务开启之后可以正常执行权限

public class FTPUtil { private FTPClient ftpClient=null; private boolean result = false; private FileInputStream fis; String ftpHost = "10.16.111.111"; String port = 21 String ftpUserName = "ftpuser11; String ftpPassword = "1234561"; /** * 登录服务器 * @param ftpInfo * @return * @throws IOException */ public FTPClient login() throws IOException { ftpClient = new FTPClient(); ftpClient.connect(ftpHost); boolean login = ftpClient.login(ftpUserName,ftpPassword); int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); } if(login){ System.out.println("ftp连接成功!"); }else{ System.out.println("ftp连接失败!"); } //ftpClient.setControlEncoding("GBK"); return ftpClient; } /** * 字符串作为文件上传指定目录 下 * @param content 源字符串 * @param uploadDir 上传目录 * @param ftpFileName 上传文件名称 * @throws Exception */ public void ftpUploadByText(String content ,String uploadDir,String ftpFileName) throws Exception{ try { ftpClient = this.login(); //创建目录 createDir(ftpClient,uploadDir); // 设置上传目录 这个也应该用配置文件读取 ftpClient.changeWorkingDirectory(uploadDir); ftpClient.setBufferSize(1024); ftpClient.setControlEncoding("GBK"); // 设置文件类型(二进制) ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); String fileName = new String(ftpFileName.getBytes("GBK"),"iso-8859-1"); OutputStream os = ftpClient.storeFileStream(fileName); byte[] bytes = content.getBytes(); os.write(bytes); os.flush(); os.close(); } catch (Exception e) { ftpClient.disconnect(); ftpClient = null; e.printStackTrace(); throw e; }finally{ ftpClient.disconnect(); ftpClient = null; } } /** * 移动文件 * @param ftpInfo * @return * @throws Exception */ public boolean moveFile(FTPInfo ftpInfo)throws Exception { boolean flag = false; try { ftpClient = this.login(); flag = this.moveFile(ftpClient, ftpInfo.getChangeWorkingDirectoryPath(), ftpInfo.getFilePath()); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try { ftpClient.disconnect(); ftpClient = null; } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭FTP连接发生异常!", e); }catch (Exception e) { e.printStackTrace(); throw e; } } return flag; } /** * 删除文件 * @param ftpInfo * @return * @throws Exception */ public boolean deleteFile(FTPInfo ftpInfo)throws Exception { boolean flag = false; try { ftpClient = this.login(); flag = this.deleteByFolder(ftpClient, ftpInfo.getChangeWorkingDirectoryPath()); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try { ftpClient.disconnect(); ftpClient = null; } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭FTP连接发生异常!", e); }catch (Exception e) { e.printStackTrace(); throw e; } } return flag; } /** * 实现文件的移动,这里做的是一个文件夹下的所有内容移动到新的文件, * 如果要做指定文件移动,加个判断判断文件名 * 如果不需要移动,只是需要文件重命名,可以使用ftp.rename(oleName,newName) * @param ftp * @param oldPath * @param newPath * @return */ public boolean moveFile(FTPClient ftp,String oldPath,String newPath){ boolean flag = false; try { ftp.changeWorkingDirectory(oldPath); ftp.enterLocalPassiveMode(); //获取文件数组 FTPFile[] files = ftp.listFiles(); //新文件夹不存在则创建 if(!ftp.changeWorkingDirectory(newPath)){ ftp.makeDirectory(newPath); } //回到原有工作目录 ftp.changeWorkingDirectory(oldPath); for (FTPFile file : files) { if(file.isDirectory()) { moveFile(ftp,oldPath+file.getName()+"/" ,newPath+file.getName()+"/"); }else{ //转存目录 flag = ftp.rename(oldPath+new String(file.getName().getBytes("GBK"),"ISO-8859-1"), newPath+"/"+new String(file.getName().getBytes("GBK"),"ISO-8859-1")); } if(flag){ System.out.println(file.getName()+"移动成功"); }else{ System.out.println(file.getName()+"移动失败"); } } ftp.removeDirectory(new String(oldPath.getBytes("GBK"),"ISO-8859-1")); } catch (Exception e) { e.printStackTrace(); System.out.println("移动文件失败"); } return flag; } /** * 删除FTP上指定文件夹下文件及其子文件方法,添加了对中文目录的支持 * @param ftp FTPClient对象 * @param FtpFolder 需要删除的文件夹 * @return */ public boolean deleteByFolder(FTPClient ftp,String FtpFolder){ boolean flag = false; try { ftp.changeWorkingDirectory(new String(FtpFolder.getBytes("GBK"),"ISO-8859-1")); ftp.enterLocalPassiveMode(); FTPFile[] files = ftp.listFiles(); for (FTPFile file : files) { //判断为文件则删除 if(file.isFile()){ ftp.deleteFile(FtpFolder+new String(file.getName().getBytes("GBK"),"ISO-8859-1")); } //判断是文件夹 if(file.isDirectory()){ String childPath = FtpFolder +file.getName()+ "/"; //递归删除子文件夹 deleteByFolder(ftp,childPath); } } //循环完成后删除文件夹 flag = ftp.removeDirectory(new String(FtpFolder.getBytes("GBK"),"ISO-8859-1")); if(flag){ System.out.println(FtpFolder+"文件夹删除成功"); }else{ System.out.println(FtpFolder+"文件夹删除成功"); } } catch (Exception e) { e.printStackTrace(); System.out.println("删除失败"); } return flag; } /** * 创建目录 * @param createpath * @param sftp */ public void createDir(FTPClient ftpClient,String createpath) throws Exception { try { if(ftpClient.changeWorkingDirectory(createpath)) { return; } String pathArry[] = createpath.split("/"); StringBuffer filePath = new StringBuffer("/"); for (String path : pathArry) { if (path.equals("")) { continue; } filePath.append(path + "/"); if(!ftpClient.changeWorkingDirectory(filePath.toString())) { ftpClient.makeDirectory(filePath.toString()); ftpClient.changeWorkingDirectory(filePath.toString()); } } ftpClient.changeWorkingDirectory(createpath); }catch (Exception e) { e.printStackTrace(); throw new Exception("创建路径错误:" + createpath); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值