ubuntu18系统搭建ftp服务器详解

版权声明:转载请注明出处: https://blog.csdn.net/qq_38701476/article/details/82932463

当我们有有的自己的服务器就可以搭建一个ftp服务器,可以自己放进去一些东西,这样以后下载的话会很方便,而且服务器端速度很快(所以快慢基本就看自己下载段的速度了.

1. 更新一些服务器上的文件文件源

sudo apt-get update

2. 下载vsftpd软件

sudo apt-get install vsftpd
之后就等着下完

3. 配置ftp登录的用户

ftp服务器也是和正常ssh登录一样,也是需要用户指令的.当我们下载完vsftpd后,我们系统上就会多出一个ftp用户和ftp组,当然这个ftp用户是不能直接登录的.因此,我们也要为ftp创建一个用户,当然这个用户所在的组就是ftp组.例如,创建一个test:123456 的用户,如下:

sudo useradd test -g ftp -d /home/    //在/home文件夹下创建该用户
之后会提示你密码,当然太简单的密码无法通过

4. 配置vsftpd配置

在/etc/目录下有个vsftp.conf文件,该文件是ftp服务器的主要配置文件,用vm打开,对里面的文件进行修改.下面贴出我的文件:

后面会对其中一些做出简要说明,要说明的的放会有 注(num):

# 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=NO  #注1: 
#
# 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  #注2:
userlist_deny=YES

#
# 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#注3
#
# 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
#
# 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/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.
#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.
# 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=hellow! Welcome to Curve's FTP service.#注4
#
# 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_local_user=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_local_user=YES           #注5
chroot_list_enable=YES
local_root=/var/ftp        #注6
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list
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
#
# 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

LISTEN = NO :这个值只能为NO,如果改为YES的话,启动时会出错
anonymous_enable=NO :是否允许匿名登录,不做匿名的话就不重要
write_enable=YES :设置可以上传文件
ftpd_banner= :设置登录是可以看到的信息,可以任意修改

chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list
对ftp登录的用户进行限制具体请参考

local_root=/var/ftp :设置根目录所在的位置,可以自己设置.注意可能会需要权限此时加上下面两条语句,如这个例子,
chown -R test ftp //test为要ftp登录的用户名,第二个为文件夹
chgrp -R ftp ftp //第一个ftp 为ftp组

5. 对ftp用户访问进行配置

  • 设置test用户只能够ftp登录,不能ssh登录

    usermod -s /sbin/nologin test
    
  • 修改/etc/ftpusers.这个目录是设置禁止ftp登录用户的因此,如果有自己想让登录的用户,那就直接删掉

  • 增加/etc/vsftpd.chroot_list文件,这个和上面那个注5是相关的,在我的配置下的话,要增加用户名,因此直接增加test

  • 此时当你试着连接时,可能仍旧出现“530 Login incorrect”,修改/etc/shells文件,后面添加以下内容:/sbin/nologin/

  • 登录时可能会出现的错误"refusing to run with writable root inside chroot()",问题在于启用了chroot的话,根目录要设置为不可写

chmod a-w /home/test
  • 当然还能够限制那些用户可以登录,当如不加也是可以的参考源

    首先修改vsftpd.cong文件
     userlist_enable=YES //设置用户访问列表
     #允许登录的用户 
    userlist_file=/etc/allowed_users //设置该列表所在的位置
    #最后,对其进行修改,加入自己想要添加进的用户名即可
    ```
    
    
    

最最后,重新启动vsftpd进程

server vsftpd restart

6. 登录时常用登录命令

  • 自己可以下载个ftp连接的软件,这里说明一下linux终端命令所需的常用命令
  1. 登录:
    • ftp ip 例如ftp 1.1.1.1
    • 直接ftp进入ftp命令行 用open ip
      之后会让你输入用户名和密码
    • 关闭连接输入close
    • 推出ftp命令行bye
  2. 列出当下目录中文件内容:
    • ls或者dir
      注意用ls很多此的话,可能会出现连接错误,dir目前还为发现
  3. 目录切换
    cd 命令
  4. 下载文件
    • get|mget 目的文件 下载位置.
      get下载单个文件,mget下载多个文件.*.*代表所有文件
  5. 上传文件
    • put local_filt remote_file


下面附上学习时所参考文章的来源,感谢各位大佬文章
参考文章1   参考文章2   参考文章3  参考文章4 参考文章5  参考文章6
当然,还有一些学习是所看过的文章,由于看时还没有开始写这个文章.因此,有些知识也不知道了来源,因此没附地址.

猜你喜欢

转载自blog.csdn.net/qq_38701476/article/details/82932463