首先检查是否安装了ftp服务器:
vsftpd -version
如果没有安装则安装:
sudo apt-get install vsftpd
新建一个用户文件夹用于ftp工作目录:
mkdir /home/ftp
创建ftp用户:
sudo useradd -d /home/ftp -s /bin/bash ftpname
为新建的用户设置密码:
passwd ftpname
打开vsftpd.conf,设置属性值:
local_enable=YES
write_enable =YES
重新启动ftp服务器:
sudo /etc/init.d/vsftpd restart
修改ftp文件夹权限,否则客户端无法上传文件:
sudo chown ftpname:ftpname ftp
客户端连接并测试:
bekl@bekl:~/github$ ftp 192.168.1.123
Connected to 192.168.1.123.
220 (vsFTPd 3.0.3)
Name (192.168.1.123:bekl): zyq //这里我的ftpname为zyq
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
226 Directory send OK.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 1586882 Jul 14 15:23 magdec.ini
-rw-r--r-- 1 0 0 3432 Jul 14 15:23 package.ini
-rw-r--r-- 1 0 0 1108 Jul 14 15:23 param.ini
-rw-r--r-- 1 0 0 1027 Jul 14 15:23 power.ini
-rw-r--r-- 1 0 0 3777 Jul 14 15:23 pscan.ini
226 Directory send OK.
ftp> put fc.txt
local: fc.txt remote: fc.txt
200 PORT command successful. Consider using PASV.
553 Could not create file. //未修改权限前无法上传文件
ftp> put fc.txt
local: fc.txt remote: fc.txt
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp>
如果出现:
vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot
则:
/etc/vsftpd/vsftpd.conf
新增如下:
allow_writeable_chroot=YES #允许用户具有主目录写权限
C++的ftp库地址:
https://github.com/mkulke/ftplibpp.git
使用的时候直接包含ftplib.h就可以了
例子:
#include "ftplib.h"
int main()
{
ftplib * ftp = new ftplib();
ftp->Connect("192.168.1.123");
ftp->Login("zyq","0");
ftp->Dir("param.ini","./");
ftp->Get("magdec.ini","magdec.ini",ftplib::ascii);
}
注意编译的时候要连接 -lssl -lcrypto