TFTP一般用于向目标板下载镜像文件,TFTP是用来下载远程文件的最简单网络协议,它基于UDP协议而实现

env

  • centos6.5

#tftp

ubuntu tftpd

deploy

1.安装tftp软件包

yum install -y tftp tftp-server xinetd

[root@r6-24 ~]# rpm -ql tftp-server
/etc/xinetd.d/tftp   //配置
/usr/sbin/in.tftpd   //主程序
/usr/share/doc/tftp

2.配置tftp

tee >/etc/xinetd.d/tftp <<EOF
service tftp
{      
       socket_type           = dgram
       protocol              = udp
       wait                  = yes
       user                  = root
       server                = /usr/sbin/in.tftpd
       server_args           = -s /var/lib/tftpboot -c
       disable               = no
       per_source            = 11
       cps                   = 100 2
       flags                 = IPv4
}
EOF

per_source: 指定每个IP地址允许的最大连接数
cps: 指定每秒允许的数据包数量
flags: 指定套接字的选项,IPv4表示只使用IPv4地址
/var/lib/tftpboot //tftroot home

确保/var/lib/tftpboot权限(the default upload user is "nobody")
chmod o+w  /var/lib/tfpboot

#/etc/xinetd.d/tftp(default)

[root@r6-24 xinetd.d]# cat 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 /var/lib/tftpboot
        disable                 = yes
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

3.启动tftp服务

/etc/init.d/iptables stop   //关闭防火墙

/sbin/service xinetd start
or
service xinetd restart
or
/etc/init.d/xinetd start

[root@r6-24 xinetd.d]# /sbin/service xinetd start
Starting xinetd:                                           [  OK  ]
[root@r6-24 xinetd.d]# /sbin/service xinetd status
xinetd (pid  1436) is running...
[root@r6-24 xinetd.d]# 

4.检查

# 日志
tail -f /var/log/message
....
Jun 24 12:08:54 r6-24 xinetd[1709]: xinetd Version 2.3.14 started with libwrap loadavg labeled-networking options compiled in.
Jun 24 12:08:54 r6-24 xinetd[1709]: Started working: 1 available service

# 登陆测试及查看传输模式
tftp 172.24.20.31
tftp> status
Connected to 172.24.20.31.
Mode: netascii Verbose: off Tracing: off Literal: off   //默认ascii模式传输文件
Rexmt-interval: 5 seconds, Max-timeout: 25 seconds
tftp> 

# 查看监听端
[root@r6-24 ~]# ss -lnpu |grep tftp
UNCONN     0      0                         *:69                       *:*      users:(("in.tftpd",1672,0))
UNCONN     0      0                         *:51897                    *:*      users:(("tftp",1711,3))

通信协议udp,端口 69

tftp使用测试

1.安装客户端  
yum install -y tftp   

2.tftp命令用法
tftp     your-ip-address

tftp 172.24.20.31
tftp>get <download file>
tftp>put <upload file>
tftp>q

3.查看帮助命令
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


4.客户端使用
[root@r6-24 tmp]# tftp -h (v0.49)
Usage: tftp [-4][-6][-v][-l][-m mode] [host [port]] [-c command]

[root@r6-24 tmp]#  tftp  -4 -m binary  172.24.20.31
tftp> status
Connected to 172.24.20.31.
Mode: octet Verbose: off Tracing: off Literal: off
Rexmt-interval: 5 seconds, Max-timeout: 25 seconds

#高版本tftp支持参数

tftp [option] ... host [port]
    -g 表示下载文件 (get)
    -p 表示上传文件 (put)
    -l 表示本地文件名 (local file)
    -r 表示远程主机的文件名 (remote file)

tftp -g -r a 10.0.0.1    #从远程主机10.0.0.1上下载文件a
tftp -p -l a 10.0.0.1    #从本地上传文件a到10.0.0.1