(二)vsftpd服务配置多用户多目录权限

(二)vsftpd服务配置多用户多目录权限

1,安装vsftpd服务

# 关闭SELINUX
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config && setenforce 0
# 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
# 时间同步
ntpdate 0.asia.pool.ntp.org
# 安装服务
yum install vsftpd -y
#此目录是配置文件目录
cd /etc/vsftpd/ && ls
# 启动
systemctl start vsftpd
# 开机自启
systemctl enable vsftpd

2,场景化

搭建应用部FTP服务器,要求:

  1. 每位员工拥有独立根目录
  2. 在自己目录下可见应用部公共目录publish(只读)、其他部门共享目录share(读写)
  3. 在自己目录下有独立的日志(工作日报)目录,可写、可删除权限,但目录本身员工不可删除
  4. 禁锢虚拟用户的主目录,同时禁止访问除主目录之外的目录
  5. 开启日志,记录登录、上传、下载、删除信息

目录规划

应用部共享目录:/home/vsftpuser/share(任何用户可删除可上传可下载)

应用部公共目录:/home/vsftpuser/publish (任何用户可下载,特殊用户可上传下载删除)

员工个人独立目录:/home/vsftpuser/app/{xxx} (只能当前用户才能上传下载删除)

mkdir -p /home/vsftpuser/publish
mkdir -p /home/vsftpuser/share
mkdir -p /home/vsftpuser/app/zhangsan
mkdir -p /home/vsftpuser/app/lisi

调整 vsftpd.conf 配置

vim /etc/vsftpd/vsftpd.conf
# 修改 第12行
# 关闭匿名
anonymous_enable=NO

# 关闭注释 第100行
# 用户不可出根目录
chroot_local_user=YES
# 出根目录白名单
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list

# 修改 第115行
listen=YES

# 打开注释 第123行
listen_ipv6=NO

# 修改
userlist_enable=YES
tcp_wrappers=YES

# 关闭注释 第43行
#connect_from_port_20=YES

# 新增
# 开启pam模块登录认证
pam_service_name=vsftpd.vu
# 禁锢虚拟用户的主目录,同时禁止访问除主目录之外的目录
virtual_use_local_privs=YES
guest_enable=YES
# 映射虚拟用户至系统用户(指定系统用户)
guest_username=vsftpuser
# 虚拟用户配置目录
user_config_dir=/etc/vsftpd/virconf/
allow_writeable_chroot=YES
# 开启被动模式
pasv_enable=YES
# 使用被动模式38001至39001端口
pasv_min_port=38001
pasv_max_port=39001

#使用本地时间
use_localtime=YES

pasv_address=192.168.56.90
pasv_addr_resolve=YES
ftp_data_port=16020

# 日志文件配置,并开启xferlog与vsftp.log双日志,xferlog记录上传与下载信息,vsftpd.log记录登录、上传、下载、删除等详细信息
dual_log_enable=YES
xferlog_enable=YES
log_ftp_protocol=YES
xferlog_std_format=YES
reverse_lookup_enable=NO

创建 chroot_list 文件

touch /etc/vsftpd/chroot_list

创建 vsftpuser 系统用户

# 所有vsftpd虚拟用户需要映射到一个系统用户,该系统用户不需要密码,也不需要登录,主要用于虚拟用户映射使用
useradd -d /home/vsftpuser -s /sbin/nologin vsftpuser

创建虚拟用户

vim /etc/vsftpd/vsftpuser

内容如下:

lisi  ##奇数行为用户名
123123 ##偶数行为密码
zhangsan
123123

生成ftp登录认证的数据库文件

db_load -T -t hash -f vsftpuser vsftpuser.db
chmod 600 vsftpuser ##安全起见,不让别人看到,设置权限
chmod 600 vsftpuser.db

配置ftp使用虚拟用户

vim /etc/pam.d/vsftpd.vu

完整配置如下:

auth required pam_userdb.so db=/etc/vsftpd/vsftpuser ##识别转换过的数据库文件
account required pam_userdb.so db=/etc/vsftpd/vsftpuser ##连接的密码服务

创建虚拟用户权限目录及配置

创建虚拟用户权限目录
mkdir /etc/vsftpd/virconf
虚拟用户权限配置

每个用户都有一个配置文件

zhangsan
vi /etc/vsftpd/virconf/zhangsan
local_root=/home/vsftpuser/app/zhangsan
virtual_use_local_privs=YES
# 允许登录用户有可写权限
write_enable=YES
# 允许匿名用户上传文件权限,只在write_enable=YES时生效
anon_upload_enable=YES
# 允许匿名用户创建目录,只有在write_enable=YES时生效
anon_mkdir_write_enable=YES
# 可写
allow_writeable_chroot=YES

