Debian/linux 安装配置ftp服务器
转载自:https://www.cnblogs.com/aqing1987/p/4206754.html
安装
$ sudo apt-get install vsftpd
配置
- 修改vsftpd.conf
sudo nano /etc/vsftpd.conf
按如下格式 修改,保存。
# Example config file /etc/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.
#
#
# Run standalone? vsftpd can run either from an inetd or as a standalone
# daemon started from an initscript.服务器监听
listen=YES
#
# 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.服务器监听
# listen_ipv6=YES
#
# Allow anonymous FTP? (Disabled by default).禁止匿名访问
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
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.
#开放上传权限, 匿名用户可以上传
#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
#
# If enabled, vsftpd will display directory listings with the time
# in your local time zone. The default is to display GMT. The
# times returned by the MDTM FTP command are also affected by this
# option.
use_localtime=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
# 支持数据流的被动式连接模式
# pasv_enable=NO
# 被动模式配置方法:
# 设置是否允许被动模式
pasv_enable=YES
pasv_min_port=3000
pasv_max_port=5000
#
# 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!
# 改变上传文件的所有者为root
chown_uploads=YES
chown_username=root
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
xferlog_file=/var/log/vsftpd.log
#
# 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.
# 设置空闲的用户会话的中断时间, 将在用户会话空闲10分钟后被中断
idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
# 设置空闲的数据连接的的中断时间,将在数据连接空闲2分钟后被中断
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.
# 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=Welcome to blah FTP service.
#
# 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 restrict local users to their home directories. See the FAQ for
# the possible risks in this before using chroot_local_user or
# chroot_list_enable below.
#设置对所有的本地用户执行chroot(即活动范围限制在自家目录)只要将chroot_local_user的值设 为YES即可
chroot_local_user=YES
allow_writeable_chroot=YES
#
# 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)
#chroot_list_enable=YES
# (default follows)创建可以登录FTP的用户列表每个用户名一行
#chroot_list_file=/etc/vsftpd.chroot_list
# 指定本地ftp根目录或者可以用创建虚拟用户时指定的用户目录
# local_root=/mnt
# 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
#
# Customization
#
# Some of vsftpd's settings don't fit the filesystem layout by
# default.
#
# This option should be the name of a directory which is empty. Also, the
# directory should not be writable by the ftp user. This directory is used
# as a secure chroot() jail at times vsftpd does not require filesystem
# access.
secure_chroot_dir=/var/run/vsftpd/empty
#
# This string is the name of the PAM service vsftpd will use.
pam_service_name=vsftpd
#
# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
#rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
#rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
#ssl_enable=NO
#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
#utf8_filesystem=YES
#
#设置最大传输速率限制
#使本地用户的最大传输速率为50kbytes / sec,匿名用户的 传输速率为30 kbytes / sec
#local_max_rate=50000
#anon_max_rate=30000
#限制FTP的最大连接数和每个IP的最大连接数
max_clients=100
max_per_ip=5
listen=YES
listen_ipv6=NO
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
#是否激活目录欢迎信息功能
#当用户用CMD模式首次访问服务器上某个目录时,FTP服务器将显示欢迎信息
#默认情况下,欢迎信息是通过该目录下的.message文件获得的
#此文件保存自定义的欢迎信息,由用户自己建立
#dirmessage_enable=YES
use_localtime=YES
#表明FTP服务器记录上传下载的情况
xferlog_enable=YES
#是否允许匿名用户上传文件,须将全局的write_enable=YES。默认为YES
#anon_upload_enable=YES
#是否允许匿名用户创建新文件夹
#anon_mkdir_write_enable=YES
### 主动模式配置方法:
# 主动式连接使用的数据通道
#ftp-data数据传输,21为连接控制端口
# connect_from_port_20=YES
# 支持数据流的被动式连接模式
# pasv_enable=NO
### 被动模式配置方法:
# 设置是否允许被动模式
connect_from_port_20=NO
pasv_enable=YES
pasv_min_port=3000
pasv_max_port=5000
###
#nopriv_user=nobody
#设定是否允许改变上传文件的属主,与下面一个设定项配合使用
#注意,不推荐使用root用户上传文件
#chown_uploads=YES
#设置想要改变的上传文件的属主,如果需要,则输入一个系统用户名
#可以把上传的文件都改成root属主。whoever:任何人
#chown_username=whoever
###
#表明将记录的上传下载情况
xferlog_std_format=YES
xferlog_file=/var/log/vsftp_xferlog.log
#启用双份日志
dual_log_enable=YES
#用来记录服务器的传输情况
vsftpd_log_file=/var/log/vsftpd.log
idle_session_timeout=600
data_connection_timeout=120
#ascii_upload_enable=YES
#ascii_download_enable=YES
chroot_local_user=YES
allow_writeable_chroot=YES
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd.chroot_list
###
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
###
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
#黑名单设置。如果很讨厌某些email address,就可以使用此设定来取消他的登录权限
#可以将某些特殊的email address抵挡住
#deny_email_enable=YES
#当上面的deny_email_enable=YES时,可以利用这个设定项来规定哪些邮件地址不可登录vsftpd服务器
#此文件需用户自己创建,一行一个email address即可
#banned_email_file=/etc/vsftpd/banned_emails
###
#是否允许递归查询。默认为关闭,以防止远程用户造成过量的I/O
#ls_recurse_enable=YES
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
utf8_filesystem=YES
userlist_file=/etc/vsftpd.user_list
userlist_enable=YES
#1)vsftpd.user_list本地用户不能访问,而其他本地用户可访问
#userlist_deny=YES
#2)vsftpd.user_list本地用户可以访问,而其他本地用户不可访问
userlist_deny=NO
###
#是否使用tcp_wrappers作为主机访问控制方式
#tcp_wrappers可以实现linux系统中网络服务的基于主机地址的访问控制
#在/etc目录中的hosts.allow和hosts.deny两个文件用于设置tcp_wrappers的访问控制
#前者设置允许访问记录,后者设置拒绝访问记录。
#如想限制某些主机对FTP服务器192.168.57.2的匿名访问,编缉/etc/hosts.allow文件,如在下面增加两行命令:
#vsftpd:192.168.57.1:DENY 和vsftpd:192.168.57.9:DENY
#表明限制IP为192.168.57.1/192.168.57.9主机访问IP为192.168.57.2的FTP服务器
#此时FTP服务器虽可以PING通,但无法连接
#tcp_wrappers=YES
###
#设置最大传输速率限制
#设置项 用于设置本地用户的最大传输速率,单位为B/s,值为0时表示不限制
#表示FTP服务器的本地用户最大传输速率设置为500KB/s.
#local_max_rate=500000
#用于设置匿名用户的最大传输速率,单位为B/s,值为0表示不限制。
#表示FTP服务器的匿名用户最大传输速率设置为200KB/s.
#anon_max_rate=200000
#限制FTP的最大连接数和每个IP的最大连接数
#用于设置FTP服务器所允许的最大客户端连接数,值为0时表示不限制。例如max_clients=100表示FTP服务器的所有客户端最大连接数不超过100个
max_clients=100
max_per_ip=5
配置2
listen=YES
listen_ipv6=NO
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
#是否激活目录欢迎信息功能
#当用户用CMD模式首次访问服务器上某个目录时,FTP服务器将显示欢迎信息
#默认情况下,欢迎信息是通过该目录下的.message文件获得的
#此文件保存自定义的欢迎信息,由用户自己建立
#dirmessage_enable=YES
use_localtime=YES
#表明FTP服务器记录上传下载的情况
xferlog_enable=YES
#是否允许匿名用户上传文件,须将全局的write_enable=YES。默认为YES
#anon_upload_enable=YES
#是否允许匿名用户创建新文件夹
#anon_mkdir_write_enable=YES
### 主动模式配置方法:
# 主动式连接使用的数据通道
#ftp-data数据传输,21为连接控制端口
# connect_from_port_20=YES
# 支持数据流的被动式连接模式
# pasv_enable=NO
### 被动模式配置方法:
# 设置是否允许被动模式
connect_from_port_20=NO
pasv_enable=YES
pasv_min_port=3000
pasv_max_port=5000
###
#nopriv_user=nobody
#设定是否允许改变上传文件的属主,与下面一个设定项配合使用
#注意,不推荐使用root用户上传文件
#chown_uploads=YES
#设置想要改变的上传文件的属主,如果需要,则输入一个系统用户名
#可以把上传的文件都改成root属主。whoever:任何人
#chown_username=whoever
###
#表明将记录的上传下载情况
xferlog_std_format=YES
xferlog_file=/var/log/vsftp_xferlog.log
#启用双份日志
dual_log_enable=YES
#用来记录服务器的传输情况
vsftpd_log_file=/var/log/vsftpd.log
idle_session_timeout=600
data_connection_timeout=120
#ascii_upload_enable=YES
#ascii_download_enable=YES
chroot_local_user=YES
allow_writeable_chroot=YES
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd.chroot_list
###
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
###
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
#黑名单设置。如果很讨厌某些email address,就可以使用此设定来取消他的登录权限
#可以将某些特殊的email address抵挡住
#deny_email_enable=YES
#当上面的deny_email_enable=YES时,可以利用这个设定项来规定哪些邮件地址不可登录vsftpd服务器
#此文件需用户自己创建,一行一个email address即可
#banned_email_file=/etc/vsftpd/banned_emails
###
#是否允许递归查询。默认为关闭,以防止远程用户造成过量的I/O
#ls_recurse_enable=YES
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
utf8_filesystem=YES
userlist_file=/etc/vsftpd.user_list
userlist_enable=YES
#1)vsftpd.user_list本地用户不能访问,而其他本地用户可访问
#userlist_deny=YES
#2)vsftpd.user_list本地用户可以访问,而其他本地用户不可访问
userlist_deny=NO
###
#是否使用tcp_wrappers作为主机访问控制方式
#tcp_wrappers可以实现linux系统中网络服务的基于主机地址的访问控制
#在/etc目录中的hosts.allow和hosts.deny两个文件用于设置tcp_wrappers的访问控制
#前者设置允许访问记录,后者设置拒绝访问记录。
#如想限制某些主机对FTP服务器192.168.57.2的匿名访问,编缉/etc/hosts.allow文件,如在下面增加两行命令:
#vsftpd:192.168.57.1:DENY 和vsftpd:192.168.57.9:DENY
#表明限制IP为192.168.57.1/192.168.57.9主机访问IP为192.168.57.2的FTP服务器
#此时FTP服务器虽可以PING通,但无法连接
#tcp_wrappers=YES
###
#设置最大传输速率限制
#设置项 用于设置本地用户的最大传输速率,单位为B/s,值为0时表示不限制
#表示FTP服务器的本地用户最大传输速率设置为500KB/s.
#local_max_rate=500000
#用于设置匿名用户的最大传输速率,单位为B/s,值为0表示不限制。
#表示FTP服务器的匿名用户最大传输速率设置为200KB/s.
#anon_max_rate=200000
#限制FTP的最大连接数和每个IP的最大连接数
#用于设置FTP服务器所允许的最大客户端连接数,值为0时表示不限制。例如max_clients=100表示FTP服务器的所有客户端最大连接数不超过100个
max_clients=100
max_per_ip=5
- 设置ftp相关目录及权限
$ sudo mkdir -p /mnt/ftp/upload $ sudo mkdir -p /mnt/ftp/download $ sudo chmod 755 /mnt/ftp $ sudo chmod 777 /mnt/ftp/download $ sudo chmod 755 /mnt/ftp/upload
- 启动vsftpd
sudo service vsftpd start
验证是否启动成功
$ ps aux | grep vsftp root 2864 0.0 0.0 4512 1044 ? S 08:29 0:00 /usr/sbin/vsftpd
- 增加组及用户并设置用户口令
$ sudo groupadd ftpgroup $ sudo useradd -g ftpgroup -d /mnt/ftp/upload -s /sbin/nologin -M bob $ sudo passwd bob $ sudo usermod -s /sbin/nologin bob $ sudo usermod -d /mnt/ftp/download bob
- 编辑chroot_list文件:
sudo nano /etc/vsftpd.chroot_list echo "bob" >> /etc/vsftpd.chroot_list
内容为ftp用户名,每个用户占一行
- 重新启动vsftpd:
$ sudo service vsftpd start
问题
- 本地用户使用/sbin/nologin作为shell后无法登录
解决方法:将/sbin/nologin追加到文件/etc/shells中echo "/sbin/nologin" >> /etc/shells
/bin/false 和 /usr/sbin/nologin 拒绝用户登录及其功能分析
添加一个不能登录的用户useradd -d /usr/local/apache -g apache -s /bin/false apache
要拒绝系统用户登录,可以将其shell设置为/usr/sbin/nologin或者/bin/false
usermod -s | --shell /usr/sbin/nologin username # 或者 usermod -s | -shell /bin/false username
说明及比较:
- /bin/false:/bin/false什么也不做只是返回一个错误状态,然后立即退出。将用户的shell设置为/bin/false,用户会无法登录,并且不会有任何提示。
- /usr/sbin/nologin:nologin会礼貌的向用户显示一条信息,并拒绝用户登录:This account is currently not available.
有一些软件,比如一些ftp服务器软件,对于本地非虚拟账户,只有用户有有效的shell才能使用ftp服务。这时候就可以使用nologin使用户即不能登录系统,还能使用一些系统服务,比如ftp服务。/bin/false则不行,这是二者的重要区别之一。
如果存在/etc/nologin文件,则系统只允许root用户登录,其他用户全部被拒绝登录,并向他们显示/etc/nologin文件的内容。
其他用户管理用法
锁定用户账户passwd -l | --lock username
解锁用户账户
passwd -u | --unlock username
删除用户密码
passwd -d | --delete username
- FTP:vsftpd中的local_umask和anon_umask
umask是在linux中常见的一个东西,它其实是一个掩码。当然,也有umask这样一个命令,它是对用户建立的文件的默认属性的定义。该 定义为:
假设umask为022,则对于一个文件夹的话,它的默认属性为 777-022=755,这也就是我们平时建立文件夹的权限。而对于一般的文件的话,则是用 666-022=644.
umask是unix操作系统的概念,umask决定目录和文件被创建时得到的初始权限
umask = 022 时,新建的目录 权限是755,文件的权限是 644
umask = 077 时,新建的目录 权限是700,文件的权限时 600
vsftpd的local_umask和anon_umask借鉴了它
默认情况下vsftp上传之后文件的权限是600,目录权限是700
想要修改上传之后文件的权限,有两种情况
如果使用vsftp的是本地用户
则要修改配置文件中的 local_umask 的值
如果使用vsftp的是虚拟用户
则要修改配置文件中的 anon_umask 的值
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。