Linux最全linux搭建ftp服务-ftp服务器搭建和ftp客户端使用(1),2024年教你增加拿到BAT等大厂offer几率

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以点击这里获取!

Init context: system_u:system_r:init_t:s0

/usr/sbin/sshd unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

File contexts:

Controlling terminal: unconfined_u:object_r:user_devpts_t:s0

/etc/passwd system_u:object_r:passwd_file_t:s0

/etc/shadow system_u:object_r:shadow_t:s0

/bin/bash system_u:object_r:shell_exec_t:s0

/bin/login system_u:object_r:login_exec_t:s0

/bin/sh system_u:object_r:bin_t:s0 -> system_u:object_r:shell_exec_t:s0

/sbin/agetty system_u:object_r:getty_exec_t:s0

/sbin/init system_u:object_r:bin_t:s0 -> system_u:object_r:init_exec_t:s0

/usr/sbin/sshd system_u:object_r:sshd_exec_t:s0

[root@centos76_2 ~]#

配置yum源【公网跳过该步骤】


首先上传一个和操作系统版本一致的镜像到服务器,我已经上传至/home目录;

[root@centos76_2 home]# mkdir centos # 创建一个文件用来挂载镜像

[root@centos76_2 home]# mount CentOS-7.6-x86_64-DVD-1810.iso centos # 挂载镜像

[root@centos76_2 home]# df -h | tail -n 1 #查看挂载

/dev/loop0 4.3G 4.3G 0 100% /home/centos

[root@centos76_2 home]#

[root@centos76_2 yum.repos.d]# ls #默认会有很多文件夹

CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo

[root@centos76_2 yum.repos.d]# mkdir bak #创建一个文件存放现有文件

[root@centos76_2 yum.repos.d]# mv * bak # 移动过去

[root@centos76_2 yum.repos.d]#

[root@centos76_2 yum.repos.d]# vim centos.repo # 创建一个新的yum源文件

[root@centos76_2 yum.repos.d]# cat centos.repo # 将下列内容复制进去保存即可

[7.6-bendi]

name=ccx_settin

baseurl=file:///home/centos

gpgcheck=0

enable=1

[root@centos76_2 yum.repos.d]#

[root@centos76_2 yum.repos.d]# yum repolist # 查看,repolist有内容即可

Loaded plugins: fastestmirror, langpacks

Loading mirror speeds from cached hostfile

repo id repo name status

7.6-bendi ccx_settin 4,021

repolist: 4,021

[root@centos76_2 yum.repos.d]#

安装vsftp服务


下面命令照着执行一遍就好了

[root@centos76_2 yum.repos.d]# yum -y install vsftpd # 安装

[root@centos76_2 yum.repos.d]# systemctl is-active vsftpd #查看状态

unknown

[root@centos76_2 yum.repos.d]# systemctl start vsftpd # 启动

[root@centos76_2 yum.repos.d]# systemctl is-active vsftpd

active

[root@centos76_2 yum.repos.d]#

[root@centos76_2 yum.repos.d]# systemctl enable vsftpd # 加入开机启动

Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

[root@centos76_2 yum.repos.d]#

vsftp参数详细说明


vsftp的配置文件为:/etc/vsftpd/vsftpd.conf

[root@centos76_2 yum.repos.d]# cat /etc/vsftpd/vsftpd.conf | grep -v “^#” #查看默认开启的服务

anonymous_enable=YES #不允许匿名访问

local_enable=YES #默认启用

write_enable=YES #允许写入

local_umask=022 # 默认本地用户上传文件权限755

dirmessage_enable=YES #显示每个目录下的文件信息

xferlog_enable=YES # 日志启用

connect_from_port_20=YES # 主动请求的数据端口

xferlog_std_format=YES #。。。

listen=NO #服务将自己监听下面内容【listen_ipv6=YES】

listen_ipv6=YES #上面listen为NO,这个没作用

pam_service_name=vsftpd # 设置PAM认证模块使用的预览名称

userlist_enable=YES #所有文件列出用户

tcp_wrappers=YES # 服务端和客户端访问控制策略【服务级别的一种防火墙】

#除了上面自带的功能外,常用重要配置还有如下几个

chown_uploads=YES #所有匿名用户上传的文件所属用户将会被改成chown_username

chown_username=whoname #匿名上传的所属用户名是whoname【需要和上面一条配套使用】

xferlog_stad_format=YES #启用日志

xferlog_file=/var/log/xferlog#启用的日志文件【需要和上面一条配套使用】

local_root=/data/ccx # 修改默认家路径

idle_session_timeout=600 #空闲连接超时

data_connection_timeout=120 #数据连接超时

nopriv_user=ftpsecure #当服务器运行与最底层时使用的用户名

chroot_local_user=YES #不可以切换到其他用户