参数说明:

完全权限:

cmds_allowed=ABOR,CWD,LIST,DELE,RMD,MDTM,MKD,NLST,PASS,PASV,PORT,PWD,QUIT,RETR,RNFR,RNTO,SIZE,STOR,TYPE,USER,ACCT,APPE,CDUP,HELP,MODE,NOOP,REIN,STAT,STOU,STRU,SYST

仅上传权限:

cmds_allowed=FEAT,REST,CWD,LIST,MDTM,MKD,NLST,PASS,PASV,PORT,PWD,QUIT,RMD,SIZE,STOR,TYPE,USER,ACCT,APPE,CDUP,HELP,MODE,NOOP,REIN,STAT,STOU,STRU,SYST


权限项详情:

commoncommands 常用命令:

# ABOR - abort a filetransfer 取消文件传输

# CWD - change workingdirectory 更改目录

# DELE - delete a remotefile 删除文件

# LIST - list remotefiles 列出目录

# MDTM - return themodification time of a file 返回文件的更新时间

# MKD - make a remotedirectory 新建文件夹

# NLST -name list of remote directory 文件夹重命名

# PASS -send password 发送密码

# PASV -enter passive mode 进入被动模式

# PORT - open a data port打开一个传输端口

# PWD - print workingdirectory 显示当前工作目录

# QUIT - terminate theconnection 退出

# RETR - retrieve aremote file 下载文件

# RMD -remove a remote directory 删除文件夹

# RNFR -rename from 重命名

# RNTO -rename to 重命名

# SITE -site-specific commands 特定的命令

# SIZE - return the sizeof a file 返回文件大小

# STOR - store a file onthe remote host 上传文件

# TYPE -set transfer type 设置传输类型

# USER -send username 发送的用户名

 

# lesscommon commands 不常用命令:

# ACCT*- send account information 发送账号信息

# APPE -append to a remote file 追加文件

# CDUP -CWD to the parent of the current directory 进入上级目录

# HELP -return help on using the server 返回帮助信息

# MODE -set transfer mode 设置传输模式

# NOOP -do nothing 等待,此命令不产生什么实际动作,它仅使服务器返回OK。

# REIN*- reinitialize the connection 重新初始化连接

# STAT -return server status 返回服务器状态

# STOU -store a file uniquely 唯一地存储文件

# STRU -set file transfer structure 设置文件传输结构

# SYST -return system type 返回系统类型

设置公共目录

创建公共目录

mkdir -p /home/vsftpuser/app/zhangsan/publish
mkdir -p /home/vsftpuser/app/zhangsan/share
mkdir -p /home/vsftpuser/app/lisi/publish
mkdir -p /home/vsftpuser/app/lisi/share 

使用mount -B -o rw,ro来将公共目录、日志目录添加至个人根目录

因为1个虚拟用户只可以配置1个目录,故需要通过mount -B来实现目录共享

mount -B -o ro /home/vsftpuser/publish /home/vsftpuser/app/zhangsan/publish
mount -B -o rw /home/vsftpuser/share /home/vsftpuser/app/zhangsan/share
mount -B -o ro /home/vsftpuser/publish /home/vsftpuser/app/lisi/publish 
mount -B -o rw /home/vsftpuser/share /home/vsftpuser/app/lisi/share 

注:mount -B 是保存在内存中,重启服务器失效,故需要将配置放入/etc/rc.local中

解除挂载命令

umount /home/vsftpuser/app/zhangsan/publish

当挂载目录的时候,如果所被挂载的目录有文件数据,那么挂载完以后原来的数据就看不见了。当解除挂载后又能看到。

优化mount挂载

方式一:直接写入/etc/rc.local文件

vim /etc/rc.local

追加内容如下:

mount -B -o ro /home/vsftpuser/publish /home/vsftpuser/app/zhangsan/publish
mount -B -o rw /home/vsftpuser/share /home/vsftpuser/app/zhangsan/share
mount -B -o ro /home/vsftpuser/publish /home/vsftpuser/app/lisi/publish 
mount -B -o rw /home/vsftpuser/share /home/vsftpuser/app/lisi/share 

保存退出。

方式二:脚本执行

#35为启动级别,10为关闭的优先级,90为启动的优先级

