当前位置: 代码网 > 服务器>服务器>ftp服务器 > FTP服务器的搭建及配置文件的使用详解

FTP服务器的搭建及配置文件的使用详解

2024年05月12日 ftp服务器 我要评论
ftp简介文件传输协议ftp(file transfer ftp)作为网络共享文件的传输协议,在网络应用软件中具有广泛的应用。ftp协议ftp是tcp/ip的协议簇协议之一,其主要功能是借助网络实现远

ftp简介

文件传输协议ftp(file transfer ftp)作为网络共享文件的传输协议,在网络应用软件中具有广泛的应用。

ftp协议

ftp是tcp/ip的协议簇协议之一,其主要功能是借助网络实现远距离主机间的文件传输。

1.使用vsftpd服务器

vsftpd (very secure ftp)是linux系统的常用软件。

1.1 vsftpd的服务器的安装与启动

vsftpd在yum源中的名称就是叫做vsftpd

yum install -y vsftpd* //*代表下载所有关于vsftpd的软件包

1.2 vsftpd服务器的用户类型

vsftpd的用户类型主要分为匿名用户、本地用户和虚拟用户。

(1)匿名用户:如果vsftpd服务器提供匿名访问功能,默认的匿名访问用户为anonymous或ftp,密码为空,匿名用户登录后进入到工作目录是/var/ftp

(2)本地用户:本地用户信息存储在/etc/passwd文件中,本地用户输入用户名和密码后可登录vsftpd,并且直接进入该用户的宿主目录。

(3)虚拟用户:旨在vsftpd服务器中拥有账号,并且该账号只能用于文件传输服务的专有用户,也称guest用户。该类用户可以通过输入用户名和密码进行授权登录。

2. vsftpd服务器的测试

ftp服务器的运行模式基于服务器、客户端,服务器安装完vsftpd软件包后,可以通过客户端进行测试。

1.首先下载vsftpd与其相关的服务

yum install -y vsftpd* ftp*

2.然后打开ftp服务的端口,使防火墙放行ftp服务

 firewall-cmd --zone=public --add-port=21/tcp --per
 systemctl restart vsftpd firewalld

3.在虚拟机上以匿名用户身份进行测试,使用ftp命令,后面输入本机的ip地址,用户名是ftp,密码是空,登录过程如下。

[root@linux2 ~]# ftp 192.168.123.102
connected to 192.168.123.102 (192.168.123.102).
220 (vsftpd 3.0.2)
name (192.168.123.102:root): ftp
331 please specify the password.
password:
230 login successful. 
remote system type is unix.
using binary mode to transfer files.
ftp>
ftp> ls

以上命令中的192.168.123.102是vsftpd服务器的ip地址

终端打印信息“230 login successful. ”说明登录成功

3.ftp常用的命令

命令说明
ls查看服务器当前目录的文件
mkdir 【目录名】在服务器新建目录
put 文件名向服务器上传指定的一个文件
mput 文件名列表向服务器上传多个文件
chmod改变服务器中的文件权限
?或help显示内部命令的帮助信息
open 域名或ip地址建立于指定服务器的连接
close终止远端的进程,返回命令状态
bye或quit推出服务器

4.vsftp服务器的配置文件

配置基本的vsftpd服务器比较简单,只需要正确安装了vsftpd软件,直接启动vsftpd并放行端口就行了,但是如果想要更进一步的搭建vsftpd服务,那就要掌握vsftpd服务器的配置文件。

文件说明
/etc/vsftpd/vsftpd.confvsftpd服务器主配置文件
/etc/vsftpd/ftpusers禁止访问vsftp服务器的用户名单
/etc/vsftpd/user_list指定用户能否访问ftp服务器取决于userlist_deny选项的设置
/etc/vsftpd/chroot_list目录访问控制文件

下面对这几个文件进行详细说明

1.vsftpd.conf文件

vsftpd.conf位于/etc/vsftpd目录下,与大多数配置文件一样,vsftpd.conf文件中以符号”#“开始注释信息,我们可以用vim等工具对它进行修改。在vsftpd中去掉注释,显示该文件的配置内容如下:

[root@linux2 ~]# grep -v "#" /etc/vsftpd/vsftpd.conf
anonymous_enable=yes
local_enable=yes
write_enable=yes
local_umask=022
dirmessage_enable=yes
xferlog_enable=yes
connect_from_port_20=yes
xferlog_std_format=yes
listen=no
listen_ipv6=yespam_service_name=vsftpd
userlist_enable=yes
tcp_wrappers=yes

下面对该文件中的常用选项进行介绍,合理使用这些选项是保障ftp安全稳定的前提