chroot_list_file=/etc/vsftpd/chroot_list #只允许该文件中的用户登陆ftp,和上面一个配套使用

[root@centos76_2 yum.repos.d]#

  • 上面基本是常用的配置了,测试demo中我基本上只对上面中做说明,但只要知道怎么配置以后,下面的功能要的直接拿来用就行了;

  • 在这儿整理了更多 更详细的内容,编辑文件均在:/etc/vsftpd/vsftpd.conf里面

  • anonymous_enable=YES /允许匿名访问 12行

  • local_enable=YES /允许本地用户访问(/etc/passwd中的用户) 15行

  • write_enable=YES /允许写入权限,包括修改,删除 18行

  • local_umask=022 /本地用户文件上传后的权限是-rw-r–r-- 22行

本地用户上传最高权限为666,默认权限为666-022=644,即local_umask=022

  • anon_umask=022 /默认没有这一项【了解即可】,匿名用户上传后的权限是-rw-------。匿名用户上传的默认权限为600,即anon_umask=066,匿名用户上传最大权限是666,这里权限是用666-022=644方法得来的-rw-r–r-

  • anon_world_readable_only=YES /允许匿名用户浏览,下载文件,默认没有这一项,只有在虚拟用户的配置文件里才有用

  • anon_upload_enable=YES /默认注释掉了,允许匿名用户上传 27行

  • anon_mkdir_write_enable=YES /默认注释掉了,允许匿名用户上传/建立目录 31行

  • anon_other_write_enable=YES /默认没有这一项,允许匿名用户具有建立目录,上传之外的权限,如重命名,删除

  • dirmessage_enable=YES /是否显示目录说明文件, 默认是YES 但需要手工创建.message文件,这个.message,只有用命令登陆或者用工具,才可以看见,他不是一个弹出对话框,而是一段字符,如在pub下建立一个.message,那么在客户端进入pub目录时就会显示.message文档中的内容 35行

  • xferlog_enable=YES /记录使用者所有上传下载信息 38行

  • chown_uploads=YES /默认注释了,开启匿名用户上传用户映射,和下面一行一起使用 48行

  • chown_username=whoever/默认注释了,将匿名用户上传的文件的用户映射为whoever用户 49行

  • xferlog_file=/var/log/vsftpd.log/默认注释掉了这一项 51行

#将上传下载信息记录到/var/log/vsftpd.log中

  • connect_from_port_20=YES#确保ftp-datad 数据传送使用port 20(20号端口)

  • idle_session_timeout=600 /默认注释掉了这一项 58行

#如果使用者600秒没有动作,则强制离线,也就是指令超时时间,

  • data_connection_timeout=120 /如果 client与 Server 间的数据传送在 120 秒内都无法传送成功,那 Client的联机就会被我们的 vsftpd 强制剔除! 62行

  • ftpd_banner=Welcome to blahFTP service. /ftp的欢迎信息 84行

  • banner_file=/etc/vsftpd/banner_file.txt /默认没有这一项,ftp的欢迎信息

#欢迎信息存放在banner_file.txt文件中,如果添加中文欢迎信息,就需要在xp中将此文件编辑好后上传到/etc/vsftpd 目录中。如果ftpd_banner和banner_file同时设置,那么banner_file将覆盖ftpd_banner的设置。

  • Listen_address=10.1.6.0 /默认没有这一项,禁止10.1.6.0段访问此FTP

  • chroot_list_enable=YES /禁固宿主目录,默认这一项注释掉了 95行

#限制使用者不能离开家目录,例如blue登陆后位于/home/blue下,设置该选项后,他不可以转到/home/blue的上层目录,如/bin, /usr,/opt…etc。

  • chroot_list_file=/etc/vsftpd/chroot_list/默认这一项注释掉了

#与上条同时使用,设置要禁固宿主目录的用户的配置文件为/etc/vsftpd/chroot_list 建立文本文件/etc/vsftpd/chroot_list,写入要禁固宿主目录的用户,一行一个。当chroot_list_enable=YES时,则禁固chroot_list文件里面的用户的宿主目录,而为NO时chroot_list文件里面的用户不禁固宿主目录。

  • tcp_wrappers=YES /默认没有这一项

#支持tcp_wrappers,限制访问(/etc/hosts.allow,/etc/hosts.deny)

  • xferlog_std_format=YES

  • listen=YES /109行

#使用standalone启动vsftpd,而不是super daemon(xinetd)控制它 (vsftpd推荐使用standalone方式) 108行

  • listen_port=21 /默认没有这一行 #ftp监听端口

  • pam_service_name=vsftpd /116行