# touch /etc/init.d/mountVSFTPD_[虚拟用户]
vim /etc/init.d/mountVSFTPD_vsftpuser
#!/bin/bash
# chkconfig: 35 10 90
mount -B -o ro /home/vsftpuser/publish /home/vsftpuser/app/zhangsan/publish
mount -B -o rw /home/vsftpuser/share /home/vsftpuser/app/zhangsan/share
mount -B -o ro /home/vsftpuser/publish /home/vsftpuser/app/lisi/publish 
mount -B -o rw /home/vsftpuser/share /home/vsftpuser/app/lisi/share 

保存退出。

chmod +x /etc/init.d/mountVSFTPD_vsftpuser
chkconfig --add mountVSFTPD_vsftpuser
chkconfig --level 35 mountVSFTPD_vsftpuser on
chkconfig --list

查看操作日志

tail -f /var/log/vsftpd.log

汇总目录结构

- /usr/sbin/vsftpd ---- VSFTPD的主程序
- /etc/rc.d/init.d/vsftpd ---- 启动脚本
- /etc/vsftpd/vsftpd.conf ---- 主配置文件
- /etc/pam.d/vsftpd.vu ---- PAM认证文件
- /etc/vsftpd.ftpusers ---- 禁止使用VSFTPD的用户列表文件
- /etc/vsftpd.user_list ---- 禁止或允许使用VSFTPD的用户列表文件
- /var/log/vsftpd.log  ---- 日志文件

vsfpuser 目录授权

chown -R vsftpuser:vsftpuser /home/vsftpuser

3,启动ftp

# 启动
systemctl restart vsftpd

# 查看状态
systemctl status vsftpd

4,测试机上cmd使用用户登录ftp

C:\Users\liuhm>ftp 192.168.56.90
连接到 192.168.56.90。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.56.90:(none)): zhangsan ## 登录
331 Please specify the password.
密码:
230 Login successful.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd  ## 展示当前目录
257 "/"
ftp> put test.log ## 上传文件
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp: 发送 3072 字节,用时 0.003072.00千字节/秒。
ftp> ls /
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
/test.log
226 Directory send OK.
ftp: 收到 14 字节,用时 0.0014.00千字节/秒。

#测试publish和share
C:\Users\liuhm>ftp 192.168.56.90
连接到 192.168.56.90。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.56.90:(none)): zhangsan
331 Please specify the password.
密码:
230 Login successful.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
publish
share
226 Directory send OK.
ftp: 收到 19 字节,用时 0.0019.00千字节/秒。
ftp> cd publish  ## 切换到publish
250 Directory successfully changed.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
asdasd
xzcxz
226 Directory send OK.
ftp: 收到 18 字节,用时 0.0018000.00千字节/秒。
ftp> pwd
257 "/publish"
ftp> put test.log  ## 上传文件
200 PORT command successful. Consider using PASV.
553 Could not create file.  ## 上传失败文件
ftp> get xzcxz  ## 下载文件
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for xzcxz (0 bytes).
226 Transfer complete.
ftp> cd ..
250 Directory successfully changed.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
publish
share
226 Directory send OK.
ftp: 收到 19 字节,用时 0.0019.00千字节/秒。
ftp> cd share
250 Directory successfully changed.
ftp> pwd
257 "/share"
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
1.log
11.log
226 Directory send OK.
ftp: 收到 18 字节,用时 0.0018.00千字节/秒。
ftp> put test.log   ## 上传文件
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.  ## 上传成功文件
ftp: 发送 3072 字节,用时 0.003072.00千字节/秒。
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
1.log
11.log
test.log
226 Directory send OK.
ftp: 收到 28 字节,用时 0.0028.00千字节/秒。
ftp> get 1.log ## 下载文件
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for 1.log (3072 bytes).
226 Transfer complete.

5,初始用户脚本

使用步骤:
在文件/etc/vsftpd/vsftpuser 增加用户

执行下面的脚本

vi /etc/vsftpd/initFtpUser.sh  
#!/bin/sh

CURRENT_DIR=$(
   cd "$(dirname "$0")"
   pwd
)
# 特殊用户配置 大权限的用户
EXCLUDE_USER=("zhangsan")

#///
# 获取奇数行的用户名
rm -rf initFtpsuer.log
awk 'NR%2==1' vsftpuser  >> initFtpsuer.log

# 删除db
rm -rf vsftpuser.db
db_load -T -t hash -f vsftpuser vsftpuser.db
chmod 600 vsftpuser 
chmod 600 vsftpuser.db

# 创建虚拟用户权限目录
mkdir -p /etc/vsftpd/virconf

# 创建用户配置文件
for line in `cat initFtpsuer.log`
do

mkdir -p /home/vsftpuser/app/$line/publish
mkdir -p /home/vsftpuser/app/$line/share 

