Linux搭建FTP服务器

Linux搭建FTP服务器

简述

FTP是File Transfer Protocol(文件传输协议)的简称,用于internet上的控制文件的双向传输。
FTP也是一个应用程序,基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。

一.安装服务器

1.1. 安装vsftp
yum install -y vsftp

1.2. 配置文件路径
配置文件/etc/vsftp/vsftp.conf
vsftpd用户认证配置文件/etc/pam.d/vsftpd
配置文件目录/etc/vsftpd/

1.3. vsftp常用命令

启动ftp服务器service vsftp start
停止ftp服务器service vsftp stop
查看ftp服务器service vsftp status

二. 配置vsftp

2.1. 配置ftp用户
useradd ftpuser                          # 创建ftp用户
echo ftppasswd | passwd ftpuser --stdin  # 设置ftp用户的密码
usermod -s /sbin/nologin ftpuser         # 不允许ftp用户登陆
usermod -d /home/ftpuser/file ftpuser    # 设置ftpuser用户的访问路径
chmod 777 -R /home/ftpuser/file/         # 设置ftp用户的读写权限

2.2. 配置vsftp
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
# 取消对此的注释以允许本地用户登陆
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/xferlog
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode. The vsftpd.conf(5) man page explains
# the behaviour when these options are disabled.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
ascii_upload_enable=YES
ascii_download_enable=YES
#
# You may fully customise the login banner string:
ftpd_banner=欢迎登陆本地的FTP服务器.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
allow_writeable_chroot=YES
chroot_local_user=YES
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=NO
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
# Make sure, that one of the listen options is commented !!
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES

2.3. 注释掉auth防止无法登陆
[root@molongyin file]# cat /etc/pam.d/vsftpd
#%PAM-1.0
session    optional     pam_keyinit.so    force revoke
auth       required	pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
#auth       required	pam_shells.so  # 注释掉
auth       include	password-auth
account    include	password-auth
session    required     pam_loginuid.so
session    include	password-auth

2.4. 启动ftp服务器
service vsftp start

2.5. 浏览器登陆

我们在 /home/ftpuser/file 下面创建一个测试文件 1.txt 文件。
接着打开浏览器: ftp://192.168.31.26 你测试的Centos服务器的地址。
![image.png](https://img-blog.csdnimg.cn/img_convert/e0ac2155a5bf3ef15b37b344140e63ad.png#align=left&display=inline&height=255&margin=[object Object]&name=image.png&originHeight=255&originWidth=856&size=75852&status=done&style=none&width=856)
首次访问会让你输入用户(ftpuser)密码(ftppasswd),然后就可以进入看到我们刚才的创建的测试文件了。

三. Win配合使用

如果我们想要从win笔记本上传文件的话,不需要下载软件,打开控制台(cmd或者wt),输入:ftp ip 端口,就可以操作。

ftp> open 192.168.31.26 21  # 输入密码。就可以访问
ftp> put e:\test\1.txt      # 上传文件
ftp> quit                   # 退出

其他的命令自行百度吧。

四. vsftp常用配置

参数作用
anonymous_enable=YES启用匿名用户登录
anon_upload_enable=YES允许匿名用户上传
anon_mkdir_write_enable=YES允许匿名用户创建目录,但是不能删除
anon_other_write_enable=YES允许匿名用户创建和删除目录
local_enable=YES启用本地用户登录
write_enable=YES允许本地用户有写权限
local_umask=022通过ftp上传文件的默认遮罩码
chroot_local_user=YES禁锢所有的ftp本地用户于其家目录中
chroot_list_enable=YES开启禁锢文件列表,需要与chroot_list_file参数一起使用
chroot_list_file=/etc/vsftpd/chroot_list指定禁锢列表文件路径,在此文件里面的用户将被禁锢在其家目录中
allow_writeable_chroot=YES允许被禁锢的用户家目录有写权限
xferlog_enable=YES是否启用传输日志,记录ftp传输过程
xferlog_std_format=YES传输日志是否使用标准格式
xferlog_file=/var/log/xferlog指定传输日志存储的位置
chown_uploads=YES是否启用改变上传文件属主的功能
chown_username=whoever指定要将上传的文件的属主改为哪个用户,此用户必须在系统中存在
pam_service_name=vsftpd指定vsftpd使用/etc/pam.d下的,哪个pam配置文件进行用户认证
userlist_enable=YES是否启用控制用户登录的列表文件:默认为/etc/vsftpd/user_list文件
userlist_deny=YES是否拒绝userlist指定的列表文件中存在的用户登录ftp
max_clients=#最大并发连接数
max_per_ip=#每个IP可同时发起的并发请求数
anon_max_rate匿名用户的最大传输速率,单位是“字节/秒”
local_max_rate本地用户的最大传输速率,单位是“字节/秒”
dirmessage_enable=YES启用某目录下的.message描述信息,假定有一个目录为/upload,在其下创建一个文件名为.message,在文件内写入一些描述信息,则当用户切换至/upload目录下时会自动显示.message文件中的内容
message_file设置访问一个目录时获得的目录信息文件的文件名,默认是.message
idle_session_timeout=600设置默认的断开不活跃session的时间
data_connection_timeout=120设置数据传输超时时间
ftpd_banner=“Welcome to chenlf FTP service.”定制欢迎信息,登录ftp时自动显示

五. 总结

配合ftp和ssh我们其实可以抛弃现在的xshell和sftp等一些软件。简单又方便!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值