且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ftp服务器详解

更新时间:2022-09-22 22:27:44

centos 5.1 ftp安装配置详解。
 
ftp文件传输协议(file transfer protocal ,ftp)顾名思义在文件传输的时候用通行协议,它出现在tcp/ip网络和internet上的时间很早。
 
FTP的特色:
稳定的传输机制,它是用两个连接口来客户端连接:tcp 20,tcp21,其中连接端口tcp 20用来传输数据,21用来传输流程控制,这种设计可以支持多个客户端同时连接ftp服务端,并且具有稳定性的特点。
1.交互式访问。

2.指定下载的文件格式。

3.身份验证控制。

4.提供跨平台的数据交换。
 
FTP的体系结构:

ftp是一种c/s客户端/服务器的通行协议,因此必须有两台主机,一台必须运行ftp服务端的软件,如:vsftp,windows平台Serv-U等软件,而另一台则需要ftp客户端程序,如ie6.0或ftp指令。文件传输包含两种形式 下载:文件由服务器发送客户端。上传文件由客户端发送到服务器。
ftp是用tcp传输控制协议,因此它是提供可靠的可信赖的面向连接。
vsftp服务器安装
vsftp(very secure ftp)非常安全的ftp服务器,可以在安装系统之前安装vsftp.
 
[root@ns1 ~]# rpm -ivh vsftpd-2.0.5-10.el5.i386.rpm //安装vsftp软件包,或者用yum来安装。
yum -y install vsftpd*
yum -y install pam*
yum -y install db4*
warning: vsftpd-2.0.5-10.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID e8562897
Preparing...                ########################################### [100%]
   1:vsftpd                 ########################################### [100%]
[root@ns1 ~]#/etc/rc.d/init.d/vsftpd start或/stop/restart启动停止重新启动
开机时候启动vsftpd
1.ntsysv 设置vsftp服务器的自动启动

2.以服务器配置设置vsftp的启动

3.chkconfig --level 3 vsftpd on

4.netstat -tl 可以查看ftp端口是否在监听

5.VSFTPD有两种开机自启动模式: inet模式和standalone模式,推荐使用standalone模式。为vsftpd添加ftp账号本地用户经过设置后可以进行ftp访问。而匿名用户的访问经过了转换,在系统中。匿名用户的用户名为ftp, 系统将其属性设置为 根目录 /var/ftp/, 禁止控制台登陆,也就是,该用户只能进行ftp访问。用useradd建立的用户,如果后面不加参数,建立的用户为普通用户,有系统登陆权限,所以要在useradd命令后面加上参数,为虚拟主机添加不可以登录系统的ftp专用账号,命令如下:

# useradd -s /sbin/nologin -d /var/www   -g ftp ftpadmin
如果显示如下信息:
Creating mailbox file: File exists
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
表示用户家目录已经存在(/var/www),不可再新建此目录,并非添加用户失败,接下来给用户设置密码,否则此账号不能使用,
 
命令如下:
# passwd ftpadmin
这样就为linux系统添加用户ftpadmin,用户目录指定为/var/www,属于ftp用户组,且此用户不能登陆系统。
注:

-s /sbin/nologin是让其不能登陆系统
-d 是指定用户目录为/var/www
-g ftp 把用户加入到ftp组中
修改/var/www目录属性:
$ chmod -R 777 /var/www 递归地给此目录下所有文件和子目录的读、写、执行权限
$ chgrp -R ftp   /var/www 递归地把此目录及该目录下所有文件和子目录的组属性设置成ftp组
 
//在runlevel 3的时候就会自动启动vsftpd这个守护进程。
测试vsftp服务器
[root@ns1 ~]#telnet ftp.ns1.com 21
telnet>q
connection closed.

注解:防火墙分三个等级high,medium,no firewall.在安装系统时候默认为中等,但是high,和medium等级是不允许ftp使用的,因此要修改为no firewall.或定制配置才可以启动ftp服务,要修改防火墙等级,首先终端setup指令,然后启动firewall configuration设置选项,然后再加以设置。

客户端的连接
 
以浏览器连接到vsftp服务器
浏览器一般人上网不可缺少的部分,但是除了浏览网页外,还有的用处就是连接ftp服务器最方便的客户端程序之一,用ie浏览url前加入ftp://ip或域名即可。而不是http://或www啊。还有些常见的工具客户端,cuteftp,ws-ftp ,bullet proof ftp,flashfxp等。如果要输入详细的用户名和密码和ftp地址一起连用以下为格式
在浏览器中ftp://用户名:密码@ftp服务器地址。
如果要用windows平台的cmd可以直接ftp
open to ip即可,然后输入?可以显示出详细的命令,以下是搜集的命令的详解参数。
FTP的命令行格式为: ftp -v -d -i -n -g [主机名] ,其中
  -v 显示远程服务器的所有响应信息;