选项说明
anonymous_enable设置是否允许匿名用户登录服务器
local_enable设置是否允许本地用户登录服务器
write_enable设置是否允许写操作
local_umask设置本地用户创建文件的umask值
anon*_upload_enable*设置是否允许匿名用户上传文件
anon*_mkdir_write_enable*设置是否允许匿名用户建立目录
xferlog_enable是否激活日志功能
chown_uploads修改匿名用户上传文件的所有者
chown_username=whoever启用chown_uploads=yes时,指定为主用户账户,whoever表示你的ftp用户
chroot*_local_user*设置是否将所有用户限制在其主目录
chroot*_list_enable*设置是否启用限制用户的名单
chroot*_list_file*设置是否限制/排除主目录下的用户名单,限制/排除有chrootlocaluser值决定
allow*_writeable_choot*设置chroot目录的写权限

2.ftpusers文件

/etc/vsftpd/ftpusers文件用于指定不能访问vsftp服务器的用户列表,此文件在格式中采用每个用户一行的形式,其中包含的用户通常是系统的超级用户和系统用户

3.user_list文件

/etc/vsftpd/userlist 文件也用于保留用户列表,指定的用户能否访问vsftp服务取决于userlistdeny选项的参数,当userlist_deny选项的参数=yes时,此文件中写入的所有用户都将无法登录vsftp服务,想都别想,但是当userlist_deny=no时,这些用户就起来了,那就表示只有这些用户能够访问vsftp服务,所以一定要注意这个选项的参数。非常重要。

如果要限制一些指定的本地用户不能访问ftp服务,则可以添加以下内容来做到:

进入/etc/vaftpd/vsftpd.conf 末行添加

userlist_enable=yes

userlist_deny=yes

userlistfile=/etc/vsftpd/user_list

进入/etc/vsftpd/user_list 添加你需要拒绝访问的本地用户

toto (意为指定用户 每个指定用户单独占一行)

如果需要指定几个本地用户才能访问ftp服务,则可以添加以下内容来做到:

进入/etc/vaftpd/vsftpd.conf 末行添加

userlist_enable=yes

userlist_deny=no

userlistfile=/etc/vsftpd/user_list

进入/etc/vsftpd/user_list 添加你需要拒绝访问的本地用户

toto (意为指定用户 每个指定用户单独占一行)

4.chroot_list文件

默认情况下,匿名用户会被锁定在默认ftp目录中,而本地用户却可以访问自己主目录以外的内容,出于安全考虑,通过chroot_list文件可以限制用户只能访问其主目录,也可以实现使用不同哟ing胡不仅能访问自己的目录也可以访问主目录以外的内容。

chrootlist文件设计chrootlocaluser和chroot*_list_enable两个选项*

chroot*_list_file默认指向/etc/ sftpd/chroot_list*

chroot_list文件的功能有两个

  • 是限制用户名单中的用户只能在其主目录中
  • 名单中的用户不仅能访问自己的主目录,还能跳出主目录,访问其它的目录。

当chroot*_list_enable=yes时,chroot_list中的用户只能在本目录中*

当chroot*_list_enable=no时,chroot_list中的目录可以访问除主目录以外的内容*

5.vsftp服务器的使用

前面介绍了vsftpd服务器的安装,启动及主要的配置文件,下面通过具体的实例来介绍该服务器的具体应用。

1.匿名用户访问vsfto服务器

本次实验环境为centos7 服务器的ip地址为192.168.123.101 主机名为linux1

第一步:安装vsftp服务及开启该服务

yum install -y vsftpd
systemctl restart vsftpd

使用ftp匿名用户登录,如下所示:

[root@linux2 ~]# ftp 192.168.123.101
connected to 192.168.123.101 (192.168.123.101).
220 (vsftpd 3.0.2)
name (192.168.123.101:root): ftp
331 please specify the password.
password:
230 login successful.
remote system type is unix.
using binary mode to transfer files.
ftp>
ftp> ls
227 entering passive mode (192,168,123,101,101,29).
150 here comes the directory listing.
drwxr-xr-x    2 0        0               6 oct 13  2020 pub
226 directory send ok.

登录成功,使用ls命令浏览默认的ftp目录 (/var/ftp)

1、首先在ftp默认目录/var/pub中创建两个文本文件1.txt和2.txt

[root@ayaka ~]# cd /var/ftp/pub/
[root@ayaka pub]# touch 1.txt
[root@ayaka pub]# touch 2.txt
[root@ayaka pub]# ls
1.txt 2.txt

2、在客户端上进入/pub目录

ftp> ls
227 entering passive mode (192,168,123,101,75,103).
150 here comes the directory listing.
-rw-r–r-- 1 0 0 0 may 09 10:37 1.txt
-rw-r–r-- 1 0 0 0 may 09 10:37 2.txt

将vsftpd服务器上文件下载到客户端

进入pub目录 使用get命令将文件1.txt与2.txt下载到当前目录下,下载成功的过程如下

