在CentOS7环境下搭建FTP 服务器

实验环境:3.10.0-1062.el7.x86_64 CentOS 7 Windows Server 2012

1.安装ftp软件包,否则运行ftp指令时会出现command not find的问题

[[email protected] gyf]# yum install ftp

2.接下来安装vsftp,这是搭建ftp的关键软件包

 [[email protected] gyf]#yum install vsftpd

3.设置ftp服务为开机启动项

[[email protected] gyf]# chkconfig vsftpd on

4.重启服务

[[email protected] gyf]# service vsftpd restart

5.检查服务是否在运行

[[email protected] gyf]# netstat -antup|grep ftp

tcp6       0      0 :::21                   :::*                    LISTEN      1102/vsftpd    #这一行是运行结果

6.接下来修改FTP的配置文件

[[email protected] gyf]# vi /etc/vsftpd/vsftpd.conf 

根据你的需要决定是否允许匿名用户登录,如果不允许,就把默认的YES改为NO,如下

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO

接下来的操作会将登录用户的探索范围限制在其家目录范围内(为了安全,你应当这么做,除非你真的很信任登录者),你需要把下面黑色加粗字体的键值对前面的# 号去掉(默认是有#号的,这里已经将其去掉)。

# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
chroot_local_user=YES
#chroot_list_enable=YES

7.接下来你需要关闭selinux,把下面加粗字体的值改为disabled

 [[email protected] gyf]#vi /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

9.重启一下你的电脑吧

10.现在使用ipconfig命令查看一下你的计算机IP地址,并记好

11.运行ftp://IP地址

[[email protected] gyf]# ftp 192.168.50.134
Connected to 192.168.50.134 (192.168.50.134).
220 (vsFTPd 3.0.2)
Name (192.168.50.134:root): gyf
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed.

421 Service not available, remote server has closed connection
ftp> ^C
ftp> ^C
ftp> ^C
ftp> exit

12.发现错了吧,不要慌,这是因为你登录的用户的家目录权限问题。

补充说明:

从2.3.5之后,vsftpd软件增强了安全检查,如果用户被限定在了其主目录下(前面第6步我们干了这件事),则该用户的主目录不能再具有写权限了!

如果检查发现还有写权限,就会报该错误。 

要解决这个错误,用命令chmod a-w /home/user除去用户主目录的写权限。

这里的指令当然是:chmod a-w /home/gyf

现在再运行[[email protected] gyf]# ftp 192.168.50.134,结果如下:

Connected to 192.168.50.134 (192.168.50.134).
220 (vsFTPd 3.0.2)
Name (192.168.50.134:root): gyf
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,50,134,128,196).
150 Here comes the directory listing.
drwxr-xr-x    2 1000     1000            6 Mar 11 21:11 Desktop
drwxr-xr-x    2 1000     1000            6 Mar 11 21:11 Documents
drwxr-xr-x    2 1000     1000            6 Mar 11 21:11 Downloads
drwxrwxr-x    2 1000     1000            6 Mar 11 22:20 Learn
drwxr-xr-x    2 1000     1000            6 Mar 11 21:11 Music
drwxr-xr-x    2 1000     1000            6 Mar 11 21:11 Pictures
drwxr-xr-x    2 1000     1000            6 Mar 11 21:11 Public
drwxr-xr-x    2 1000     1000            6 Mar 11 21:11 Templates
drwxr-xr-x    2 1000     1000            6 Mar 11 21:11 Videos
226 Directory send OK.
ftp> exit
221 Goodbye.

可以看到家目录都被清晰的展示出来

你也可以使用浏览器登录FTP,在地址栏里填写ftp://192.168.50.134

然后在弹出的页面里填写登录名密码即可,如下:

在CentOS7环境下搭建FTP 服务器

13.我们架设服务器当然是为了让别人访问的,现在为了实现此目标,需要关闭CentOS7的防火墙。

[[email protected] gyf]# systemctl stop firewalld.service 

现在,打开其他计算机来测试一下:以Windows为例:如下图

在CentOS7环境下搭建FTP 服务器

OK,说明成功了。