返回列表 发帖

CentOS8_在Docker中安装vsftpd

笺注:Docker的安装可参考 CentOS8_在Docker中使用Nginx的反向代理


查看Docker的版本信息:
[root@centos8 ~]# docker -v
Docker version 20.10.21, build baeda1f
[root@centos8 ~]#



从公网下载镜像:( 以下是下载Nginx/1.18的镜像 )
[root@centos8 ~]# docker pull nginx:1.18
1.18: Pulling from library/nginx
f7ec5a41d630: Pull complete
0b20d28b5eb3: Pull complete
1576642c9776: Pull complete
c12a848bad84: Pull complete
03f221d9cf00: Pull complete
Digest: sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0
Status: Downloaded newer image for nginx:1.18
docker.io/library/nginx:1.18
[root@centos8 ~]#


查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        1.18      c2c45d506085   23 months ago   133MB
[root@centos8 ~]#


使用镜像“nginx:1.18”创建并启动容器“Vsftpd_1”:
[root@centos8 ~]# docker run -itd --name Vsftpd_1 -p 21:21 -v /opt/share:/usr/share/nginx/html nginx:1.18
8c78aca4293d5f6453a23ac6298decf381a627bc75dbb89c6e81fb04ac6e2672
[root@centos8 ~]#

注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 第一个21是宿主机的端口,暴露给外部直接访问;第二个21是容器的端口。
-v 挂载目录 : 这里是把宿主机的目录/opt/share挂载到容器的目录/usr/share/nginx/html;假如目录不存在,就会自动创建。
nginx:1.18 : 镜像名称:版本号


在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                       NAMES
8c78aca4293d   nginx:1.18   "/docker-entrypoint.…"   42 seconds ago   Up 39 seconds   0.0.0.0:21->21/tcp, :::21->21/tcp, 80/tcp   Vsftpd_1
[root@centos8 ~]#

注释:宿主机的TCP 21端口,映射到容器“Vsftpd_1”的TCP 21端口。



###

进入容器“Vsftpd_1”:
[root@centos8 ~]# docker exec -it Vsftpd_1 /bin/bash
root@8c78aca4293d:/#


查看容器“Vsftpd_1”的系统版本信息:
root@8c78aca4293d:/# cat /etc/issue
Debian GNU/Linux 10 \n \l

root@8c78aca4293d:/# more /etc/debian_version
10.9
root@8c78aca4293d:/# uname -r
4.18.0-193.el8.x86_64
root@8c78aca4293d:/#


查看容器“Vsftpd_1”的Nginx版本:
root@8c78aca4293d:/# nginx -v
nginx version: nginx/1.18.0
root@8c78aca4293d:/#

root@8c78aca4293d:/# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.3.0 (Debian 8.3.0-6)
built with OpenSSL 1.1.1d  10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.18.0/debian/debuild-base/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
root@8c78aca4293d:/#



在容器“Vsftpd_1”中安装vsftpd:
root@8c78aca4293d:/# whoami
root
root@8c78aca4293d:/# apt-get update

root@8c78aca4293d:/# apt-get -y install vsftpd


查看vsftpd的版本:
root@8c78aca4293d:~# vsftpd -v
vsftpd: version 3.0.3
root@8c78aca4293d:~#



建立FTP用户:(使用vsftpd用户验证)
root@8c78aca4293d:/# groupadd ftp_01
root@8c78aca4293d:/#

root@8c78aca4293d:/# useradd -g ftp_01 -d /usr/share/nginx/html -s /bin/bash happy
root@8c78aca4293d:/# passwd happy
New password:
Retype new password:
passwd: password updated successfully
root@8c78aca4293d:/#

root@8c78aca4293d:/# useradd -g ftp_01 -d /usr/share/nginx/html -s /bin/bash ftp_user_1
root@8c78aca4293d:/# passwd ftp_user_1
New password:
Retype new password:
passwd: password updated successfully
root@8c78aca4293d:/#


