Linux tftp部署详解

一、简介

tftp命令用于在本机和服务器之间使用TFTP协议传输文件。TFTP是用来下载远程文件的最简单网络协议,它其于UDP协议而实现。
服务器端通过tftp-server或tftpd支持,客户端通过tftp-client或tftp支持。

二、客户端安装

通过yum install -y tftpapt-get install -y tftp指令安转客户端。

root@openwifi-55:~# apt-get install tftp
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  tftp
0 upgraded, 1 newly installed, 0 to remove and 167 not upgraded.
Need to get 16.8 kB of archives.
After this operation, 57.3 kB of additional disk space will be used.
Get:1 http://cn.archive.ubuntu.com/ubuntu hirsute/universe amd64 tftp amd64 0.17-23ubuntu1 [16.8 kB]
Fetched 16.8 kB in 1s (14.3 kB/s)
Selecting previously unselected package tftp.
(Reading database ... 245869 files and directories currently installed.)
Preparing to unpack .../tftp_0.17-23ubuntu1_amd64.deb ...
Unpacking tftp (0.17-23ubuntu1) ...
Setting up tftp (0.17-23ubuntu1) ...
Processing triggers for man-db (2.9.4-2) ...
root@openwifi-55:~# 

三、服务器端安装

服务器端安装需要xinetdtftp-server(或tftpd)软件。通过apt-get或yum直接安装即可。

root@openwifi-55:~# apt-get install tftpd
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  xinetd
The following NEW packages will be installed:
  tftpd xinetd
0 upgraded, 2 newly installed, 0 to remove and 167 not upgraded.
Need to get 122 kB of archives.
After this operation, 359 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://cn.archive.ubuntu.com/ubuntu hirsute/universe amd64 xinetd amd64 1:2.3.15.3-1 [108 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu hirsute/universe amd64 tftpd amd64 0.17-23ubuntu1 [14.6 kB]
Fetched 122 kB in 2s (67.3 kB/s)
Selecting previously unselected package xinetd.
(Reading database ... 245876 files and directories currently installed.)
Preparing to unpack .../xinetd_1%3a2.3.15.3-1_amd64.deb ...
Unpacking xinetd (1:2.3.15.3-1) ...
Selecting previously unselected package tftpd.
Preparing to unpack .../tftpd_0.17-23ubuntu1_amd64.deb ...
Unpacking tftpd (0.17-23ubuntu1) ...
Setting up xinetd (1:2.3.15.3-1) ...
Setting up tftpd (0.17-23ubuntu1) ...
Note: xinetd currently is not fully supported by update-inetd.
Please consult /usr/share/doc/xinetd/README.Debian and itox(8).
update-inetd: warning: cannot add service, /etc/inetd.conf does not exist
Processing triggers for man-db (2.9.4-2) ...
root@openwifi-55:~# 

四、配置TFTP服务器

修改或创建/etc/xinetd.d/tftp文件,内容如下:

root@analog:~# cat /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 /home/tftp -c
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

注意:-s指定server根目录,-c指定可以创建文件,同时需要创建根目录(/home/tftp)并且给予读写权限。

五、重启并检查服务

通过service xinetd restartetc/init.d/xinetd start指令重启服务。

检查tftp服务是否启动(netstat -a | grep tftp)

[17:03@root /home/tftp]$netstat -a | grep tftp
udp        0      0 0.0.0.0:tftp            0.0.0.0:* 

root@analog:~# netstat -a | grep tftp
udp        0      0 *:tftp                  *:*   

六、TFTP的使用命令

1、TFTP命令行

命令 格式 作用 备注
tftp tftp [ip] 启动TFTP客户端命令行并且连接到指定的IP IP暂时可不指定
connect connect [ip] 连接某个TFTP服务器
put put [local-file] [remote-file] 从本地上传某个文件到服务器
get get [remote-file] [local-file] 从服务器上下载某个文件
quit(q) quit 退出tftp命令行
verbose(v) verbose 切换是否显示详细的处理信息 默认:关
binary(bin) binary 切换到二进制传输模式 默认:ASCII模式
ascii(asc) ascii 切换到ASCII传输模式 默认:ASCII模式
mode `mode [ ascii netascii binary image octet ]` 切换文件传输模式
status status 显示当前的状态信息
trace trace 切换显示包路径信息 默认:关
rexmt remxt [sec] 设置包传输超时时间 单位:秒
timeout timeout [sec] 设置重传超时时间 单位:秒
help(?) help 显示帮助信息
root@openwifi-55:~# tftp
tftp> status
Not connected.
Mode: netascii Verbose: off Tracing: off
Rexmt-interval: 5 seconds, Max-timeout: 25 seconds
tftp> trace
Packet tracing on.
tftp> v
Verbose mode on.
tftp> asc
mode set to netascii
tftp> bin
mode set to octet
tftp> mode
Using octet mode to transfer files.
tftp> status
Not connected.
Mode: octet Verbose: on Tracing: on
Rexmt-interval: 5 seconds, Max-timeout: 25 seconds
tftp> connect 10.134.255.101
tftp> get a.txt aa.txt
getting from 10.134.255.101:a.txt to aa.txt [octet]
sent RRQ <file=a.txt, mode=octet>
received DATA <block=1, 3 bytes>
Received 3 bytes in 0.0 seconds [inf bits/sec]
tftp> put b.txt bb.txt
putting b.txt to 10.134.255.101:bb.txt [octet]
sent WRQ <file=bb.txt, mode=octet>
received ERROR <code=1, msg=File not found>
Error code 1: File not found
tftp> put b.txt
putting b.txt to 10.134.255.101:b.txt [octet]
sent WRQ <file=b.txt, mode=octet>
received ACK <block=0>
sent DATA <block=1, 7 bytes>
received ACK <block=1>
Sent 7 bytes in 0.0 seconds [inf bits/sec]
tftp> rexmt 10
tftp> timeout 30
tftp> status
Connected to 10.134.255.101.
Mode: octet Verbose: on Tracing: on
Rexmt-interval: 10 seconds, Max-timeout: 30 seconds
tftp> 

2、Busybox的命令

命令格式:tftp [option] ... host [port].
[option]可选值如下:

-g:表示下载文件(get)
-p:表示上传文件(put)
-l:表示本地文件名(local file)
-r:表示远程主机的文件名(remote file)

tftp -g -r tmp.txt 10.134.255.101表示从101服务器上下载tmp.txt文件。
tftp -p -l tmp.txt 10.134.255.101表示将本地tmp.txt文件上传到101服务器。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容