Use vsftpd to build ftp

1. Install the vsftpd tool

Comes with vsftpd on centos

[root@host ~]# yum install -y vsftpd

2. Create a user

[root@host ~]# useradd -s /sbin/nologin virftp
#创建一个vsftpd服务的普通用户进行传输数据,避免使用root用户导致安全问题。

3. Configure the virtual user password file

[root@host ~]# vim /etc/vsftpd/vsftpd_login   //内容如下,奇数行为用户名,偶数行为密码,多个用户就写多行
testuser1
123456

[root@host ~]# chmod 600 /etc/vsftpd/vsftpd_login   //更改文件的权限

4. Password Conversion

Convert the password to a binary file that the computer can recognize

[root@host ~]# db_load -T -t hash -f /etc/vsftpd/vsftpd_login /etc/vsftpd/vsftpd_login.db

5. Configuration

[root@host ~]# mkdir /etc/vsftpd/vsftpd_user_conf    //创建存放目录

[root@host ~]# cd /etc/vsftpd/vsftpd_user_conf    

[root@host vsftpd_user_conf]# vim testuser1     //指定虚拟用户的 配置文件,虚拟用户配置文件和虚拟用户名称需要保持一致。
local_root=/home/virftp/testuser1
#定义虚拟用户家目录
anonymous_enable=NO
#是否允许匿名用户登录
write_enable=YES
#是否可写
local_umask=022
#定义创建新文件时的默认权限
anon_upload_enable=NO
#是否允许匿名用户上传文件
anon_mkdir_write_enable=NO
#是否允许匿名用户创建目录文件
idle_session_timeout=600
#空闲用户保留时间
data_connection_timeout=120
#数据传输超时时间
max_client=10
#客户端最大连接数量

6. Create a virtual user home directory

[root@host vsftpd_user_conf]# mkdir /home/virftp/testuser1

创建一个文件:
[root@host vsftpd_user_conf]# touch /home/virftp/testuser1/123.txt
[root@host vsftpd_user_conf]# chown -R virftp:virftp /home/virftp

7. Virtual User Password Matching

[root@host vsftpd_user_conf]# vim /etc/pam.d/vsftpd    //添加这两行内容,用于指定用户的密码文件位置
#%PAM-1.0
auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login

8. Edit the vsftpd main configuration file

[root@host vsftpd_user_conf]# vim /etc/vsftpd/vsftpd.conf
……
anonymous_enable=NO
anon_upload_enable=NO
anon_mkdir_write_enable=NO
……

在文件内容最后添加如下内容:  
chroot_local_user=YES
guest_enable=YES
guest_username=virftp
#开启虚拟用户和系统用户的映射
virtual_use_local_privs=YES
#使用虚拟用户
user_config_dir=/etc/vsftpd_user_conf
allow_writeable_chroot=YES

9. Start the FTP service

[root@host vsftpd_user_conf]# systemctl start vsftpd

[root@host vsftpd_user_conf]# ps aux |grep vsftpd
root      3671  0.0  0.0  52708  564 ?        Ss  18:40  0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
[root@host vsftpd_user_conf]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address          Foreign Address        State      PID/Program name
tcp6      0      0 :::21                  :::*                    LISTEN      3671/vsftpd

10. Test

  • For testing on Windows systems, you need to install the filezilla software.
  • To test in Linux, install the lftp tool.
[root@host ~]# yum install -y lftp

11. Usage

[root@host ~]# lftp [email protected]
口令: 
#登录
lftp [email protected]:~> ls      
drwxr-xr-x    2 1002    1002          22 Aug 24 10:19 testuser1
lftp [email protected]:/> ?
#查询在lftp中可执行的命令
#常用命令:put、get
lftp [email protected]:/> get testuser1/123.txt
lftp [email protected]:/> quit

[root@host ~]# ls
123.txt  anaconda-ks.cfg     //下载文件成功

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325063929&siteId=291194637