FTP服务器搭建与配置

  • FTP介绍
  • 使用vsftpd搭建ftp服务
  • xshell使用sftp传输文件
  • xshell使用xftp传输文件
  • 使用pure-ftpd搭建ftp服务

FTP介绍

FTP是File Transfer Protocol(文件传输协议,简称文件协议)的简称,用于在Internet上控制文件的双向传输。
FTP的主要作用就是让用户连接一个远程计算机(这些计算机上运行着FTP服务器程序)
并查看远程计算机中的文件,然后把文件从远程计算机复制到要地计算机,或是本地计算机的文件传送到远程计算机。
小公司用的多,大企业不用FTP,因为不安全。

使用vsftpd搭建ftp服务 安装vsftpd服务(192.168.221.10)

yum install vsftpd db4-utils -y  //db4-utils包用来生成密码库文件

建立与虚拟账号相关联的系统账号

useradd -s /sbin/nologin virftp

建立与虚拟账号相关的文件,更改权限,生成对应的库文件

vim /etc/vsftpd/vsftpd_login //奇数行为用户名,偶数行为密码
zhangsan
123456
lisi
654321
chmod 600 /etc/vsftpd/vsftpd_login
db_load -T -t hash -f /etc/vsftpd/vsftpd_login /etc/vsftpd/vsftpd_login.db

建立与虚拟账号相关的目录以及配置文件(复制一份lisi的配置文件)

mkdir /etc/vsftpd/vsftpd_user_conf
cd /etc/vsftpd/vsftpd_user_conf
vim zhangsan //以下是文件内容
local_root=/home/virftp/zhangsan
anonymous_enable=NO
write_enable=YES
local_umask=022
anon_upload_enable=NO
idle_session_timeout=600
data_connection_timeout=120
max_clients=10
max_per_ip=5
local_max_rate=50000

创建虚拟用户的家目录和文件

mkdir /home/virftp/zhangsan
echo "zhangsan" > /home/virftp/zhangsan/zhangsan.txt

pam机制认证(让系统找到虚拟用户的密码文件)

vim /etc/pam.d/vsftpd //添加如下两行
auth sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
account sufficient /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login

修改virftp家目录下的目录与文件的所有者、组为virftp

chown -R virftp:virftp /home/virftp/

修改主配置文件/etc/vsftpd/vsftpd.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/vsftpd_user_conf
allow_writeable_chroot=YES

启动vsftpd服务

systemctl start vsftpd

在vsftpd客户端上(192.168.221.20)安装lftp并访问ftp服务器(192.168.221.10)

yum install lftp.x86_64 -y
lftp zhangsan@192.168.221.20  //如果一直连不上,说明有防火墙

用户名或密码输错,就会报530 测试

[root@apenglinux-002 ~]# lftp zhangsan@192.168.221.10
口令: 
lftp zhangsan@192.168.221.10:~> ls       
-rw-r--r--    1 1000     1000            9 Mar 04 12:32 zhangsan.txt

Xshell使用sftp传输文件

sftp:/root> help
bye     finish your SFTP session
cd      change your remote working directory
clear   clear screen
exit    finish your SFTP session
explore explore your local directory
get     download a file from the server to your local machine
help    give help
lcd     change and/or print local working directory
lls     list contents of a local directory
lpwd    print your local working directory
ls      list contents of a remote directory
mkdir   create a directory on the remote server
mv      move or rename a file on the remote server
put     upload a file from your local machine to the server
pwd     print your remote working directory
quit    finish your SFTP session
rename  move or rename a file on the remote server
rm      delete a file
rmdir   remove a directory on the remote server

sftp:/root> put   //会弹出一个窗口,可选择一个文件上传
Uploading vsftpd.txt to remote:/root/vsftpd.txt
sftp: sent 1.16 KB in 0.02 seconds

上传了一个vsftpd.txt文件到Linux中,发现中文乱码,则按如下解决

iconv -f gb2312 vsftpd.txt -o vs.txt //将编码为gb2312的文件vsftpd.txt转为linux可辨别的编码文件vs.txt

iconv命令

-f,--from-code=名称 原始文本编码
-t,--to-code=名称 输出编码
-o,--output=file 输出文件
-l,--list 列出所有已知的字符集

Xshell使用xftp传输文件

打开xshell连接到linux服务器以后,按ctrl+alt+f进入xftp的下载页面
安装xftp
xshell连接到Linux服务器以后, ctrl+alt+f就可以连上linux服务器
双击就可下载/上传文件了。
https://www.netsarang.com/news/ver6_beta_release.html

使用pure-ftpd搭建ftp服务 安装pure-ftpd

yum install epel-release -y
yum install pure-ftpd -y

配置pure-ftpd

vim /etc/pure-ftpd/pure-ftpd.conf
# PureDB                        /etc/pure-ftpd/pureftpd.pdb  //去掉最前面的"#"
systemctl start pure-ftpd.service

建立系统账号,虚拟账号,虚拟账号的根目录及测试文件

mkdir -p /pure-ftpd/zhangsan
echo "zhangsan" > /pure-ftpd/zhangsan/zhangsan.txt
useradd -s /sbin/nologin pure-ftp
pure-pw useradd zhangsan -u pure-ftp -d /pure-ftpd/zhangsan/  //输入密码两次

创建用户信息数据库文件

pure-pw mkdb

将虚拟用户的家目录和属主、组改为系统用户pure-ftp

chown -R pure-ftp:pure-ftp /pure-ftpd/zhangsan/

在同一机器上安装ftp客户端软件lftp

yum install lftp -y

访问ftp服务器

lftp zhangsan@127.0.0.1  //访问成功
口令: 
lftp zhangsan@127.0.0.1:~> ls       
drwxr-xr-x    2 1000       pure-ftp           26 Mar  5 07:58 .
drwxr-xr-x    2 1000       pure-ftp           26 Mar  5 07:58 ..
-rw-r--r--    1 1000       pure-ftp            9 Mar  5 07:58 zhangsan.txt

在另外一台机器上安装lftp并访问ftp服务器(需要关闭服务器的防火墙)

yum install lftp -y
lftp zhangsan@192.168.221.20
口令: 
lftp zhangsan@192.168.221.20:~> ls       
drwxr-xr-x    2 1000       pure-ftp           26 Mar  5 07:58 .
drwxr-xr-x    2 1000       pure-ftp           26 Mar  5 07:58 ..
-rw-r--r--    1 1000       pure-ftp            9 Mar  5 07:58 zhangsan.txt