#PAM所使用的名称.同userlist_*一样限制用户登陆,不同的是userlist_*在进行密码验证之前拒绝用户登陆,pam是在密码验证之后拒绝登陆.(提示密码错误) 用户列表默认存放在/etc/vsftpd/ftpusers中,一行一个. (可通过/etc/pam.d/vsftpd重定向用户列表存放文件)

#获得 /etc/pam.d/vsftpd:

#[root@LFSvsftpd-2.0.1]#cp RedHat/vsftpd.pam/etc/pam.d/vsftpd

  • userlist_enable=YES/ 为yes时, /etc/vsftpd/user__list文件中的用户将不能访问vsftpd服务器 117行

  • userlist_deny=YES /默认没有这一项,与下面这条参数:userlist_file=/etc/vsftpd/localuser_list结合使用。

userlist_file=/etc/vsftpd/localuser_list /默认没有这一项, 与上面参数:userlist_deny=YES结合使用此参数用,来改变存放可以登录或禁止登录的账号,suerlist_deny为YES时,这里面的用户则不能登录,当为NO时,这里面的用户可以登录,和/etc/vsftpd/user_list是一个意思

  • max_clients=100 /默认没有这一项

#最大用户在线数量

  • max_per_ip=2/默认没有这一项

#每ip最大线程

  • anon_max_rate=30000 /默认没有这一项

#匿名用户最大传输速度 单位: bytes/秒

  • local_max_rate=50000 /默认没有这一项

#本地用户最大传输速度 单位:bytes/秒

  • user_config_dir=/etc/userconf /默认没有这一项

#个别用户配置目录(用来设定特殊帐号),例如我想让blue这个用户的传输速度是100KB

就可以在/etc/userconf/下创建文本文件blue(与用户名相同),加入local_max_rate=100000即可 /默认没有这一项

  • anon_root=/var/ftp /默认没有这一项

#设定匿名用户登陆后所在的目录

  • local_root=/var/local_user /默认没有这一项

#设定所有本地用户登陆后的目录,如不设置此项,则本地用户登陆后位于各自家目录下。

  • use_localtime=YES/默认没有这一项

#使用本地时间而不是GMT

  • nopriv_user=vsftpd/默认没有这一项

指定vsftpd服务的运行帐户,不指定时使用ftp

vsftp配置


前提准备

当前服务端虚拟机ip为:192.168.59.129

vsftp默认存放路径地址为:/var/ftp/pub然后在这个文件中继续创建文件夹或文件即可。

在这个路径下创建几个文件【file为文件夹,test为文件】

[root@centos76_2 ~]# cd /var/ftp/pub/

[root@centos76_2 pub]# ls

[root@centos76_2 pub]# mkdir file{1…3}

[root@centos76_2 pub]# touch test{1…3}

[root@centos76_2 pub]# ls

file1 file2 file3 test1 test2 test3

[root@centos76_2 pub]#

[root@centos76_2 pub]# echo cat << e > test1

test1

hello test1

e

[root@centos76_2 pub]# echo cat << e > test2

test2

god test2

e

[root@centos76_2 pub]#

打开就可以看到了哦【不要急打开方式这些,我后面会做说明,只是让你知道而已】

在这里插入图片描述

注:其实路径用/var/ftp就可以了,但因为pub是默认生成的,也代表了公共文件,所以一般默认放/var/ftp/pub里面了,也可以在/var/ftp里面创建文件,和pub一样作为基层文件使用的,如下图

在这里插入图片描述

服务检查

下面3项如果有问题,会导致ftp无法正常使用。

  • 1、检查 vsftpd服务是不是正常开启

  • 2、检查防火墙是否关闭【如果防火墙有放开策略了,关不关无所谓】

  • 3、selinux是否关闭

[root@centos76_2 pub]# systemctl is-active vsftpd

active

[root@centos76_2 pub]# systemctl is-active fir

firewalld.service firstboot-graphical.service

[root@centos76_2 pub]# systemctl is-active fir

firewalld.service firstboot-graphical.service

[root@centos76_2 pub]# systemctl is-active firewalld.service

unknown

[root@centos76_2 pub]# getenforce

Permissive

[root@centos76_2 pub]#

所有用户生效的常用功能


对于所有用户默认生效一般也就能否上传和能否删除而已,其他就不多做说明了。

上传功能配置

未配置时是无法拷贝文件的

在这里插入图片描述

  • 1、 修改配置文件/etc/vsftpd/vsftpd.conf相关选项

[root@centos76_2 pub]# vim /etc/vsftpd/vsftpd.conf

anon_umask=022 #增加

anon_upload_enable=YES #取消注释,在29行

anon_mkdir_write_enable=YES #取消注释,这33行

在这里插入图片描述

  • 2、重启vsftpd服务

[root@centos76_2 pub]# systemctl restart vsftpd

  • 3、修改目录/var/ftp/pub权限,给目录写权限

