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,输入新的用户名即可