搭建ftp服务器

生活工作中,经常会遇到一些共享文件传输,使用ftp服务器是一个很不错的选择。本文介绍如何在linux系统下搭建ftp服务器,已在局域网测试成功。

下载安装

linux 主要有vsftpd和proftpd两个ftp服务器,这里使用vsftpd。两者都是开源软件,但是vsftpd 比较proftpd 更简单易用。proftpd 配置比较复杂,但功能更丰富。

1、下载vsftpd:

1
2
3
sudo apt-get install vsftpd
# 校验
vsftpd -v

2、启动vsftpd

1
sudo systemctl enable vsftpd.service

3、查看服务端口

1
sudo netstat -antup | grep ftp

效果如下:

配置

这部分比较重要,很多连接失败都是因为配置错误。

主机配置:
1、创建用户并配置密码

1
2
3
4
5
# 创建用户
sudo adduser test_ftp
# 配置密码
sudo passwd ftptest

2、创建文件夹

1
2
3
4
5
6
7
8
9
10
# 创建主文件夹
sudo mkdir /home/test/ftp

# 创建上传子文件夹
sudo mkdir /home/test/ftp/upload

# 完成权限配置(自己根据需要配置文件夹目录权限)
sudo chown -R test_ftp:test_ftp /home/test/ftp
sudo chmod -R 755 /home/test/ftp

3、修改登录模式
因为ftp 登录默认进入用户主目录,但是系统并没有相关主目录,需要重新设置登录模式。

1
2
3
4
5
6
# 去除所有用户组权限
sudo chmod a-w /home/test_ftp

# 设置用户test_ftp 登录主目录
sudo usermod -d /home/test/ftp test_ftp

3、配置文件
ubuntu 默认配置文件在:/etc/vsftpd.conf;centos 默认配置文件在:/etc/vsftpd/vsftpd.conf
主要配置项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 设置本地用户登录后所在目录。
local_root=/home/test/ftp

# 允许用户写入文件。
write_enable=YES

# 设置用户登录后进入的目录。
chroot_local_user=YES

# 允许用户在chroot目录下写文件。
allow_writeable_chroot=YES

# 文件名字乱码问题 使用utf8编码
utf8_filesystem=YES

#禁止匿名登录FTP服务器。
anonymous_enable=NO

#允许本地用户登录FTP服务器。
local_enable=YES

#监听IPv4 sockets。
listen=YES

以上配置是我验证通过的,如果需要配置匿名登录或者ssl验证,需要添加相应的配置项

阿里云参考

启动与验证

1、配置完成后,重启服务

1
2
sudo systemctl restart vsftpd

2、安装filezilla 软件测试
输入 服务器地址和密码,端口默认21,然后点击快速连接,如下:

连接成功会出现日志:

过程问题

1、登录失败报错

1
Response: 331 Please specify the password. Command: PASS ****** Response: 500 OOPS: cannot change directory:/home/test_ftp Error: Critical error: Could not connect to server

解决:没有正确配置用户登录权限和登录的主目录

2、登录提示

1
this server does not support FTP over TLS

解决:FileZilla 客户端尝试使用“加密连接(FTPS)”,但是服务器没有启用 TLS。如果没有启用,可以跳过。

3、上传下载文件失败

1
2
Response:	550 Permission denied.
Error: Critical file transfer error

解决:没有正确配置目录权限和确保配置文件中有write_enable=YESallow_writeable_chroot=YES

4、乱码问题
解决:linux一般为utf8编码,启用utf8_filesystem=YES