搭建ftp服务

搭建ftp服务

ftp服务:在可以上传文件,共享文件

环境:准备两台服务器
vsftp 192.168.184.137
lftp 192.168.184.150

1、基础配置

(1)关闭防火墙、selinux

[root@vsftp ~]# systemctl stop firewalld       # 关闭防火墙
[root@vsftp ~]# systemctl disable firewalld    # 开机自动关闭防火墙
[root@vsftp ~]# getenforce                     #查看selinux的状态
enforcing  
[root@vsftp ~]# setenforce 0                   # 临时关闭selinux
[root@vsftp ~]# vim /etc/selinux/config        #永久关闭selinux,要重启机器才生效(reboot)
SELINUX=disabled                                   #将enforcing修改为disabled 

(2)对服务器进行解析(解析是为了在本文档中区分服务器,不解析也可以),重新连接配置生效

在服务器1上

[root@vsftp ~]# vim /etc/hosts
192.168.184.137   vsftp
192.168.184.150   lftp
[root@vsftp ~]# hostnamectl set-hostname vsftp

在服务器2上

[root@vsftp ~]# vim /etc/hosts
192.168.184.137   vsftp
192.168.184.150   lftp
[root@vsftp ~]# hostnamectl set-hostname lftp 

2、在服务端操作

(1)下载安装vsftpd服务(vsftp的全称为Very Secure FTP)

[root@vsftp ~]# yum -y install vsftpd
[root@vsftp ~]# systemctl restart vsftpd
[root@vsftp ~]# systemctl enable vsftpd

(3)ftp服务的文件存放在/var/ftp中,可以在目录下创建文件及目录

[root@vsftp ~]# cd /var/ftp
[root@vsftp ftp]# ls
pub
[root@vsftp ftp]# mkdir test
[root@vsftp ftp]# cd test
[root@vsftp test]# echo 123 > 1.txt
[root@vsftp test]# echo qazwsx > 2.txt

(4)修改配置文件

将【anon_upload_enable=YES】和【anon_mkdir_write_enable=YES】前的注释去掉,这两个分别表示匿名用户可上传文件、匿名用户可写入内容。

[root@vsftp ftp]# vim /etc/vsftpd/vsftpd.conf
anon_upload_enable=YES             #去掉注释(在29行)
anon_mkdir_write_enable=YES        #去掉注释(在33行)
[root@vsftp ftp]# systemctl restart vsftpd

3、在客户端操作

(1)安装lftp服务来连接(也可以服务端安装lftp,用服务器充当客户端来测试)

[root@lftp ~]# yum -y install lftp

(2)测试

连接到ftp服务器上,并下载文件(put),可以用-o参数下载到指定目录

[root@lftp ~]# lftp 192.168.184.137
lftp 192.168.184.137:~> ls
drwxr-xr-x    2 0        0               6 Oct 30  2018 pub
drwxr-xr-x    2 0        0              19 Oct 14 22:11 test
lftp 192.168.184.137:/> cd test/
lftp 192.168.184.137:/test> ls
-rw-r--r--    1 0        0               4 Oct 14 22:11 1.txt
-rw-r--r--    1 0        0               4 Oct 14 22:12 2.txt
lftp 192.168.184.137:/test> get 1.txt            #下载文件
4 bytes transferred
lftp 192.168.184.137:/test> get 2.txt -o /tmp/   #-o参数下载到指定目录
5 bytes transferred
lftp 192.168.184.137:/test> exit                 #exit退出ftp      
[root@lftp ~]#  ls
1.txt  anaconda-ks.cfg
[root@lftp ~]# cd /tmp/
[root@lftp tmp]# ls
3.txt

(3)连接到ftp服务器上,上传文件

[root@lftp ~]# lftp 192.168.184.137
lftp 192.168.184.137:~> ls
drwxr-xr-x    2 0        0               6 Oct 30  2018 pub
drwxrwxrwx    2 0        0              32 Oct 15 10:46 test
lftp 192.168.184.137:/> cd test/
lftp 192.168.184.137:/test> ls
-rwxr-xr-x    1 0        0               4 Oct 14 22:11 1.txt
-rwxr-xr-x    1 0        0               5 Oct 15 10:46 3.txt
lftp 192.168.184.137:/test> put 3.txt 
put: Access failed: 553 Could not create file. (2.txt)          #没有权限,解决方法如下
lftp 192.168.184.137:/test> exit

在服务端操作,修改权限

[root@vsftp ~]# cd /var/ftp/
[root@vsftp ftp]# chmod 777 test/
[root@vsftp ftp]# ll test/
total 0
drwxr-xr-x 2 root root  6 Oct 31  2018 pub
drwxrwxrwx 2 root root 32 Oct 15 18:46 test
[root@vsftp ftp]# systemctl start vsftpd

回到客户端测试

[root@lftp ~]# lftp 192.168.184.137
lftp 192.168.184.137:/> cd test/
lftp 192.168.184.137:/test> put 3.txt 
6 bytes transferred                         #上传成功
lftp 192.168.184.137:/test> exit

(3)下载目录的命令 — mirror 目录名

[root@lftp ~]# lftp 192.168.184.137
lftp 192.168.184.137:/> ls
drwxr-xr-x    2 0        0               6 Oct 30  2018 pub
drwxr-xr-x    2 0        0              32 Oct 15 10:46 test
lftp 192.168.184.137:/> mirror test/         #下载目录
Total: 1 directory, 2 files, 0 symlinks
New: 2 files, 0 symlinks
9 bytes transferred
lftp 192.168.184.137:/> exit
[root@lftp ~]# ls
1.txt  2.txt  anaconda-ks.cfg  test

(4)exit退出ftp

错误汇总

1、出现如下错误:

[root@lftp ~]# lftp 192.168.184.137
lftp 192.168.184.137:~> ls 
`ls' at 0 [Delaying before reconnect: 9]

解决思路:

(1)查看两台服务器selinux和防火墙的状态(必须要处于关闭状态)

###服务端
[root@vsftp ~]# systemctl status firewalld
[root@vsftp ~]# getenforce
###客户端
[root@lftp ~]# systemctl status firewalld
[root@lftp ~]# getenforce

如果是开启状态,请参考本文档的第一步,进行操作

(2)查看服务端是否开启vsftpd(必须要处于开启状态)

[root@vsftp ~]# systemctl status vsftpd

2、匿名用户上传失败、下载失败的问题

客户端不能上传,错误如下:

lftp 192.168.184.137:/test> put 3.txt 
put: Access failed: 553 Could not create file. (2.txt)

解决方法咋上文已讲解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值