使用Docker实现vsftpd配置——用户验证访问&上传

36 篇文章 2 订阅

你可能需要看一下:Docker系列-容器修改后保存为镜像并导出

1、服务安装

1.1、容器创建

创建一个容器

docker run -tid -p 20:20 -p 21:21 -v /ftp/:/home/liumou/ftp/ --name ftp_rw debian:10 /bin/bash

进入容器

docker exec -ti ftp_rw /bin/bash

1.2、安装vsftpd

首先配置源,配置教程:Debian10常用国内源更换镜像站汇总
安装之前需要先:

apt update

然后再安装

apt install -y vsftpd

安装过程(部分):

root@bdbb277fd90b:/# apt install vftpd
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package vftpd
root@bdbb277fd90b:/# apt update
Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:2 http://mirrors.ustc.edu.cn/debian buster InRelease [121 kB]
Get:3 http://security.debian.org/debian-security buster/updates/main Sources [152 kB]
Get:4 http://mirrors.ustc.edu.cn/debian buster-updates InRelease [51.9 kB]
Get:5 http://mirrors.ustc.edu.cn/debian buster-backports InRelease [46.7 kB]
Get:6 http://mirrors.ustc.edu.cn/debian buster/main Sources [7841 kB]
Get:7 http://security.debian.org/debian-security buster/updates/main arm64 Packages [237 kB]
Get:8 http://mirrors.ustc.edu.cn/debian buster/main arm64 Packages [7736 kB]     
Get:9 http://mirrors.ustc.edu.cn/debian buster-updates/main Sources [3716 B]           
Get:10 http://mirrors.ustc.edu.cn/debian buster-updates/main arm64 Packages [7848 B]

只要网络没问题就能安装成功

2、配置修改

修改之前先备份

cp /etc/vsftpd.conf /etc/vsftpd.conf.bak

2.1、配置文件内容

使用下面的命令进行配置vsftp服务

cat <<EOF > /etc/vsftpd.conf 
listen=YES
listen_ipv6=NO
anonymous_enable=NO
local_enable=YES
write_enable=YES
anon_upload_enable=YES
anon_mkdir_write_enable=YES
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
chroot_local_user=YES
chroot_list_enable=YES
allow_writeable_chroot=YES
chroot_list_file=/etc/vsftpd.chroot_list
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO

tcp_wrappers=YES
utf8_filesystem=YES
local_root=/home/liumou/ftp
EOF

2.2、用户列表

touch /etc/vsftpd.chroot_list

PS:如果希望某个用户可以登陆访问其他目录,请写入该文件

3、权限配置

3.1、用户创建

创建用户目录

mkdir -p /home/liumou/ftp/

创建用户

useradd -d /home/liumou/ liumou

设置密码

passwd liumou

执行过程:

root@b31e7fa60187:/# useradd -d /home/liumou/liumou
root@b31e7fa60187:/# passwd liumou
New password: 
Retype new password: 
passwd: password updated successfully

然后对这个目录进行权限的设置,命令如下:

chown -R liumou:liumou /home/liumou/ftp/
chmod -R 755 /home/liumou/ftp/

执行过程:

root@b31e7fa60187:/# chown -R liumou:liumou /home/liumou/ftp/
root@b31e7fa60187:/# chmod -R 755 /home/liumou/ftp/

配置结果:

root@b31e7fa60187:/# ls -l /home/liumou/
total 4
drwxr-xr-x 2 liumou liumou 4096 Nov  2 02:12 ftp
root@b31e7fa60187:/# 

3.2、配置生效

配置完成之后,我们需要对服务进行重启才能使其生效,方法如下:

cd /etc/init.d/
./vsftpd restart

执行结果:

root@b31e7fa60187:/# cd /etc/init.d/
root@b31e7fa60187:/etc/init.d# ./vsftpd restart
[....] Stopping FTP server: vsftpdNo /usr/sbin/vsftpd found running; none killed.
. ok 
[ ok ] Starting FTP server: vsftpd.
root@b31e7fa60187:/etc/init.d# 

4、宿主机配置

