红联Linux门户
Linux帮助

Linux下FTP和TFTP服务配置

发布时间:2015-04-08 10:09:12来源:linux网站作者:linux人

FTP和TFTP是我们经常使用的文件传输协议。在Linux中,sftp协议由于其安全性的优点,被作为默认的链接协议。但是,一些场合下,我们依然需要使用ftp和tftp协议进行文件传输。本篇主要介绍配置方法,供有需要的朋友待查。


1、环境准备

我们选择Linux 2.6内核进行测试。

[root@SimpleLinuxUp ~]# uname -r

2.6.18-128.el5

当前OS运行在level 3模式下。

[root@SimpleLinuxUp ~]# grep init /etc/inittab

# inittab      This file describes how the INIT process should set up

#  0 - halt (Do NOT set initdefault to this)

#  6 - reboot (Do NOT set initdefault to this)

id:3:initdefault:

# System initialization.

si::sysinit:/etc/rc.d/rc.sysinit

默认情况下,tftp服务器包是安装上的,而FTP服务器没有安装。

[root@SimpleLinuxUp Server]# rpm -qa | grep ftp

tftp-server-0.42-3.1

ftp-0.17-35.el5

lftp-3.5.1-2.fc6


2、TFTP服务器安装配置

TFTP是一种比较特殊的文件传输协议。相对于FTP和目前经常使用的SFTP,TFTP是基于TCP/IP协议簇,用于进行简单文件传输,提供简单、低开销的传输服务。TFTP的端口设置为69。
 
相对于常见的FTP,TFTP有两个比较好的优势:

TFTP基于UDP协议,如果环境中没有TCP协议,是比较合适的;

TFTP执行和代码占用内存量比较小;

默认情况下,Linux内部是安装了tftp服务器包的。但是默认是不启动的。

[root@SimpleLinuxUp ~]# chkconfig --list tftp

tftp            off

启用和禁用tftp服务是通过配置文件/etc/xinetd.d/tftp,将其中参数设置。

[root@SimpleLinuxUp ~]# vi /etc/xinetd.d/tftp

# default: off

# description: The tftp server serves files using the trivial file transfer \

#      protocol.  The tftp protocol is often used to boot diskless \

#      workstations, download configuration files to network-aware printers, \

#      and to start the installation process for some operating systems.

service tftp

{

socket_type            = dgram

protocol                = udp

wait                    = yes

user                    = root

server                  = /usr/sbin/in.tftpd

server_args            = -s /tftpboot -c

disable                = no

per_source              = 11

cps                    = 100 2

flags                  = IPv4

}

配置文件中,将disable默认值从yes改为no。适当修改server_args参数,主要是其中的tftp根目录地址。

Tftp服务是不需要单独启动的,是作为xinetd服务的一个附属对象连带启动。

[root@SimpleLinuxUp ~]# service xinetd status

xinetd (pid 2194) is running...

[root@SimpleLinuxUp ~]# cd /

[root@SimpleLinuxUp /]# mkdir /tftpboot

