CentOS 7
2.1 安装
假设网络条件允许直接通过yum安装,通过以下命令安装tftp服务端和tftp客户端
# yum install -y tftp-server tftp
安装完后,在/sbin
和/usr/sbin
目录下均多了一个服务端程序文件in.tftpd
,这两个文件的MD5值是一样的
# which in.tftpd
/sbin/in.tftpd
# ls /usr/sbin | grep tftp
in.tftpd
tftp客户端程序则安装在/bin
目录下
# which tftp
/bin/tftp
2.2 配置
使用yum安装完tftp服务端程序后,在/usr/lib/systemd/system
目录下多了两个文件,tftp.service
和tftp.socket
,他们是tftp服务端的配置文件
# pwd
/usr/lib/systemd/system
# cat tftp.service
[Unit]
Description=Tftp Server
Requires=tftp.socket
Documentation=man:in.tftpd
[Service]
ExecStart=/usr/sbin/in.tftpd -c -p -s /var/lib/tftpboot
StandardInput=socket
[Install]
Also=tftp.socket
# cat tftp.socket
[Unit]
Description=Tftp Server Activation Socket
[Socket]
ListenDatagram=69
[Install]
WantedBy=sockets.target
2.3 启动
使用下面的命令启动tftp server
# systemctl daemon-reload
# systemctl enable --now tftp
Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.
查看启动的tftp server状态信息
# systemctl status tftp
● tftp.service - Tftp Server
Loaded: loaded (/usr/lib/systemd/system/tftp.service; indirect; vendor preset: disabled)
Active: active (running) since Tue 2022-12-20 20:59:24 CST; 41s ago
Docs: man:in.tftpd
Main PID: 22776 (in.tftpd)
CGroup: /system.slice/tftp.service
└─22776 /usr/sbin/in.tftpd -c -p -s /tftpboot
Dec 20 20:59:24 yq01-sys-netadmin01.yq01.baidu.com systemd[1]: Started Tftp Server.
# lsof -i :69
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root 22u IPv6 400870756 0t0 UDP *:tftp
in.tftpd 22776 root 0u IPv6 400870756 0t0 UDP *:tftp
3 测试
tftp客户端通过put
命令上传文件,通过get
命令下载文件,但不支持list文件服务器的文件和目录列表,也不支持删除文件
tftp支持的所有命令:
# 检查tftp客户端是否已安装,系统一般默认安装
$ which tftp
/usr/bin/tftp
$ tftp -v 127.0.0.1
Connected to 127.0.0.1 (127.0.0.1), port 69
tftp> ?
tftp-hpa 0.49
Commands may be abbreviated. Commands are:
connect connect to remote tftp
mode set file transfer mode
put send file
get receive file
quit exit tftp
verbose toggle verbose mode
trace toggle packet tracing
literal toggle literal mode, ignore ':' in file name
status show current status
binary set mode to octet
ascii set mode to netascii
rexmt set per-packet transmission timeout
timeout set total retransmission timeout
? print help information
help print help information
tftp> quit
测试文件上传和下载
$ tftp 127.0.0.1
tftp> put test.txt
tftp> get test.txt
tftp> quit
或
# 上传
$ tftp -v 127.0.0.1 -c put test.txt
Connected to 127.0.0.1 (127.0.0.1), port 69
putting test.txt to 127.0.0.1:test.txt [netascii]
Sent 19 bytes in 0.0 seconds [8069 bit/s]
# tftpd文件目录下查看上传的文件
$ ls /var/lib/tftpboot
test.txt
$ rm -f test.txt
$ ls
# 下载
$ tftp -v 127.0.0.1 -c get test.txt
Connected to 127.0.0.1 (127.0.0.1), port 69
getting from 127.0.0.1:test.txt to test.txt [netascii]
Received 19 bytes in 0.0 seconds [17714 bit/s]
# 查看下载的文件
$ ls
test.txt
4 原理
4.1 协议概述
TFTP英文全称:Trivial File Transfer Protocol
,中文全称:简单文件传输协议。提供不复杂、开销不大的文件传输服务。端口号为69。基于UDP协议。
4.2 TFTP报文类型
TFTP共定义了五种类型的包,类型的区分由包数据前两个字节的Opcode字段区分,分别是:
- 读文件请求包:Read request,简写为RRQ,对应Opcode字段值为1
- 写文件请求包:Write requst,简写为WRQ,对应Opcode字段值为2
- 文件数据包:Data,简写为DATA,对应Opcode字段值为3
- 回应包:Acknowledgement,简写为ACK,对应Opcode字段值为4
- 错误信息包:Error,简写为ERROR,对应Opcode字段值为5
4.3 TFTP端口号分配
TFTP客户端发送read request和write request报文的时候,目的端口是69。而Data、Acknowledgement、Error不使用69端口,它们使用的是随机端口1024~5000。
不同的操作系统有不同的端口号规定Linux使用32768~61000、Windows 使用1025~5000