-n 限制ftp的自动登录,即不使用;
.n etrc文件;
-d 使用调试方式;
-g 取消全局文件名。
ftp使用的内部命令如下(中括号表示可选项):
1.![cmd[args]]:在本地机中执行交互shell,exit回到ftp环境,如:
!ls*.zip.
2.$ macro-ame[args]:执行宏定义macro-name.
3.account[password]:提供登录远程系统成功后访问系统资源所需的补
充口令。
4.append local-file[remote-file]:将本地文件追加到远程系统主机,
若未指定远程系统文件名,则使用本地文件名。
5.ascii:使用ascii类型传输方式。
6.bell:每个命令执行完毕后计算机响铃一次。
7.bin:使用二进制文件传输方式。
8.bye:退出ftp会话过程。
9.case:在使用mget时,将远程主机文件名中的大写转为小写字母。
10.cd remote-dir:进入远程主机目录。
11.cdup:进入远程主机目录的父目录。
12.chmod mode file-name:将远程主机文件file-name的存取方式设置为
mode,如: chmod 777 a.out 。
13.close:中断与远程服务器的ftp会话(与open对应)。
14.cr:使用asscii方式传输文件时,将回车换行转换为回行。
15.delete remote-file:删除远程主机文件。
16.debug[debug-value]:设置调试方式, 显示发送至远程主机的每条命
令,如: deb up 3,若设为0,表示取消debug。
17.dir[remote-dir][local-file]:显示远程主机目录,并将结果存入本
地文件local-file。
18.disconnection:同close。
19.form format:将文件传输方式设置为format,缺省为file方式。
20.get remote-file[local-file]: 将远程主机的文件remote-file传至
本地硬盘的local-file。
21.glob:设置mdelete,mget,mput的文件名扩展,缺省时不扩展文件名,
同命令行的-g参数。
22.hash:每传输1024字节,显示一个hash符号(#)。
23.help[cmd]:显示ftp内部命令cmd的帮助信息,如:help get。
24.idle[seconds]:将远程服务器的休眠计时器设为[seconds]秒。
25.image:设置二进制传输方式(同binary)。
26.lcd[dir]:将本地工作目录切换至dir。
27.ls[remote-dir][local-file]:显示远程目录remote-dir, 并存入本
地文件local-file。
28.macdef macro-name:定义一个宏,遇到macdef下的空行时,宏定义结
束。
29.mdelete[remote-file]:删除远程主机文件。
30.mdir remote-files local-file:与dir类似,但可指定多个远程文件,
如: mdir *.o.*.zipoutfile 。
31.mget remote-files:传输多个远程文件。
32.mkdir dir-name:在远程主机中建一目录。
33.mls remote-file local-file:同nlist,但可指定多个文件名。
34.mode[modename]:将文件传输方式设置为modename, 缺省为stream方
式。
35.modtime file-name:显示远程主机文件的最后修改时间。
36.mput local-file:将多个文件传输至远程主机。
37.newer file-name: 如果远程机中file-name的修改时间比本地硬盘同
名文件的时间更近,则重传该文件。
38.nlist[remote-dir][local-file]:显示远程主机目录的文件清单,并
存入本地硬盘的local-file。
39.nmap[inpattern outpattern]:设置文件名映射机制, 使得文件传输
时,文件中的某些字符相互转换,如:nmap $1.$2.$3[$1,$2].[$2,$3],则
传输文件a1.a2.a3时,文件名变为a1,a2。该命令特别适用于远程主机为非UNIX
机的情况。
40.ntrans[inchars[outchars]]:设置文件名字符的翻译机制,如ntrans
1R,则文件名LLL将变为RRR。
41.open host[port]:建立指定ftp服务器连接,可指定连接端口。
42.passive:进入被动传输方式。
43.prompt:设置多个文件传输时的交互提示。
44.proxy ftp-cmd:在次要控制连接中,执行一条ftp命令, 该命令允许
连接两个ftp服务器,以在两个服务器间传输文件。第一条ftp命令必须为open,
以首先建立两个服务器间的连接。
45.put local-file[remote-file]:将本地文件local-file传送至远程主
机。
46.pwd:显示远程主机的当前工作目录。
47.quit:同bye,退出ftp会话。
48.quote arg1,arg2...:将参数逐字发至远程ftp服务器,如:
quote syst.
49.recv remote-file[local-file]:同get。
50.reget remote-file[local-file]:类似于get,但若local-file存在,
则从上次传输中断处续传。
51.rhelp[cmd-name]:请求获得远程主机的帮助。
52.rstatus[file-name]:若未指定文件名,则显示远程主机的状态,否
则显示文件状态。
53.rename[from][to]:更改远程主机文件名。
54.reset:清除回答队列。
55.restart marker:从指定的标志marker处,重新开始get或put,如:
restart 130。
56.rmdir dir-name:删除远程主机目录。
57.runique:设置文件名唯一性存储,若文件存在,则在原文件后加后缀
..1,.2等。
58.send local-file[remote-file]:同put。
59.sendport:设置PORT命令的使用。
60.site arg1,arg2...:将参数作为SITE命令逐字发送至远程ftp主机。
61.size file-name:显示远程主机文件大小,如:site idle 7200。
62.status:显示当前ftp状态。
63.struct[struct-name]:将文件传输结构设置为struct-name, 缺省时
使用stream结构。
64.sunique:将远程主机文件名存储设置为唯一(与runique对应)。
65.system:显示远程主机的操作系统类型。
66.tenex:将文件传输类型设置为TENEX机的所需的类型。
67.tick:设置传输时的字节计数器。
68.trace:设置包跟踪。
69.type[type-name]:设置文件传输类型为type-name,缺省为ascii,如:
type binary,设置二进制传输方式。
70.umask[newmask]:将远程服务器的缺省umask设置为newmask,如:
umask 3。
71.user user-name[password][account]:向远程主机表明自己的身份,
需要口令时,必须输入口令,如:user anonymous my@email
72.verbose:同命令行的-v参数,即设置详尽报告方式,ftp服务器的所有
响应都将显示给用户,缺省为on.
73.?[cmd]:同help.
 

用户管理:
/etc/vsftpd/ftpusers文件时vsftp服务器中用来管理登录账号的配置文件,但是它最特别的一点就是指定不允许登录账号。以下为ftpusers的内容。
[root@ns1 ~]# vi /etc/vsftpd/ftpusers 
# Users that are not allowed to login via ftp
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
以上用户内容不难了解为何无法以root用户账号登录,因为root账号默认存在此文件,如果要允许以root账号进行登录,只需要把账号去除即可。为了系统的安全,并不赞成这样做。其它账号均为系统账号非实际用户账户建议保留。
 
vsftp服务器配置文件

/etc/vsftpd/vsftpd.conf文件时vsftp服务器最主要的配置文件,其中包含了许多重要的设置选项,建议仔细研读其中的内容,使vsftp服务器发挥最大的功能和安全性。
配置etc/vsftpd/vsftpd.conf文件:
nano -w /etc/vsftpd/vsftpd.conf
配置文件范例:
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/vsftpd.log
#
# If you want, you can have your log file in standard ftpd xferlog format
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# 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().
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and 
# listens on IPv4 sockets. This directive cannot be used in conjunction 
# with the listen_ipv6 directive.
listen=YES
#
# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd whith two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES
pam_service_name=vsftpd
chroot_local_user=YES
userlist_enable=YES
tcp_wrappers=YES
注:此配置文件仅在默认配置文件基础上修改为如下两项:
anonymous_enable=NO #禁止匿名登录
chroot_local_user=YES #把系统内所有的FTP用户都限制在家目录中
 
7.查看谁登陆了FTP,并杀死它的进程
pstree |grep ftp
kill  -9 进程号pid也可以killall进程名字。
 
8.使vsftp下root登录:
1)编辑两个FTP配置文件:/etc/vsftpd/ftpusers和/etc/vsftpd/user_list,将root那一行删掉或#注释掉;
 
第二种方法:
1 打开 /etc/vsftpd/vsftpd.conf文件。将anonymous_enable=YES,改为anonymous_enable=NO
 
  2 打开 /etc/vsftpd/vsftpd.conf文件。添加user_config_dir=/etc/vsftpd/virtual,并建立virtual目录。在此目录中建立以用户名为文件名的文件,并写入:local_root=[目录],这个目录即是FTP连接时的主目录。
  3 限定用户只在自己目录:修改vsftpd.conf文件,取消注释:
  chroot_list_enable=YES
  chroot_list_file=/etc/vsftpd/chroot_list
  在/etc/vsftpd/目录下添加文件chroot_list,加入作为FTP用户的本地用户名。
  4 解决用户无法进入目录问题:
  打开终端,输入:setsebool -P ftpd_disable_trans 1
  然后重启FTP服务:service vsftpd restart
 
四、权限:
假设是/var/www/html
这个目录的权限应该是770,owner是root,group是ftp
chmod 770 /var/www/html
chown root:ftp /var/www/html
2)重启vsftpd服务:
service vsftpd restart
 


本文转自zh888 51CTO博客,原文链接:http://blog.51cto.com/zh888/366164,如需转载请自行联系原作者