安装tftp

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.servicetftp.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字段区分,分别是:

  1. 读文件请求包:Read request,简写为RRQ,对应Opcode字段值为1
  2. 写文件请求包:Write requst,简写为WRQ,对应Opcode字段值为2
  3. 文件数据包:Data,简写为DATA,对应Opcode字段值为3
  4. 回应包:Acknowledgement,简写为ACK,对应Opcode字段值为4
  5. 错误信息包: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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值