220 (vsftpd 3.0.2)
name (192.168.123.101:root): ftp
331 please specify the password.
password:
230 login successful.
remote system type is unix.
using binary mode to transfer files.
ftp> cd pub
250 directory successfully changed.
ftp> ls
227 entering passive mode (192,168,123,101,25,132).
150 here comes the directory listing.
-rw-r--r--    1 0        0              50 may 09 10:54 1.txt
-rw-r--r--    1 0        0               0 may 09 10:37 2.txt
226 directory send ok.
ftp> !pwd
/root
ftp> get 1.txt
local: 1.txt remote: 1.txt
227 entering passive mode (192,168,123,101,145,74).
150 opening binary mode data connection for 1.txt (50 bytes).
226 transfer complete.
50 bytes received in 0.000134 secs (373.13 kbytes/sec)
ftp> !ls
1  1.txt  anaconda-ks.cfg
ftp>

测试在vsftp服务器创建目录

ftp> mkdir toto
550 permission denied.
ftp

通过以上操作可以得出:在默认情况下,匿名用户一般只有从服务器服务器下载文件的权限,不能上传文件或进行其它操作

那如何让他变得有权限呢,接下来我们来配置vsftpd使得有上传文件或进行其它操作

更改vsftpd.conf文件,开启匿名用户上传,创建目录权限

将如下两个参数取消注释

29 #anon_upload_enable=yes //允许匿名文件上传文件
34 #anon_mkdir_write_enable=yes //允许匿名用户建立目录

取消注释后保存并退出 重启vsftpd服务让它生效

systemctl restart vsftpd

创建匿名用户上传目录

需要在/var/ftp目录下创建一个目录,修改目录的权限,让匿名用户具有写权限

[root@ayaka pub]# mkdir /var/ftp/hub
[root@ayaka pub]# chmod o+w /var/ftp/hub

上传目录

使用put命令把本地文件上传到ftp服务器上,具体过程如下。

[root@linux2 ~]# ftp 192.168.123.101
connected to 192.168.123.101 (192.168.123.101).
220 (vsftpd 3.0.2)
name (192.168.123.101:root): ftp
331 please specify the password.
password:
230 login successful.
remote system type is unix.
using binary mode to transfer files.
ftp> cd hub  			//进入目录hub 
250 directory successfully changed.
ftp> !ls			//执行本机shell命令,显示当前内容 
1  1.txt  anaconda-ks.cfg  file1.txt
ftp> put 1   		//上传文件1
local: 1 remote: 1
227 entering passive mode (192,168,123,101,176,227).
150 ok to send data.
226 transfer complete.
1664 bytes sent in 0.000814 secs (2044.23 kbytes/sec)
ftp> ls -l          //查看hub目录中的文件列表
227 entering passive mode (192,168,123,101,85,50).
150 here comes the directory listing.
-rw-------    1 14       50           1664 may 09 11:26 1
226 directory send ok.
ftp> mkdir tomato			//创建目录
257 "/hub/tomato" created				//创建目录成功
ftp> ls
227 entering passive mode (192,168,123,101,99,18).
150 here comes the directory listing.
-rw-------    1 14       50           1664 may 09 11:26 1
drwx------    2 14       50              6 may 09 11:35 tomato
226 directory send ok.
ftp> 

2.本地用户访问vsftp服务器

本地用户是在ftp服务器上拥有用户账户的用户,相当于ftp服务器中的真实实际用户,其通过输入自己的账号和口令来进行授权登录,当用户成功登录服务器后,其登录目录为用户的主目录,用户的权限对该主目录的操作权限,可以下载也可以上传文件。

下面我们将用实例来介绍本地用户登录服务器下载,上传文件的过程。

本地用户访问vsftp服务器,本例vsftp服务器的ip地址为192.168.123.101 客户端主机名为client

vsftp服务器建立用户ftpuser,并在用户的家目录建立文件toto和yoyo 命令如下

[root@ayaka pub]# useradd ftpuser
[root@ayaka pub]# echo “123” | passwd --stdin ftpuser //设置ftpuser的密码为123
[root@ayaka pub]# su - ftpuser //登录用户ftpuser
[ftpuser@ayaka ~]$ touch toto.txt
[ftpuser@ayaka ~]$ touch yoyo.txt

使用本地用户登录vsftpd服务器后,其登录目录为用户的主目录/home/ftpuser,具体过程如下