cat > /etc/vsftpd/virconf/$line<<EOF
local_root=/home/vsftpuser/app/$line
virtual_use_local_privs=YES
# 允许登录用户有可写权限
write_enable=YES
# 允许匿名用户上传文件权限,只在write_enable=YES时生效
anon_upload_enable=YES
# 允许匿名用户创建目录,只有在write_enable=YES时生效
anon_mkdir_write_enable=YES
# 可写
allow_writeable_chroot=YES
EOF

echo "$line 的配置文件"
cat /etc/vsftpd/virconf/$line
done

# 脚本执行
cat > /etc/init.d/mountVSFTPD_vsftpuser<<EOF
#!/bin/bash
# chkconfig: 35 10 90
EOF

for line in `cat initFtpsuer.log`
do

if [[ ! "${EXCLUDE_USER[@]}"  =~ "$line" ]] 
then
# 正常权限
echo "mount -B -o ro /home/vsftpuser/publish /home/vsftpuser/app/$line/publish" >> /etc/init.d/mountVSFTPD_lijian
echo "mount -B -o rw /home/vsftpuser/share /home/vsftpuser/app/$line/share" >> /etc/init.d/mountVSFTPD_lijian
umount /home/vsftpuser/app/$line/publish
umount /home/vsftpuser/app/$line/share
mount -B -o ro /home/vsftpuser/publish /home/vsftpuser/app/$line/publish
mount -B -o rw /home/vsftpuser/share /home/vsftpuser/app/$line/share
else 

echo "特殊权限 大权限 $line"
# 特殊权限 大权限
echo "mount -B -o rw /home/vsftpuser/publish /home/vsftpuser/app/$line/publish" >> /etc/init.d/mountVSFTPD_lijian
echo "mount -B -o rw /home/vsftpuser/share /home/vsftpuser/app/$line/share" >> /etc/init.d/mountVSFTPD_lijian
umount /home/vsftpuser/app/$line/publish
umount /home/vsftpuser/app/$line/share
mount -B -o rw /home/vsftpuser/publish /home/vsftpuser/app/$line/publish
mount -B -o rw /home/vsftpuser/share /home/vsftpuser/app/$line/share


fi	
done
echo "查看  /etc/init.d/mountVSFTPD_vsftpuser"
cat /etc/init.d/mountVSFTPD_vsftpuser
chmod +x /etc/init.d/mountVSFTPD_vsftpuser
#授权
chown -R vsftpuser:vsftpuser /home/vsftpuser

# 启动
systemctl restart vsftpd

# 查看状态
systemctl status vsftpd
cd /etc/vsftpd
chmod +x /etc/vsftpd/initFtpUser.sh 
sed -i 's/\r$//' initFtpUser.sh 
./initFtpUser.sh 
vsftpd虚拟用户是指在vsftpd服务器中创建的一种特殊类型的用户,这些用户并不是系统用户,而是通过配置文件进行管理的。虚拟用户权限目录是由服务器配置文件中的设置来确定的。在搭建vsftpd服务器时,可以使用虚拟用户来实现更灵活的用户管理和权限控制。 要创建vsftpd虚拟用户,首先需要修改vsftpd配置文件。在文件/etc/vsftpd.conf中,可以设置以下参数来配置虚拟用户: - guest_enable=YES:开启虚拟用户功能。 - guest_username=virtusers:指定虚拟用户的宿主用户。 - user_config_dir=/etc/vsftpd/vuser_conf:指定虚拟用户配置文件目录。 然后,需要创建虚拟用户配置文件。在目录/etc/vsftpd/vuser_conf/中,可以为每个虚拟用户创建一个单独的配置文件,例如/etc/vsftpd/vuser_conf/vuser1。在这个配置文件中,可以设置虚拟用户权限目录等信息。 接下来,需要创建虚拟宿主用户。虚拟宿主用户是一个系统用户,用于承载所有虚拟用户权限。可以使用以下命令创建虚拟宿主用户: ``` useradd -d /data/ftproot virtusers -s /sbin/nologin ``` 其中,-d参数指定虚拟宿主用户的home目录,-s参数指定系统用户登录的shell,使用/sbin/nologin可以增强安全性。 最后,需要创建相关的工作目录设置权限。可以根据需要创建ftp的工作目录,例如/data/ftproot,然后设置相应的权限。 通过以上步骤,就可以成功创建和配置vsftpd虚拟用户,实现使用虚拟用户登录ftp的功能。请根据实际需求进行相应的配置和调整。[1][2][3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuhm~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值