文件传输法——TFTP服务

Published: by

有些时候需要将现实设备中的文件传输出来,设备中存在tftp命令的时候可以采用这种方法:在本地搭建TFTP服务器,然后再设备中执行tftp命令将文件传输到TFTP服务器上。

在Ubuntu上搭建TFTP服务

参考文章Ubuntu16.04中搭建TFTP 和 NFS 服务器

1.安装

$ apt-get install tftp-hpa tftpd-hpa
$ sudo apt-get install xinetd

2.建立目录

$ mkdir /tftpboot # 这是建立tftp传输目录。
$ sudo chmod 777 /tftpboot #为文件夹赋权

3.配置

$ vim /etc/xinetd.conf

修改为

# Simple configuration file for xinetd
#
# Some defaults, and include /etc/xinetd.d/
defaults
{
# Please note that you need a log_type line to be able to use log_on_success
# and log_on_failure. The default is the following :
# log_type = SYSLOG daemon info
}
includedir /etc/xinetd.d
$ vim /etc/default/tftpd-hpa

修改为

TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/tftpboot" # 这里是你的tftpd-hpa的服务目录,这个想建立在哪里都行
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="-l -c -s" # 这里是选项,-c是可以上传文件的参数,-s是指定tftpd-hpa服务目录,上面已经指定
$ vim /etc/xinetd.d/tftp

修改为

service tftp
{
socket_type = dgram
wait = yes 
disable = no
user = root
protocol = udp 
server = /usr/sbin/in.tftpd
server_args = -s /tftpboot
#log_on_success += PID HOST DURATION
#log_on_failure += HOST
per_source = 11
cps =100 2
flags =IPv4
}

4.重启服务

$ sudo service tftpd-hpa restart
$ sudo /etc/init.d/xinetd reload
$ sudo /etc/init.d/xinetd restart

5.测试使用

$ tftp localhost  #localhost 表示本机
tftp>get test.txt  //从 tftpboot 目录下载test.txt
tftp>put test1.txt //向 tftpboot 目录上传test1.txt
tftp>q             //退出

TFTP命令

参数说明:

-l 是local的缩写,后跟存在于Client的源文件名,或下载Client后 重命名的文件名。
-r 是remote的缩写,后跟Server即PC机tftp服务器根目录中的源文件名,或上传Server后重命名后的文件名。
-g 是get的缩写,下载文件时用,
-p 是put的缩写,上传文件时用,
tftp默认占用的是69端口。
$ tftp –g –l 目标文件名 –r 源文件名 服务器地址 //下载

$ tftp –p –r 目标文件名  -l 源文件名 服务器地址//上传

$ tftp –g –l/-r 源文件名   服务器地址        //不更名下载

$ tftp –p –l/-r 源文件名   服务器地址        //不更名上传