[root@linux2 ~]# ftp 192.168.123.102
connected to 192.168.123.102 (192.168.123.102).
220 (vsftpd 3.0.2)
name (192.168.123.102:root): ftpuser
331 please specify the password.
password:
530 login incorrect.
login failed.
ftp> ^z
[2]+  已停止               ftp 192.168.123.102
[root@linux2 ~]#
[root@linux2 ~]# ftp 192.168.123.101
connected to 192.168.123.101 (192.168.123.101).
220 (vsftpd 3.0.2)
name (192.168.123.101:root): ftpuser
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,123,101,56,176).
150 here comes the directory listing.
-rw-rw-r--    1 1000     1000            0 may 09 11:44 toto.txt
-rw-rw-r--    1 1000     1000            0 may 09 11:44 yoyo.txt
226 directory send ok.
ftp> pwd
257 "/home/ftpuser"
ftp>

创建目录

用户ftpuser创建目录tomato,具体操作如下

ftp> mkdir tomato
257 "/home/ftpuser/tomato" created
ftp> ls
227 entering passive mode (192,168,123,101,244,9).
150 here comes the directory listing.
drwxr-xr-x    2 1000     1000            6 may 09 11:52 tomato
-rw-rw-r--    1 1000     1000            0 may 09 11:44 toto.txt
-rw-rw-r--    1 1000     1000            0 may 09 11:44 yoyo.txt
226 directory send ok.
ftp>

上传本地文件

用户ftpuser上传本地文件到服务器指定目录,具体操作如下:

ftp> !ls -l
总用量 16
-rw-r--r--. 1 root root 1664 10月 23 2020 1
-rw-r--r--. 1 root root   50 5月   9 18:56 1.txt
-rw-------. 1 root root 1295 5月   7 19:16 anaconda-ks.cfg
-rw-r--r--. 1 root root    7 5月   9 19:20 file1.txt
ftp> !pwd
/root
ftp> put anaconda-ks.cfg				//上传本地文件
local: anaconda-ks.cfg remote: anaconda-ks.cfg
227 entering passive mode (192,168,123,101,101,125).
150 ok to send data.
226 transfer complete.
1295 bytes sent in 0.00108 secs (1194.65 kbytes/sec)
ftp> ls -l				//浏览远程目录,验证上传文件是否为本地文件
227 entering passive mode (192,168,123,101,149,213).
150 here comes the directory listing.
-rw-r--r--    1 1000     1000         1295 may 09 11:55 anaconda-ks.cfg
drwxr-xr-x    2 1000     1000            6 may 09 11:52 tomato
-rw-rw-r--    1 1000     1000            0 may 09 11:44 toto.txt
-rw-rw-r--    1 1000     1000            0 may 09 11:44 yoyo.txt
226 directory send ok.
ftp>

下载服务器文件到本地

用ftpuser下载文件toto到本地当前目录下,下载文件yoyo到本地目录/tmp下,并且重命名为toto.bak 具体操作如下

ftp> get toto
local: toto remote: toto
227 entering passive mode (192,168,123,101,206,168).
550 failed to open file.
ftp> ls
227 entering passive mode (192,168,123,101,98,221).
150 here comes the directory listing.
-rw-r--r--    1 1000     1000         1295 may 09 11:55 anaconda-ks.cfg
drwxr-xr-x    2 1000     1000            6 may 09 11:52 tomato
-rw-rw-r--    1 1000     1000            0 may 09 11:44 toto.txt
-rw-rw-r--    1 1000     1000            0 may 09 11:44 yoyo.txt
226 directory send ok.
ftp> get toto.txt           //下载文件到本地目录 
local: toto.txt remote: toto.txt
227 entering passive mode (192,168,123,101,46,209).
150 opening binary mode data connection for toto.txt (0 bytes).
226 transfer complete.
ftp> !ls -l					//浏览本地目录,验证下载文件操作是否成功
总用量 16
-rw-r--r--. 1 root root 1664 10月 23 2020 1
-rw-r--r--. 1 root root   50 5月   9 18:56 1.txt
-rw-------. 1 root root 1295 5月   7 19:16 anaconda-ks.cfg
-rw-r--r--. 1 root root    7 5月   9 19:20 file1.txt
-rw-r--r--. 1 root root    0 5月   9 19:59 toto.txt
// 下载文件yoyo.txt 到 本地目录/tmp下 并重命名为yoyo.bak
tp> get yoyo.txt /tmp/yoyo.bak
local: /tmp/yoyo.bak remote: yoyo.txt
227 entering passive mode (192,168,123,101,46,136).
150 opening binary mode data connection for yoyo.txt (0 bytes).
226 transfer complete.
ftp> lcd /tmp   进入本地目录/tmp 
local directory now /tmp
ftp> !ls   //浏览验证本地目录是否有yoyo.bak 
ks-script-e7_ota                                                         yoyo.bak
systemd-private-7da9c4fa89d64f18a66b1cf3053c3ff6-chronyd.service-jofutu  yum.log
vmware-root_797-4257069498
ftp>

 到此这篇关于ftp服务器的搭建与文件目录的使用详解的文章就介绍到这了,更多相关ftp服务器搭建内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com