[root@centos76_2 pub]# chmod 777 /var/ftp/pub/

  • 4、上传文件测试

Windows打开我的电脑,文件路径处输入ftp://ip,进入pub目录底下上传【拷贝】文件

在这里插入图片描述

删除功能配置

配置前删除是会报错的

在这里插入图片描述

1、修改配置文件/etc/vsftpd/vsftpd.conf相关选项

[root@centos76_2 pub]# vim /etc/vsftpd/vsftpd.conf

#最后一行添加如下内容

anon_other_write_enable=YES

[root@centos76_2 pub]# tail -n 1 /etc/vsftpd/vsftpd.conf

anon_other_write_enable=YES

[root@centos76_2 pub]#

2、重启vsftpd服务测试

[root@centos76_2 pub]# systemctl restart vsftpd

3、删除文件测试

Windows打开我的电脑,文件路径处输入ftp://ip,进入pub目录底下删除文件

在这里插入图片描述

本地用户生效配置


  • 不知道有没有必要解释下本地用户生效的意思

  • 就是说 下面功能仅对 服务端上的用户生效,不是服务端上的用户名,一律阻止【不允许访问】

  • 查看本地用户,目录:/etc/passwd

在服务端虚拟机上查看,这种就是普通用户

[root@centos76_2 pub]# tail -n 3 /etc/passwd

ccx❌1000:1000:ccx:/home/ccx:/bin/bash

ccx1❌1001:1001::/home/ccx1:/bin/bash

ccx2❌1002:1002::/home/ccx2:/bin/bash

[root@centos76_2 pub]#

创建本地ftp用户

  • useradd username -s /sbin/nologin# 创建 只允许ftp登陆的用户名

  • 注: 查看路径为:/etc/passwd,如下面ccx和ccx2用户编译器为:/sbin/nologin

[root@centos76_2 ccx]# tail -n 4 /etc/passwd

ccx❌1000:1000::/home/ccx:/sbin/nologin

ccx1❌1001:1001::/home/ccx1:/bin/bash

ccx2❌1002:1002::/home/ccx2:/sbin/nologin

ccx3❌1003:1003::/home/ccx3:/bin/bash

[root@centos76_2 ccx]#

  • 如果无法通过/sbin/nologin用户登陆,一直报530认证失败,但普通用户却可以正常登陆

[root@centos76_1 ~]# ftp 192.168.59.129

Connected to 192.168.59.129 (192.168.59.129).

220 (vsFTPd 3.0.2)

Name (192.168.59.129:root): ccx

331 Please specify the password.

Password:

530 Login incorrect.

Login failed.

原因:vsftpd默认会检查用户的shell,如果用户的shell在/etc/shells没有记录,则无法登陆ftp

解决办法:在服务端的/etc/shells文件里面添加用户的shell(解释器)

vim /etc/shells #添加一行/sbin/nologin即可

在这里插入图片描述

关闭匿名登陆功能

  • 1、修改主配置文件/etc/vsftpd/vsftpd.conf开启本地用户权限(关闭匿名用户)

[root@centos76_2 pub]# vim /etc/vsftpd/vsftpd.conf

#将下面这个的YES改为NO

anonymous_enable=NO

上面配置文件中的YES改为NO重启vsftpd服务以后,再次访问就需要让登陆了(Windows访问)。

在这里插入图片描述

  • 2、修改权限

如果你是能够链接到FTP 但是看不到文件 上传和下载都没有办法做出操作,再做下面操作

执行getsebool -a | grep ftp,可以看到下面红圈中2个为off状态

在这里插入图片描述

执行2个命令将她们开启:

[root@centos76_2 pub]# setsebool -P allow_ftpd_full_access on

[root@centos76_2 pub]# setsebool -P sftp_home_dir on

开启后状态就为on了

在这里插入图片描述

开启上传、下载、删除功能

[root@centos76_2 ccx]# vim /etc/vsftpd/vsftpd.conf

write_enable=YES

[root@centos76_2 ccx]# systemctl restart vsftpd

注:关闭匿名登陆以后,默认目录不在是/var/ftp/put了,而是 用户的目录【/home中,比如ccx用户,则默认目录就是/home/ccx】,关闭匿名登陆以后,访问方法是ftp://ip进去默认就是家目录,如果后面跟路径了,可能就回报550权限问题了【限windows】

测试

我先在ccx家目录中创建一些文件,可以看到ccx用户的编译器是nologin的哦。

在这里插入图片描述

打开windows,输入:ftp://ip,输入用户名和密码

在这里插入图片描述

登陆以后就可以看到了这些文件了

在这里插入图片描述

上传下载删除新建都是可以正常使用的在这里插入图片描述

敲黑板,注意,我上面说过这段话,如果没注意看,可能就会产生下面错误了

