一、ftp服务用户黑白名单
- vim /etc/vsftpd/vsftpd.conf 编辑ftp配置文件(注意不要存在空格)
- chroot_local_user=YES
- chroot_list_enable=YES 启用名单
- chroot_list_file=/etc/vsftpd/chroot_list 设置名单地址
用户白名单的设定 - userlist_deny=NO
- /etc/vsftpd/chroot_list 此文件变成用户白名单,只有在白名单上的用户才可以登录ftp
vim /etc/vsftpd/ftpusers 用户黑名单
vim /etc/vsftpd/user_list 用户临时黑名单(重启后消失)
二、虚拟帐号身份指定(去掉黑白名单,保证环境纯净)
- vim /etc/vsftpd/studentfile 创建帐号密码文件(奇数行用户名、偶数行密码)
student1
123
student2
123
student3
123
- db_load -T(允许应用程序能够将文本文件转译载入进数据库) -t(指定转译载入的数据库类型) hash -f(文件) /etc/vsftpd/studentfile /etc/vsftpd/studentfile.db 创建加密文件
- vim /etc/pam.d/student 编辑密码配置文件(注意颜色)
account 帐号 required pam_userdb.so 检验程序帐号 db=/etc/vsftpd/studentfile 帐号密码文件
auth 密码 required pam_userdb.so 检验密码程序 db=/etc/vsftpd/studentfile 帐号密码文件案
- vim /etc/vsftpd/vsftpd.conf 编辑ftp配置文件
pam_service_name=student 虚拟用户的登录身份(密码配置文件的名字)
guest_enable=YES 启用服务
guest_username=student 虚拟用户登录身份(服务器上必须有)
- chmod u—x /home/student 去掉写权限
三、虚拟用户家目录的设定
- mkdir /ftpdir/student1/student1file 创建虚拟用户家目录位置
- vim /etc/vsftpd/vsftpd.conf
user_sub_token= USER引用shell环境变量localroot=/ftpdir/ USER 读取目录位置 - systemctl restart vsftpd 重起ftp服务
四、虚拟用户权限的区分(给虚拟用户上传删除权限)
- vim /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/confdir 读取虚拟用户权限配置目录
- mkdir /etc/vsftpd/confdir 创建虚拟用户配置目录
- vim /etc/vsftpd/confdir/student1 编辑虚拟用户student1的权限
anon_upload_enable=YES 上传文件
anon_other_write_enable=YES 删除重命名
- chgrp student /ftpdir/student{1..3} 更改虚拟用户家目录的组为student
- chmod 775 /ftpdir/student{1..3} 赋予组成员写的权限
五、内核级加强型火墙(需修改)
实验内容:打开内核级火墙,在pub下存在的文件可以通过lftp服务看见,在其他目录下建立的文件即使移动到pub目录下也无法看见,无法删除
- ls -Z 查看当前目录下文件的安全上下文
- vim /etc/sysconfig/selinus
disable改为enforcing
六、修改目录安全上下文(需修改)
- chcon -t public_content_t /var/ftp/pub/file 临时修改file文件安全上下文
restorecon /var/ftp/pub 刷新目录使里面的文件上下文和目录一直
semanage fcontext -a -t public_content_t ‘/westos(/.*)?’ 把westos目录添加到内核级安全文目录上
- restorecon -RvvF /westos 刷新目录同步安全文(R第归 vv过程 F刷新)
七、在enforcing模式下使匿名用户在pub目录上上传文件
- chgrp ftp /var/ftp/pub 更改匿名用户身份为ftp
- chmod 775 /var/ftp/pub 权限为775
- setenforceing 1 内核火墙为enforcing
- chcon -t public_content_rw_t /var/ftp/pub 设置/var/ftp/pub 安全文为可写
- setsebool -P(永久) ftpd_anon_write on 打开内核级匿名用户写文件开关
使用匿名用户身份在客户端连接ftp服务器在/pub目录下上传文件
八、selinux错误解决方案
- yun install setroubleshoot-server -y 安装setroublsehoot软件(安装即可运行)
进行错误操作(在selinux关闭匿名用户上传文件的功能) - cat /var/log/messages 查看日志找到解决方案(能解决问题但不安全)
建议开启selinux上所有功能 - cat /var/log/audit/audit.log selinux日志文件(记录信息但不提供解决方案)
- touch /.autorelabel 重置selinux后自动删除(多次开关selinux上的功能可能造成错误)