1.tftp简介
TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂、开销不大的文件传输服务。由于只进行小文件传输的,因此不具有FTP的许多功能,比如,只能从文件服务器上获得或写入文件,不能列出目录,不进行认证等等。
对于技术人员,特别是嵌入式开发人员或者需要通过TFTP升级固件的IT人员,可能会经常用到TFTP。
2.tftp的安装
首先确认系统上是否安装了tftp软件包:
[xing@localhost ~]$ rpm -qa | grep tftp
tftp-server-0.49-7.el6.x86_64
若未安装,使用sudo yum install -y tftp-server
sudo yum install -y tftp
sudo yum install -y xinetd
3.tftp的配置
tftp的配置文件在/etc/xinetd.d/tftp下:
[xing@localhost ~]$ sudo vim /etc/xinetd.d/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
{
disable = no 加入这一项
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /tftp -c #修改这一项,这里-s指tftp服务器 的根目录,-c指能创建文件
per_source = 11
cps = 100 2
flags = IPv4
}
4.开启xinetd服务
[xing@localhost ~]$ sudo service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
使用netstat命令查看69端口:
[xing@localhost ~]$ sudo netstat -nlp | grep 69
udp 0 0 0.0.0.0:69 0.0.0.0:* 5745/xinetd
unix 2 [ ACC ] STREAM LISTENING 21057 3269/nautilus /tmp/orbit-tangbin/linc-cc5-0-ebc26e628dca
5.SeLinux策略修改
SeLinux保持开启状态的话,系统有可能会组织tftp客户端的下载,可以将它暂时关闭:
[xing@localhost ~]$ sudo setenforce 0
#这里0表示设置SeLinux为permissive模式,1代表设置SeLinux为enforcing模式
可以使用getenforce 命令查看SeLinux状态:
[xing@localhost ~]$ getenforce
Permissive
如果想彻底禁用SeLinux,修改其配置文件将它禁用:
[xing@localhost ~]$ sudo vim /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled #此处设置为disabled即可
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
6.防火墙策略修改
系统开启了防火墙也有可能会阻止tftp客户端的下载,我们可以在防火墙规则中使能tftp,只需要使能tftp所使用的69端口即可。
[xing@localhost ~]$ sudo /sbin/iptables -I INPUT -p tcp --dport 69 -j ACCEPT
[xing@localhost ~]$ sudo /sbin/iptables -I INPUT -p udp --dport 69 -j ACCEPT
[xing@localhost ~]$ sudo /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[xing@localhost ~]$ sudo /sbin/iptables -I INPUT -p tcp --dport 21 -j ACCEPT
[xing@localhost ~]$ sudo /sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT
保存:
[xing@localhost ~]$ sudo /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
重启防火墙:
[xing@localhost ~]$ sudo service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
查看防火墙状态:
[xing@localhost ~]$ sudo service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
2 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
4 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:69
5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:69
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
也可使用sudo service iptables stop关闭防火墙。
如果希望在系统启动时防火墙不启动,我们可以用ntsysv关闭防火墙服务,同时还可 以设置tftp服务在系统启动时就开启:
[xing@localhost ~]$ sudo ntsysv
[ ] ip6tables
[ ] iptables
[*] tftp
[*] xinetd
7.tftp命令下载测试
首先在/tftp/创建一个待测文件tt.txt:
[xing@localhost ~]$ cd /tftp/
[xing@localhost tftp]$ sudo touch tt.txt
[xing@localhost tftp]$ ls
tt.txt
接着安装busybox里的tftp客户端命令:
[xing@localhost ~]$ wget http://www.busybox.net/downloads/busybox-1.19.3.tar.bz2
[xing@localhost ~]$ tar -xjf busybox-1.19.3.tar.bz2
[xing@localhost ~]$ cd busybox-1.19.3
[xing@localhost busybox-1.19.3]$ export TERM=vt100
[xing@localhost busybox-1.19.3]$ sudo make menuconfig 不要做任何修改
[xing@localhost busybox-1.19.3]$ make
[xing@localhost busybox-1.19.3]$ file busybox
busybox: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), stripped
[xing@localhost busybox-1.19.3]$ sudo cp busybox /usr/local/bin/
[xing@localhost busybox-1.19.3]$ cd
[xing@localhost ~]$ cd /usr/local/bin/
[xing@localhost bin]$ sudo ln -s busybox tftp
使用busybox里的tftp命令测试:
[xing@localhost ~]$ tftp -gr tt.txt 192.168.1.115
[xing@localhost ~]$ ls tt.txt
tt.txt
#显示已经下载该文件
8.可能遇到的问题及解决
·在进行make menuconfig 出错,解决方法是安装ncurses库:sudo yum install ncurses-devel。ncurses库是字符终端下屏幕控制的基本库。
·出现Your display is too small to run Menuconfig!提示。只需要将你的终端窗口最大化或
·tftp目录必须创建在根目录下,如果创建在自己的主目录下,netstat -a | grep tftp这里是没有反应的,tftp服务打不开。