在这里插入图片描述

我现在访问在位置栏输入:/ftp://ip/pub/访问就会报下面的550错误,这个是无法解决的!!!

在这里插入图片描述

修改默认家目录

如果不修改的话,我们通过不同用户登陆进去的,都是在这个用户的家目录里面【比如:/home/ccx, /home/ccx1, /home/ccx2】

但是修改了这个默认家目录以后,所有的用户登陆进去都在这个指定目录里面了。

这个不建议配置,除非你知道自己的在干什么。

一般情况都是不修改默认目录,但开启目录限制,就是每个用户登陆进去只能看到自己家目录的内容不能切换到其他目录【一般在linux上可以体现,linux可以cd切换目录,下面有说明】

新增行: local_root=/data/ccx #后面/data/ccx为路径

为路径根文件设置777权限,否则后面会无法操作:chmod 777 -R /data 【家目录不用是修改是因为家目录默认777权限】

[root@centos76_2 ccx]# vim /etc/vsftpd/vsftpd.conf

新增在任意位置即可

local_root=/data/ccx

allow_writeable_chroot=YES #2.3.5之后,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了,需新增此配置

[root@centos76_2 ccx]# mkdir -p /data/ccx #创建默认目录

[root@centos76_2 ccx]# chmod 777 -R /data

[root@centos76_2 ccx]#

[root@centos76_2 ccx]# touch /data/ccx/test{1…3} #创建几个默认文件夹测试用

[root@centos76_2 ccx]# ls /data/ccx/

test1 test2 test3

[root@centos76_2 ccx]#

[root@centos76_2 ccx]# systemctl restart vsftpd

测试

我使用ccx1这个用户测试,我先在这个家目录下创建几个文件,以便于后面区别是否配置成功

[root@centos76_2 ccx]# cd

[root@centos76_2 ~]# touch /home/ccx1/ccx1{1…3}

[root@centos76_2 ~]# ls /home/ccx1

ccx11 ccx12 ccx13

我们在地址栏把这个/去掉回车就可以重新登陆了哦【windows】

在这里插入图片描述

登陆 ccx1

在这里插入图片描述

登陆进来以后看到是test文件【如果不是可以尝试刷新一下当前页面】,修改成功,2个文件夹是刚才创建的【有操作权限】

在这里插入图片描述

如果不修改权限为777的话,就无法创建文件这些操作了【刚去把权限改为755了】

在这里插入图片描述

linux上也是正常查看的,只是目录变成/

[root@centos76_1 ~]# ftp 192.168.59.129

Connected to 192.168.59.129 (192.168.59.129).

220 (vsFTPd 3.0.2)

Name (192.168.59.129:root): ccx1

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> ls

227 Entering Passive Mode (192,168,59,129,199,12).

150 Here comes the directory listing.

-rw-r–r-- 1 0 0 0 May 06 04:03 test1

-rw-r–r-- 1 0 0 0 May 06 04:03 test2

-rw-r–r-- 1 0 0 0 May 06 04:03 test3

226 Directory send OK.

ftp> pwd

257 “/”

ftp>

目录限制配置

默认情况当前用户登陆ftp后可以来回切换文件系统,如果开启,就不能来回切换目录,只能查看编辑当前目录内容了。

这个需要用linux做测试了,未修改前如下,可以正常切换所有目录

[root@centos76_1 ~]# ftp 192.168.59.129 #登陆

Connected to 192.168.59.129 (192.168.59.129).

220 (vsFTPd 3.0.2)

Name (192.168.59.129:root): ccx # 输入用户名

331 Please specify the password.

Password: #输入密码

230 Login successful. #登陆成功提升

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> pwd #默认家目录

257 “/home/ccx”

ftp> cd /var/ftp/ #可以切换目录

250 Directory successfully changed.

ftp> ls

227 Entering Passive Mode (192,168,59,129,56,40).

150 Here comes the directory listing.

drwxr-xr-x 2 0 0 6 Apr 30 04:04 file_test

drwxrwxrwx 5 0 0 84 Apr 30 09:31 pub

226 Directory send OK.

ftp> cd pub

250 Directory successfully changed.

ftp> ls

227 Entering Passive Mode (192,168,59,129,187,216).

150 Here comes the directory listing.

drwxr-xr-x 2 0 0 6 Apr 30 03:56 file1

drwxr-xr-x 2 0 0 6 Apr 30 03:56 file2

drwxr-xr-x 2 0 0 6 Apr 30 03:56 file3

-rw-r–r-- 1 0 0 34 Apr 30 09:03 test1

-rw-r–r-- 1 0 0 4 Apr 30 03:57 test2

-rw-r–r-- 1 0 0 0 Apr 30 03:56 test3

