FTP是什么?
FTP(文件传输协议,File Transfer Protocol)是一种用于在计算机之间传输文件的网络协议。它基于客户端-服务器模型,允许用户从远程服务器上传、下载和管理文件。
FTP的主要作用
文件传输:FTP最基本的功能是实现不同计算机之间的文件传输,可以高效地上传和下载文件。
文件管理:除了传输文件,FTP还允许用户在服务器上进行文件管理操作,如创建、删除、重命名目录和文件。
批量传输:FTP支持批量文件的上传和下载,适合处理大量数据的场景。
支持大文件传输:相较于HTTP等协议,FTP更适合传输大文件,因为它支持断点续传功能。
跨平台兼容性:FTP可以在不同操作系统间工作,例如Windows、Linux和macOS,使得跨平台的文件交换变得更加简单。
一、服务端配置
1、服务端安装vsftpd服务
bash
这里使用网站架构中nfs-31作为服务端
(15:21:48)[nfs root ~] # yum install vsftpd -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
epel | 4.3 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:3.0.2-29.el7_9 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================
Package Arch Version Repository Size
===============================================================================================
Installing:
vsftpd x86_64 3.0.2-29.el7_9 updates 173 k
Transaction Summary
===============================================================================================
Install 1 Package
Total download size: 173 k
stalled size: 353 k
Downloading packages:
vsftpd-3.0.2-29.el7_9.x86_64.rpm | 173 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : vsftpd-3.0.2-29.el7_9.x86_64 1/1
Verifying : vsftpd-3.0.2-29.el7_9.x86_64 1/1
Installed:
vsftpd.x86_64 0:3.0.2-29.el7_9
Complete!
2、 创建一个普通用户(ftp使用)
bash
(15:22:05)[nfs root ~] # useradd test
(15:22:17)[nfs root ~] #
(15:22:18)[nfs root ~] # echo '1' | passwd --stdin test
Changing password for user test.
passwd: all authentication tokens updated successfully.
3、修改ftp配置文件,设置用于共享的目录
bash
(15:22:43)[nfs root ~] # rpm -ql vsftpd |grep '.conf$'
/etc/vsftpd/vsftpd.conf
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf
找出和匿名用户相关的配置参数,然后关闭
(15:22:44)[nfs root ~] # grep '^anonymous' /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
(15:23:06)[nfs root ~] #
(15:23:07)[nfs root ~] # vim /etc/vsftpd/vsftpd.conf
(15:23:32)[nfs root ~] # grep '^anonymous' /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
添加自定义的共享文件夹配置参数,配置文件最后添加
(15:23:35)[nfs root ~] # vim /etc/vsftpd/vsftpd.conf
local_root=/test_0224/
chroot_local_user=YES
allow_writeable_chroot=YES
# local_root=/data/kefu 指定本地用户的默认数据根目录
# chroot_local_user=YES 禁锢本地用户的默认数据目录(禁止用户切换到其他目录)
# allow_writeable_chroot=YES 允许ftp用户登录后,可以创建数据
4、创建共享文件夹,并修改文件夹权限为创建的用户
bash
创建目录和测试文件,更改文件所属用户权限
(15:25:15)[nfs root ~] # mkdir /test_0224/
(15:25:25)[nfs root ~] # touch /test_0224/test.txt
(15:25:55)[nfs root ~] #
(15:25:56)[nfs root ~] # chown -R test:test /test_0224/
(15:26:14)[nfs root ~] # ls -l /test_0224/
total 0
-rw-r--r-- 1 test test 0 Dec 6 15:25 test.txt
(15:26:24)[nfs root ~] # ll -d /test_0224/
drwxr-xr-x 2 test test 22 Dec 6 15:25 /test_0224/
5、重启vsftpd服务
bash
(15:26:38)[nfs root ~] # systemctl restart vsftpd
(15:26:48)[nfs root ~] # ps -ef|grep vsftpd
root 1383 1 0 15:26 ? 00:00:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root 1386 1222 0 15:26 pts/0 00:00:00 grep --color=auto vsftpd
二、客户端访问配置
1、安装下载ftp服务
bash
这里使用master-61作为客户端访问ftp
(15:28:45)[master root ~] # yum -y install ftp
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package ftp.x86_64 0:0.17-67.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================
Package Arch Version Repository Size
===============================================================================================
Installing:
ftp x86_64 0.17-67.el7 base 61 k
Transaction Summary
===============================================================================================
Install 1 Package
Total download size: 61 k
Installed size: 96 k
Downloading packages:
ftp-0.17-67.el7.x86_64.rpm | 61 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ftp-0.17-67.el7.x86_64 1/1
Verifying : ftp-0.17-67.el7.x86_64 1/1
Installed:
ftp.x86_64 0:0.17-67.el7
Complete!
2、ftp+ip直接访问
ftp下载文件
bash
(15:30:23)[master root ~] # ftp 10.0.0.31
Connected to 10.0.0.31 (10.0.0.31).
220 (vsFTPd 3.0.2)
Name (10.0.0.31:root): test #输入用户名
331 Please specify the password.
Password: #输入密码
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
ftp> pwd # 显示当前路径
257 "/"
ftp> get # 下载文件
(remote-file) test.txt
(local-file) test.txt
local: test.txt remote: test.txt
227 Entering Passive Mode (10,0,0,31,203,162).
150 Opening BINARY mode data connection for test.txt (0 bytes).
226 Transfer complete.
ftp> exit
221 Goodbye.
可以看到已经下载了test.txt文件
(15:31:33)[master root ~] # ls
anaconda-ks.cfg network.sh test.txt
ftp上传文件
bash
客户端ftp创建了一个电影.mv文件
(15:52:21)[master root ~] # ls
anaconda-ks.cfg network.sh test.txt 电影.mv
(15:52:22)[master root ~] #
(15:32:10)[master root ~] # ftp 10.0.0.31
Connected to 10.0.0.31 (10.0.0.31).
220 (vsFTPd 3.0.2)
Name (10.0.0.31:root): test
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> put #上传文件
(local-file) /root/电影.mv # 文件目录位置
(remote-file) 电影.mv
local: /root/电影.mv remote: 电影.mv
227 Entering Passive Mode (10,0,0,31,63,145).
150 Ok to send data.
226 Transfer complete.
9 bytes sent in 7.1e-05 secs (126.76 Kbytes/sec)
ftp> exit
vfstpd服务端 /test_0224/下可以看到对应的文件了
(15:33:27)[nfs root ~] # cd /test_0224/
(15:33:34)[nfs root /test_0224] # ls
test.txt 电影.mv
(15:33:35)[nfs root /test_0224] #