由于我们是使用的容器进行部署,并且映射了宿主目录,所以我们还需要对宿主目录进行权限配置,命令如下:
首先退出容器,回到宿主机,命令如下:

exit

然后对映射目录进行赋权

chmod -R 755 /ftp/
chown root:root /ftp/

PS:采用容器部署的好处就是一次配置,随处可用,并且对于宿主目录而言,只需要把需要分享的路径进行挂载然后赋权即可,最多也就:导入Docker镜像->创建容器->赋权,三步完成,还可以根据不同需要制定不同的镜像,

执行过程:

root@b31e7fa60187:/etc/init.d# exit
exit
root@l:~# chmod -R 755 /ftp/
root@l:~# chown root:root /ftp/
root@l:~# 

5、登录验证

首先查询当前容器IP地址:

docker inspect ftp_rw | grep \"IPAddress\"

查询结果:

root@l:~# docker inspect ftp_rw | grep \"IPAddress\"
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
root@l:~# 

创建文件进行验证:

root@xxzx-PC:~# touch /ftp/t.txt
root@xxzx-PC:~# ls /ftp/
新建文件夹  t.txt
root@xxzx-PC:~# 

然后在宿主机通过浏览器进行访问验证:

ftp://172.17.0.2

提示输入密码
在这里插入图片描述然后登录成功
在这里插入图片描述

6、上传验证

浏览器登录成功之后,我们使用资源管理器进行登录

ftp://172.17.0.2

提示输入用户密码
在这里插入图片描述然后登录成功,但是出现了意外现象,此时登录目录并不是主目录
在这里插入图片描述

7、限制用户登录目录

7.1、重启验证

cd /etc/init.d/
./vsftpd restart

执行结果:

root@b31e7fa60187:/home/liumou/ftp# cd /etc/init.d/
root@b31e7fa60187:/etc/init.d# ./vsftpd restart
[ ok ] Stopping FTP server: vsftpd.
[ ok ] Starting FTP server: vsftpd.
root@b31e7fa60187:/etc/init.d# 

然后再次使用资源管理器进行登录:

ftp://172.17.0.2

在这里插入图片描述此时,直接进入了我们配置的主目录,完美结束?No,因为我们是容器部署的,而容器部署的意义则在于一次部署,随处可用。
在这里插入图片描述

8、添加/修改用户名

如果希望使用其他用户名进行访问,那么请根据下面的方法进行配置

8.1、创建用户

以下操作请进入容器进行,首先输入下面的命令(没错,这是命令)

echo '#!/bin/bash
##add ftp user
echo 'please Enter USER name'
read user
dir=/home/liumou/$user
mkdir -p $dir
useradd -g ftp -d $dir $user
passwd $user
chown -R $user:ftp $dir
chmod -R 755 $dir' > /add_ftp-user.sh

执行结果:

root@1c75ddedd2f3:~# more /add_ftp-user.sh 
#!/bin/bash
##add ftp user
echo please Enter USER name
read user
dir=/home/liumou/$user
mkdir -p $dir
useradd -g ftp -d $dir $user
passwd $user
chown -R $user:ftp $dir
chmod -R 755 $dir
cd cd /etc/init.d/
./vsftpd restart 
if [[ $? -eq 0 ]];then
	echo Modification successful
else
	echo Modification failed
fi
root@1c75ddedd2f3:~# 

然后执行下面的命令

在这里插入代码片
bash /add_ftp-user.sh

执行结果:

root@1c75ddedd2f3:~# bash /add_ftp-user.sh 
please Enter USER name
liu
New password: 
Retype new password: 
passwd: password updated successfully
root@1c75ddedd2f3:~# 

然后重新访问ftp,输入新的用户名即可

所以你需要先看下面这篇文章:
Docker系列-容器修改后保存为镜像并导出
上面这篇文章看完之后,相信你已经成功导出镜像了,那么请看最后一篇吧!
Docker系列-自定义FTP服务Docker镜像,实现一次部署,随处可用

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

坐公交也用券

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

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

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

打赏作者

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

抵扣说明:

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

余额充值