226 Directory send OK.

ftp> get test1 # 可以下载其他目录中的文件

local: test1 remote: test1

227 Entering Passive Mode (192,168,59,129,85,16).

150 Opening BINARY mode data connection for test1 (34 bytes).

226 Transfer complete.

34 bytes received in 0.000346 secs (98.27 Kbytes/sec)

ftp>

  • 配置

  • 增加内容为:开启chroot选项

allow_writeable_chroot=YES

  • 取消注释 指定本地用户不能翻目录

chroot_local_user=YES

[root@centos76_2 ccx]# vim /etc/vsftpd/vsftpd.conf

#大概在102行,取消注释下列行内容,我是在取消注释前面添加的这个行呢

快速跳到102行的方法,vim进去以后,按102再按G即可,临时开启行号显示,:set nu

前面的102序号是行号,不要加啊!

102 allow_writeable_chroot=YES

103 chroot_local_user=YES

[root@centos76_2 ccx]# systemctl restart vsftpd

上面这样说这样能看懂吗? 如下图,102行是我新增的,103行是我取消注释的。

在这里插入图片描述

测试

上面步骤做完以后,我们再次使用ftp登陆该服务器,可以看到登陆进去以后,目录变成/了,而非家目录全程,并且再次切换目录就会报550错误呢,配置完成。

[root@centos76_1 ~]# ftp 192.168.59.129

Connected to 192.168.59.129 (192.168.59.129).

220 (vsFTPd 3.0.2)

Name (192.168.59.129:root): ccx

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> pwd

257 “/”

ftp> ls

227 Entering Passive Mode (192,168,59,129,230,53).

150 Here comes the directory listing.

drwxr-xr-x 2 0 0 6 May 06 03:29 home_ccx

drwxr-xr-x 2 1000 1000 6 May 06 03:34 新文件夹

drwxr-xr-x 2 1000 1000 6 May 06 03:34 新文件夹 (2)

226 Directory send OK.

ftp> cd /etc/

550 Failed to change directory.

ftp>

注:上面新增的行,不要敲错代码了,如果敲错以后,登陆会变成如下样子,并且是无法做任何操作的。

[root@centos76_1 ~]# ftp 192.168.59.129

Connected to 192.168.59.129 (192.168.59.129).

220 (vsFTPd 3.0.2)

Name (192.168.59.129:root): ccx

331 Please specify the password.

Password:

500 OOPS: vsftpd: refusing to run with writable root inside chroot()

Login failed.

421 Service not available, remote server has closed connection

ftp>

ftp> ls

Not connected.

只允许用户列表中用户切换目录【白名单】

  • 说明,这个功能不是说只允许列表中用户登陆ftp,而是说列表中的用户不受功能限制,比如下面限制了本地用户不能翻目录,但列表中的用户依然是可以翻目录的【其他用户只能登陆到家目录不能切换其他目录】,就是这样,之前我以为是这个只允许列表中用户登陆ftp,其他用户无法登陆,折腾了很久,发现是我理解错了。

  • chroot_list_enable=YES 开启代表用户列表文件/etc/vsftpd/chroot_list中的用户可以访问【需要在/etc/vsftpd目录下创建文件chroot_list,在文件中写入用户名,一行一个用户】,未在文件中列出的用户不能访问

  • 需要修改3处

  • 首先开启chroot选项,添加:allow_writeable_chroot=YES【已添加忽略】

  • 取消注释 指定本地用户不能翻目录 ,chroot_local_user=YES

  • 最后取消注释,指定用户列表文件的用户可以访问chroot_list_enable=YES #和上面是配套使用的

在这里插入图片描述

[root@centos76_2 ~]# vim /etc/vsftpd/vsftpd.conf

103 allow_writeable_chroot=YES

104 chroot_local_user=YES

105 chroot_list_enable=YES

[root@centos76_2 ~]# systemctl restart vsftpd

[root@centos76_2 ~]# cd /etc/vsftpd/

[root@centos76_2 vsftpd]# ls

ftpusers user_list vsftpd.conf vsftpd_conf_migrate.sh

[root@centos76_2 vsftpd]# vim chroot_list # 创建允许登陆的用户

hero1

hero2

[root@centos76_2 vsftpd]# useradd -s /sbin/nologin hero1 #创建用户名和密码

[root@centos76_2 vsftpd]# useradd -s /sbin/nologin hero2

[root@centos76_2 vsftpd]# echo hero1|passwd --stdin hero1

Changing password for user hero1.

passwd: all authentication tokens updated successfully.

[root@centos76_2 vsftpd]# echo hero2|passwd --stdin hero2

Changing password for user hero2.

passwd: all authentication tokens updated successfully.

[root@centos76_2 vsftpd]#

测试

注意看命令中的说明

