简单文件传输协议(Trivial File Transfer Protocol,TFTP)是一种基于UDP协议在客户端和服务器之间进行简单文件传输的协议。顾名思义,它提供不复杂、开销不大的文件传输服务(可将其当作FTP协议的简化版本)。
TFTP的命令功能不如FTP服务强大,甚至不能遍历目录,在安全性方面也弱于FTP服务。而且,由于TFTP在传输文件时采用的是UDP协议,占用的端口号为69,因此文件的传输过程也不像FTP协议那样可靠。但是,因为TFTP不需要客户端的权限认证,也就减少了无谓的系统和网络带宽消耗,因此在传输琐碎(trivial)不大的文件时,效率更高。
TFTP服务的搭建,需要xinetd的支持。xinetd即extended internet daemon,xinetd是新一代的网络守护进程服务程序,又叫超级Internet服务器。经常用来管理多种轻量级Internet服务。xinetd提供类似于inetd+tcp_wrapper的功能,但是更加强大和安全。
在CentOS 7系统中,TFTP服务是使用xinetd服务程序来管理的。xinetd服务可以用来管理多种轻量级的网络服务,而且具有强大的日志功能。简单来说,在安装TFTP软件包后,还需要在xinetd服务程序中将其开启,把默认的禁用(disable)参数修改为no, 然后,重启xinetd服务并将它添加到系统的开机启动项中,以确保TFTP服务在系统重启后依然处于运行状态。考虑到有些系统的防火墙默认没有允许UDP协议的69端口,因此需要手动将该端口号加入到防火墙的允许策略中。
环境:CentOS7.4
服务端:192.168.20.104
客户端:192.168.20.13
下载支持 TFTP 服务的服务程序
服务端:192.168.20.104
[root@server104 ~]$ yum install -y tftp-server tftp xinetd
[root@server104 ~]$ vim /etc/xinetd.d/tftp
将 disable = no 改为 disable = yes
[root@server104 ~]$ systemctl restart xinetd #重启 xinetd 服务
[root@server104 ~]$ systemctl enable xinetd #将 xinetd 服务加入开机自启
[root@server104 ~]$ iptables -F #清楚防火墙规则
[root@server104 ~]$ systemctl stop firewalld #关闭防火墙
[root@server104 ~]$ systemctl disable firewalld #关闭防火墙开机自启
TFTP的根目录为/var/lib/tftpboot
tftp命令中可用的参数以及作用
命令作用
? 帮助信息
put 上传文件
get 下载文件
verbose显示详细的处理信息
status显示当前的状态信息
binary使用二进制进行传输
ascii使用ASCII码进行传输
timeout设置重传的超时时间
quit退出
服务端:192.168.20.104
[root@server104 ~]$ echo 123 > /var/lib/tftpboot/a.txt
[root@server104 ~]$ ls /var/lib/tftpboot/
a.txt
[root@server104 ~]$ cat /var/lib/tftpboot/a.txt
123
[root@server104 ~]$ cd /tmp/
[root@server104 /tmp]$ rm -rf *
[root@server104 /tmp]$ tftp 192.168.20.104
tftp> get a.txt
tftp> quit
[root@server104 /tmp]$ ls
a.txt
[root@server104 /tmp]$ cat a.txt
123
客户端:192.168.20.13
[root@server13 ~]$ yum install -y tftp xinetd
[root@server13 ~]$ systemctl restart xinetd #重启 xinetd 服务
[root@server13 ~]$ systemctl enable xinetd #将 xinetd 服务加入开机自启
[root@server13 ~]$ iptables -F #清楚防火墙规则
[root@server13 ~]$ systemctl stop firewalld #关闭防火墙
[root@server13 ~]$ systemctl disable firewalld #关闭防火墙开机自启
[root@server13 ~]$ cd /tmp/
[root@server13 /tmp]$ rm -rf *
[root@server13 /tmp]$ ls
[root@server13 /tmp]$ tftp 192.168.20.104
tftp> get a.txt
tftp> quit
[root@server13 /tmp]$ ls
a.txt
[root@server13 /tmp]$ cat a.txt
123
问题:如果出现看不到内容的情况,那么操作下面命令(也有可能是你把需要下载的文件名打错了,那么该目录会出现一个空白的错误文件名的文件)
在服务端和客户端都可以操作:
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null
setenforce 0
iptables -F
上传文件:put
服务端:192.168.20.104
[root@server104 /tmp]$ pwd
/tmp
[root@server104 /tmp]$ echo 777 > b.txt
[root@server104 /tmp]$ tftp 192.168.20.104
tftp> put b.txt
Error code 1: File not found
tftp> quit
[root@server104 /tmp]$ cat /etc/xinetd.d/tftp
service tftp
{
disable = no
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -B 1380 -v -s /var/lib/tftpboot -c #需要修改这一行
per_source = 11
cps = 100 2
flags = IPv4
}
[root@server104 /tmp]$ systemctl restart xinetd
[root@server104 /tmp]$ tftp 192.168.20.104
tftp> put b.txt
Error code 0: Permission denied
tftp> quit
[root@server104 /tmp]$ ll -d /var/lib/tftpboot/
drwxr-xr-x 2 root root 19 Jan 6 21:57 /var/lib/tftpboot/
[root@server104 /tmp]$ chmod 777 -Rf /var/lib/tftpboot/ #权限给太大可能会有风险,但这是测试环境,没有关系
[root@server104 /tmp]$ ll -d /var/lib/tftpboot/
drwxrwxrwx 2 root root 19 Jan 6 21:57 /var/lib/tftpboot/
[root@server104 /tmp]$ tftp 192.168.20.104
tftp> put b.txt
tftp> quit
[root@server104 /tmp]$ ll /var/lib/tftpboot/
total 8
-rwxrwxrwx 1 root root 4 Jan 6 21:57 a.txt
-rw-rw-rw- 1 nobody nobody 4 Jan 6 22:43 b.txt
客户端:192.168.20.13
[root@server13 /tmp]$ tftp 192.168.20.104
tftp> get b.txt
tftp> quit
[root@server13 /tmp]$ ls
a.txt b.txt
[root@server13 /tmp]$ cat b.txt
777
到此,TFTP 简单文件传输协议就搭建成功了,自己再多去测试下吧!