设置FTP目录的权限:(属主有读写的权限;组员只可以读,不可以写;其他人不可以读写)
root@8c78aca4293d:/# ls -ld /usr/share/nginx/html
drwxr-xr-x. 2 root root 6 Mar  7 00:47 /usr/share/nginx/html
root@8c78aca4293d:/#

root@8c78aca4293d:/# chown -R happy:ftp_01 /usr/share/nginx/html
root@8c78aca4293d:/#
root@8c78aca4293d:/# chmod -R 750 /usr/share/nginx/html
root@8c78aca4293d:/#
root@8c78aca4293d:/# ls -ld /usr/share/nginx/html
drwxr-x---. 2 happy ftp_01 6 Mar  7 00:47 /usr/share/nginx/html
root@8c78aca4293d:/#



###

在宿主机上修改容器“Vsftpd_1”的vsftpd的主配置文件:
[root@centos8 ~]# docker cp Vsftpd_1:/etc/vsftpd.conf /root
[root@centos8 ~]# vi vsftpd.conf
追加:
userlist_deny=NO
userlist_enable=YES
userlist_file=/etc/allowed_users
seccomp_sandbox=NO
local_enable=YES
pasv_promiscuous=YES
write_enable=YES


在宿主机上,往容器“Vsftpd_1”上传修改好的文件:
[root@centos8 ~]# docker cp /root/vsftpd.conf Vsftpd_1:/etc/vsftpd.conf
[root@centos8 ~]#



再往容器“Vsftpd_1”上传以下文件:(允许登录FTP的系统用户)
[root@centos8 ~]# cat allowed_users
happy
ftp_user_1

[root@centos8 ~]#

[root@centos8 ~]# docker cp /root/allowed_users Vsftpd_1:/etc
[root@centos8 ~]#



在宿主机的防火墙firewalld打开TCP 21端口:
firewall-cmd --zone=public --add-port=21/tcp --permanent
firewall-cmd --reload

查看防火墙所有打开的端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
21/tcp
[root@centos8 ~]#



###

在容器“Vsftpd_1”里重启vsftpd服务:(vsftpd服务不会自动启动)
root@8c78aca4293d:~# /etc/init.d/vsftpd restart
[....] Stopping FTP server: vsftpd. ok
[....] Starting FTP server: vsftpd. ok
root@8c78aca4293d:~#


查看vsftpd的状态:
root@8c78aca4293d:~# service vsftpd status
[....] FTP server is running. ok
root@8c78aca4293d:~#



创建任务计划:
root@8c78aca4293d:~# apt-get -y install vim

root@8c78aca4293d:~# crontab -e -u root
追加:
*/1 * * * * chown -R happy:ftp_01 /usr/share/nginx/html
*/1 * * * * chmod -R 750 /usr/share/nginx/html


要手动启动任务计划服务:(cron服务不会自动启动)
root@8c78aca4293d:~# service cron status
[....] cron is not running ... failed!
root@8c78aca4293d:~#
root@8c78aca4293d:~# service cron start
[....] Starting periodic command scheduler: cron. ok
root@8c78aca4293d:~#
root@8c78aca4293d:~# service cron status
[....] cron is running. ok
root@8c78aca4293d:~#



###

Windows客户端使用FlashFXP连接容器“Vsftpd_1”的FTP:(使用vsftpd用户验证)
图片1.png
2023-3-8 13:22


图片2.png
2023-3-8 13:22


实验至此,对于容器“Vsftpd_1”的FTP目录里面的所有内容,happy可以下载、上传、修改、删除;ftp_user_1只可以下载。(支持中文)














######
######

使用镜像“nginx:1.18”创建并启动容器“Vsftpd_2”:
[root@centos8 ~]# docker run -itd --name Vsftpd_2 -p 2121:21 -v /opt/share:/usr/share/nginx nginx:1.18
5fd9aa7184390186c93444a60fefc902158dec2536b6135e82381f7aa82b5b89
[root@centos8 ~]#