[root@centos76_1 ~]# ftp 192.168.59.129

Connected to 192.168.59.129 (192.168.59.129).

220 (vsFTPd 3.0.2)

Name (192.168.59.129:root): ccx #先登陆未在列表中的用户

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> ls # 能进入到家目录【我设置默认目录了】

227 Entering Passive Mode (192,168,59,129,122,26).

150 Here comes the directory listing.

-rwxrwxrwx 1 0 0 0 May 06 04:03 test1

-rw-r–r-- 1 0 0 0 May 06 04:03 test2

-rw-r–r-- 1 0 0 0 May 06 04:03 test3

226 Directory send OK.

ftp> cd /var/ftp #切换路径会报错

550 Failed to change directory.

ftp> ls

227 Entering Passive Mode (192,168,59,129,244,220).

150 Here comes the directory listing.

-rwxrwxrwx 1 0 0 0 May 06 04:03 test1

-rw-r–r-- 1 0 0 0 May 06 04:03 test2

-rw-r–r-- 1 0 0 0 May 06 04:03 test3

226 Directory send OK.

ftp> 221 Goodbye.

[root@centos76_1 ~]# ftp 192.168.59.129

Connected to 192.168.59.129 (192.168.59.129).

220 (vsFTPd 3.0.2)

Name (192.168.59.129:root): hero1 # 登陆在列表中的用户

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> ls #进入到默认路径了

227 Entering Passive Mode (192,168,59,129,56,190).

150 Here comes the directory listing.

-rwxrwxrwx 1 0 0 0 May 06 04:03 test1

-rw-r–r-- 1 0 0 0 May 06 04:03 test2

-rw-r–r-- 1 0 0 0 May 06 04:03 test3

226 Directory send OK.

ftp> cd /var/ftp # 能正常切换目录

250 Directory successfully changed.

ftp> ls

227 Entering Passive Mode (192,168,59,129,19,221).

150 Here comes the directory listing.

drwxr-xr-x 2 0 0 6 Apr 30 04:04 file_test

drwxrwxrwx 5 0 0 84 Apr 30 09:31 pub

226 Directory send OK.

ftp>

扩展说明
  • 在默认配置下,本地用户登入FTP后可以使用cd命令切换到其他目录,这样会对系统带来安全隐患。可以通过以下三条配置文件来控制用户切换目录。

  • chroot_list_enable=YES/NO#设置是否启用chroot_list_file配置项指定的用户列表文件。默认值为NO。

用户名存放文件:/etc/vsftpd.chroot_list,一行一个白名单用户,用于指定用户列表文件,该文件用于控制哪些用户可以切换到用户家目录的上级目录。

  • chroot_local_user=YES/NO#用于指定用户列表文件中的用户是否允许切换到上级目录。默认值为NO。

  • 通过搭配能实现以下几种效果:

  • ①当chroot_list_enable=YES,chroot_local_user=YES时,在/etc/vsftpd.chroot_list文件中列出的用户,可以切换到其他目录;未在文件中列出的用户,不能切换到其他目录。

  • ②当chroot_list_enable=YES,chroot_local_user=NO时,在/etc/vsftpd.chroot_list文件中列出的用户,不能切换到其他目录;未在文件中列出的用户,可以切换到其他目录。

  • ③当chroot_list_enable=NO,chroot_local_user=YES时,所有的用户均不能切换到其他目录。

  • ④当chroot_list_enable=NO,chroot_local_user=NO时,所有的用户均可以切换到其他目录。

禁止某些用户登陆

/etc/vsftpd/ftpusers这个是系统默认的黑名单,这里面的用户也是直接被拒绝的,但我们不用这种方法,用其他方法,继续往下看。

  • 额,就是不想让某些用户访问ftp

  • 命令说明

  • userlist_deny=YES /默认没有这一项,与下面这条参数:userlist_file=/etc/vsftpd/localuser_list结合使用。

userlist_file=/etc/vsftpd/localuser_list /默认没有这一项, 与上面参数:userlist_deny=YES结合使用此参数用,来改变存放可以登录或禁止登录的账号,suerlist_deny为YES时,这里面的用户则不能登录,当为NO时,这里面的用户可以登录,和/etc/vsftpd/user_list是一个意思

  • 过程见下面,后面说明,不懂就认真看。

[root@centos76_2 userconfig]# vim /etc/vsftpd/vsftpd.conf

我是在最后添加的下面2行

userlist_deny=YES

userlist_file=/etc/vsftpd/localuser_list

[root@centos76_2 vsftpd]# systemctl restart vsftpd

[root@centos76_2 userconfig]# cd /etc/vsftpd/

[root@centos76_2 vsftpd]# vim localuser_list #创建不允许登陆的用户名,一行一个

ccx

superman1

测试

