5 实现TFTP服务

5.1 TFTP介绍

TFTP:Trivial FIle Transfer Protocol,是一种用于传输文件的简单高级协议,是文件传输协议(FTP)的简化版本。用来传输比文件传输协议(FTP)更易于使用但功能较少的文件

TFTP和FTP的区别

1、安全性区别
FTP支持登录安全,具有适当的身份验证和加密协议,在建立连接期间需要与FTP身份验证通信
TFTP是一种开放协议,缺乏安全性,没有加密机制,与TFTP通信时不需要认证
2、传输层协议的区别
FTP使用TCP作为传输层协议,TFTP使用UDP作为传输层协议
3、使用端口的区别
FTP使用2个端口:TCP端口21,是个侦听端口;TCP端口20或更高TCP端口1024以上用于源连接
TFTP仅使用一个具有停止和等待模式的端口:端口:69/udp
4、RFC的区别
FTP是基于RFC 959文档,带有其他RFC涵盖安全措施;TFTP基于RFC 1350文档
5、执行命令的区别
FTP有许多可以执行的命令(get,put,Is,dir,lcd)并且可以列出目录等
TFTP只有5个指令可以执行(rrq,wrq,data,ack, error)

5.2安装和使用TFTP

安装包:

  • tftp-server 服务器包
  • tftp 客户端包

安装并使用tftp下载文件

[root@centos8 ~]# dnf install tftp-server -y
[root@centos8 ~]# rpm -ql tftp-server
/usr/lib/.build-id
/usr/lib/.build-id/8c
/usr/lib/.build-id/8c/6921a9fb21d66da4fb299d516bce9ee6afea34
/usr/lib/systemd/system/tftp.service   # tftp service文件
/usr/lib/systemd/system/tftp.socket   # tftp socket文件
/usr/sbin/in.tftpd   # tftp主程序
/usr/share/doc/tftp-server
/usr/share/doc/tftp-server/CHANGES
/usr/share/doc/tftp-server/README
/usr/share/doc/tftp-server/README.security
/usr/share/man/man8/in.tftpd.8.gz
/usr/share/man/man8/tftpd.8.gz
/var/lib/tftpboot   # tftp服务数据目录

# 启动服务
[root@centos8 ~]# systemctl enable --now tftp
Created symlink /etc/systemd/system/sockets.target.wants/tftp.socket → /usr/lib/systemd/system/tftp.socket.
[root@centos8 ~]# ss -nulp|grep tftp
UNCONN   0         0                         *:69                     *:*        users:(("in.tftpd",pid=18557,fd=0),("systemd",pid=1,fd=32))  

# 准备测试文件
[root@centos8 ~]# mkdir /var/lib/tftpboot/dir
[root@centos8 ~]# cp /etc/passwd /var/lib/tftpboot/dir/f1.txt

# 安装tftp客户端包
[root@centos7 ~]# yum install tftp -y
# 客户端通过tftp测试下载文件
[root@centos7 ~]# tftp 10.0.0.8
tftp> help
tftp-hpa 5.2
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> get f1.txt
tftp> get dir/f2.txt
tftp> quit
[root@centos7 ~]# ls
anaconda-ks.cfg  centos-release  f1.txt  f2.txt

# 以下在tftp服务器执行,当用户下载文件后,可以观察到服务器自动打开in.tftpd主程序
[root@centos8 ~]# ps aux|grep in.tftp
root      18557  0.0  0.1  14920  1840 ?        Ss   17:53   0:00 /usr/sbin/in.tftpd -s /var/lib/tftpboot
root      18590  0.0  0.1  12116  1000 pts/1    S+   17:59   0:00 grep --color=auto in.tftp