注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 2121是宿主机的端口,暴露给外部直接访问;21是容器的端口。
-v 挂载目录 : 这里是把宿主机的目录/opt/share挂载到容器的目录/usr/share/nginx;假如目录不存在,就会自动创建。
nginx:1.18 : 镜像名称:版本号


在宿主机查看所有的容器:(无论是否正在运行)
[root@centos8 ~]# docker ps -a
CONTAINER ID   IMAGE        COMMAND                  CREATED              STATUS              PORTS                                           NAMES
5fd9aa718439   nginx:1.18   "/docker-entrypoint.…"   About a minute ago   Up About a minute   80/tcp, 0.0.0.0:2121->21/tcp, :::2121->21/tcp   Vsftpd_2
8c78aca4293d   nginx:1.18   "/docker-entrypoint.…"   2 hours ago          Up 2 hours          0.0.0.0:21->21/tcp, :::21->21/tcp, 80/tcp       Vsftpd_1
[root@centos8 ~]#

注释:宿主机的TCP 2121端口,映射到容器“Vsftpd_2”的TCP 21端口。



###

进入容器“Vsftpd_2”:
[root@centos8 ~]# docker exec -it Vsftpd_2 /bin/bash
root@5fd9aa718439:/#


在容器“Vsftpd_2”中安装vsftpd:
root@5fd9aa718439:/# apt-get update

root@5fd9aa718439:/# apt-get install vsftpd -y


查看vsftpd的版本:
root@5fd9aa718439:~# dpkg -l | grep vsftpd
ii  vsftpd                     3.0.3-12                    amd64        lightweight, efficient FTP server written for security
root@5fd9aa718439:~#



###

在宿主机上修改容器“Vsftpd_2”的vsftpd的主配置文件:
[root@centos8 ~]# docker cp Vsftpd_2:/etc/vsftpd.conf ./
[root@centos8 ~]# vi vsftpd.conf
anonymous_enable=NO
修改为:(使用vsftpd匿名用户)
anonymous_enable=YES


在宿主机上,往容器“Vsftpd_2”上传修改好的文件:
[root@centos8 ~]# docker cp ./vsftpd.conf Vsftpd_2:/etc
[root@centos8 ~]#


###

在容器“Vsftpd_2”里重启vsftpd服务:(vsftpd服务不会自动启动)
root@5fd9aa718439:/# service vsftpd restart
[....] Stopping FTP server: vsftpd. ok
[....] Starting FTP server: vsftpd. ok
root@5fd9aa718439:/#

查看vsftpd的状态:
root@5fd9aa718439:/# /etc/init.d/vsftpd status
[....] FTP server is running. ok
root@5fd9aa718439:/#


FTP匿名用户的默认根目录:
root@5fd9aa718439:~# cd /srv/ftp/
root@5fd9aa718439:/srv/ftp# ls
root@5fd9aa718439:/srv/ftp#

root@5fd9aa718439:/srv/ftp# echo 'Welcome to zhuohua.' > 1.txt
root@5fd9aa718439:/srv/ftp#

root@5fd9aa718439:/srv/ftp# chmod -R 755 /srv/ftp/
root@5fd9aa718439:/srv/ftp



###

在宿主机的防火墙firewalld打开TCP 2121端口:
firewall-cmd --zone=public --add-port=2121/tcp --permanent
firewall-cmd --reload

查看防火墙所有打开的端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
21/tcp 2121/tcp
[root@centos8 ~]#



###

Windows客户端使用FlashFXP连接容器“Vsftpd_2”的FTP:(使用vsftpd匿名用户)
图片3.png
2023-3-8 13:25


图片4.png
2023-3-8 13:25


备注:
FTP匿名用户是直接登录到/srv/ftp/,且不能离开这个目录
FTP匿名用户默认是允许从FTP服务器下载文件和文件夹的
FTP匿名用户无法创建文件和文件夹;不能上传东西,也不能删除东西





相关文章:
vsftpd修改端口
本地备份站点目录和数据库+crontab计划任务

CentOS8_在Docker中安装Samba
CentOS8_在Docker中安装PureFTPd
CentOS8_在Docker的容器内自动启动服务

返回列表