因为上面限制了ccx和superman1不能正常登陆,所以我登陆这2个用户直接连输密码的机会都不给我。

[root@centos76_1 ~]# ftp 192.168.59.129

Connected to 192.168.59.129 (192.168.59.129).

220 (vsFTPd 3.0.2)

Name (192.168.59.129:root): ccx #输入用户名就结束了

530 Permission denied.

Login failed.

ftp> ls

530 Please login with USER and PASS.

Passive mode refused.

ftp> 221 Goodbye.

[root@centos76_1 ~]# ftp 192.168.59.129

Connected to 192.168.59.129 (192.168.59.129).

220 (vsFTPd 3.0.2)

Name (192.168.59.129:root): superman1 #输入用户名就结束了

530 Permission denied.

Login failed.

ftp> ls

530 Please login with USER and PASS.

Passive mode refused.

ftp>

这放一个正常的康康

[root@centos76_1 ~]# ftp 192.168.59.129

Connected to 192.168.59.129 (192.168.59.129).

220 (vsFTPd 3.0.2)

Name (192.168.59.129:root): ccx1

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp> 221 Goodbye.

配置多用户登陆

  • 这个功能可能有点鸡肋,就是为了不同用户登陆进去进入到不同的路径,和不指定默认家目录唯一不同的区别就是这个可以指定路径罢了。

  • 注:我当前ftp已经配置了默认家目录

  • 配置

命令:新增一行user_config_dir=/etc/vsftpd/userconfig#用户配置目录【多用户的时候,设置各个用户登录自己的目录】

mkdir创建/etc/vsftpd/userconfig文件夹,然后在文件夹中vim编辑需要配置的用户名,内容是:local_root=/file【口述的过程,看不懂不要紧,下面命令有详细过程 】

  • 过程看命令介绍吧,跟着命令流程走

[root@centos76_2 vsftpd]# vim vsftpd.conf

我是在最后新增的下面行内容

user_config_dir=/etc/vsftpd/userconfig

[root@centos76_2 vsftpd]# systemctl restart vsftpd

[root@centos76_2 vsftpd]# cd /etc/vsftpd

[root@centos76_2 vsftpd]# mkdir userconfig

[root@centos76_2 vsftpd]# cd userconfig

[root@centos76_2 userconfig]# vim superman1 #superman1是用户名

#这个命令其实就是修改默认家目录的命令,后面是自定义路径

local_root=/ftp/superman1

[root@centos76_2 userconfig]# vim superman2 #superman2是用户名

#这个命令其实就是修改默认家目录的命令,后面是自定义路径

local_root=/ftp/superman2

[root@centos76_2 userconfig]# useradd -s /sbin/nologin superman1 #创建用户名

[root@centos76_2 userconfig]# useradd -s /sbin/nologin superman2 #创建用户名

[root@centos76_2 userconfig]# echo superman1 | passwd --stdin superman1 # 设置密码

Changing password for user superman1.

passwd: all authentication tokens updated successfully.

[root@centos76_2 userconfig]# echo superman2 | passwd --stdin superman2

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以点击这里获取!

程看命令介绍吧,跟着命令流程走

[root@centos76_2 vsftpd]# vim vsftpd.conf

我是在最后新增的下面行内容

user_config_dir=/etc/vsftpd/userconfig

[root@centos76_2 vsftpd]# systemctl restart vsftpd

[root@centos76_2 vsftpd]# cd /etc/vsftpd

[root@centos76_2 vsftpd]# mkdir userconfig

[root@centos76_2 vsftpd]# cd userconfig

[root@centos76_2 userconfig]# vim superman1 #superman1是用户名

#这个命令其实就是修改默认家目录的命令,后面是自定义路径

local_root=/ftp/superman1

[root@centos76_2 userconfig]# vim superman2 #superman2是用户名

#这个命令其实就是修改默认家目录的命令,后面是自定义路径

local_root=/ftp/superman2

[root@centos76_2 userconfig]# useradd -s /sbin/nologin superman1 #创建用户名

[root@centos76_2 userconfig]# useradd -s /sbin/nologin superman2 #创建用户名

[root@centos76_2 userconfig]# echo superman1 | passwd --stdin superman1 # 设置密码

Changing password for user superman1.

passwd: all authentication tokens updated successfully.

[root@centos76_2 userconfig]# echo superman2 | passwd --stdin superman2

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
[外链图片转存中…(img-hlzVcPcC-1715322837688)]
[外链图片转存中…(img-xxucgEGy-1715322837688)]
[外链图片转存中…(img-9z437yJu-1715322837688)]
[外链图片转存中…(img-TqAASOVC-1715322837689)]
[外链图片转存中…(img-UgHFDOZx-1715322837689)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以点击这里获取!

  • 11
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值