mkdir: cannot create directory `/tftpboot': File exists

[root@SimpleLinuxUp /]# cd /tftpboot/

[root@SimpleLinuxUp tftpboot]# cd ..

[root@SimpleLinuxUp /]# chmod -R 777 /tftpboot/

由于连接使用UDP端口,我们将防火墙和SELinux配置关闭。

[root@SimpleLinuxUp /]# service iptables stop

[root@SimpleLinuxUp /]# service iptables status

Firewall is stopped.

对xinetd服务重启,连带将tftp服务启动。

[root@SimpleLinuxUp /]# service xinetd restart

Stopping xinetd: [  OK  ]

Starting xinetd: [  OK  ]

[root@SimpleLinuxUp /]# chkconfig --list tftp

tftp            on

使用netstat判断UDP端口开启。

[root@SimpleLinuxUp /]# netstat -nlp | grep udp

udp        0      0 0.0.0.0:772                0.0.0.0:*                              1868/rpc.statd    

udp        0      0 0.0.0.0:775                0.0.0.0:*                              1868/rpc.statd    

udp        0      0 0.0.0.0:69                  0.0.0.0:*                              3869/xinetd      

(篇幅原因,有省略……)

从远程服务器启动连接,笔者从windows环境客户端启动。TFTP是可以不输入用户名和密码的,所以对于安全文件传输是不满足的。

C:\Documents and Settings\liuzy>tftp

Transfers files to and from a remote computer running the TFTP service.

TFTP [-i] host [GET | PUT] source [destination]

-i              Specifies binary image transfer mode (also called

octet). In binary image mode the file is moved

literally, byte by byte. Use this mode when

transferring binary files.

host            Specifies the local or remote host.

GET            Transfers the file destination on the remote host to

the file source on the local host.

PUT            Transfers the file source on the local host to

the file destination on the remote host.

source          Specifies the file to transfer.

destination    Specifies where to transfer the file.

测试客户端与服务器根目录之间的文件互相拷贝传输。

C:\Documents and Settings\liuzy>tftp 192.168.0.100 put cogtrwin.ini

Transfer successful: 536 bytes in 1 second, 536 bytes/s

[root@SimpleLinuxUp tftpboot]# ls -l

total 12

-rw-rw-rw- 1 nobody nobody  507 Jan 28 10:39 cogtrwin.ini

drwxrwxrwx 4 root  root  4096 Dec 26 09:46 linux-install

D:\>tftp 192.168.0.100 get cogtrwin.ini

Transfer successful: 536 bytes in 1 second, 536 bytes/s

TFTP是一种简单的文件传输解决方案。


3、FTP配置

目前成熟系统设计中,都将FTP协议和传输定性为非安全传输协议。它和telnet登录方式,逐渐为SFTP和SSH协议所取代。在Linux流行版本中,SFTP已经成为默认配置项目。
 
在Linux发行版的光盘中,已经包括了vsftp服务器安装包,是需要手工安装。

判断没有安装vsftp

[root@SimpleLinuxUp ~]# rpm -qa | grep vsftp

加载安装光盘到一个目录中。

[root@SimpleLinuxUp /]# ls -l | grep rdcom

drwxr-xr-x  2 root  root      4096 Dec 26 10:29 rdcom

[root@SimpleLinuxUp /]# cd rdcom/

[root@SimpleLinuxUp rdcom]# ls -l

total 0

[root@SimpleLinuxUp rdcom]# mount /dev/cdrom /rdcom/

mount: block device /dev/cdrom is write-protected, mounting read-only

安装vsftp-server包。

[root@SimpleLinuxUp rdcom]# cd Server/

[root@SimpleLinuxUp Server]# pwd

/rdcom/Server

[root@SimpleLinuxUp Server]# ls -l | grep vsftp

-r--r--r--  99 root root  141003 Dec 17  2007 vsftpd-2.0.5-12.el5.i386.rpm

[root@SimpleLinuxUp Server]# rpm -ivh vsftpd-2.0.5-12.el5.i386.rpm

warning: vsftpd-2.0.5-12.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
 
Preparing...                ############################ [100%]

1:vsftpd                ############################## [100%]

安装后,vsftpd是没有自动启动的,作为系统服务也不能自动启动。

[root@SimpleLinuxUp Server]# service vsftpd status

vsftpd is stopped

[root@SimpleLinuxUp Server]# chkconfig --list vsftpd

vsftpd          0:off  1:off  2:off  3:off  4:off  5:off  6:off

启动服务,并且配置vsftp服务到适合的level等级。

[root@SimpleLinuxUp Server]# service vsftpd start

Starting vsftpd for vsftpd: [  OK  ]

[root@SimpleLinuxUp Server]# service vsftpd status

vsftpd (pid 4030) is running...

[root@SimpleLinuxUp Server]# chkconfig --level 2345 vsftpd on

[root@SimpleLinuxUp Server]# chkconfig --list vsftpd

vsftpd          0:off  1:off  2:on    3:on    4:on    5:on    6:off

远程从客户端进行访问,直接使用ftp命令行客户端。

D:\>ftp

ftp> open 192.168.0.100

Connected to 192.168.0.100.

220 (vsFTPd 2.0.5)

User (192.168.0.100:(none)): Oracle

331 Please specify the password.

Password:

230 Login successful.

注意,处于安全的考虑,并不是所有的用户都可以使用ftp远程连接,比如root。

D:\>ftp

ftp> open 192.168.0.100

Connected to 192.168.0.100.

220 (vsFTPd 2.0.5)

User (192.168.0.100:(none)): root

530 Permission denied.

Login failed.

如果确实需要root登录,可以修改配置文件/etc/vsftpd/ftpusers和/etc/vsftpd/user_list,将其中的root屏蔽住。就可以支持登录。
 
[root@SimpleLinuxUp Server]# cat /etc/vsftpd/ftpusers

# Users that are not allowed to login via ftp

root

bin

daemon

adm

nobody

[root@SimpleLinuxUp Server]# cat /etc/vsftpd/user_list

# vsftpd userlist

# If userlist_deny=NO, only allow users in this file

# If userlist_deny=YES (default), never allow users in this file, and

# do not even prompt for a password.

# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers

# for users that are denied.

root

bin

daemon

adm

lp

sync

shutdown

halt

mail

news

uucp

operator

games

nobody

安装成功。


4、结论

在Linux、AIX中,很多的配置内容都是命令行+文本配置的方法来实现的。本篇介绍了Linux环境下tftp和ftp的配置手段,留待需要朋友待查。